@@ -766,8 +766,7 @@ let rec diff a b = match a with
766766 | a :: aa ->
767767 if List. mem b a then diff aa b else a :: (diff aa b)
768768
769- let watch_volume_plugins ~root_dir ~switch_path =
770- let root_dir = Filename. concat root_dir " volume" in
769+ let watch_volume_plugins ~root_dir ~switch_path ~pipe =
771770 let create switch_path root_dir name =
772771 if Hashtbl. mem servers name
773772 then return ()
@@ -795,11 +794,8 @@ let watch_volume_plugins ~root_dir ~switch_path =
795794 Deferred. all_ignore (List. map ~f: (create switch_path root_dir) (diff needed got_already))
796795 >> = fun () ->
797796 Deferred. all_ignore (List. map ~f: (destroy switch_path) (diff got_already needed)) in
798- Async_inotify. create ~recursive: false ~watch_new_dirs: false root_dir
799- >> = fun (watch , _ ) ->
800797 sync ~root_dir ~switch_path
801798 >> = fun () ->
802- let pipe = Async_inotify. pipe watch in
803799 let open Async_inotify.Event in
804800 let rec loop () =
805801 ( Pipe. read pipe >> = function
@@ -824,8 +820,7 @@ let watch_volume_plugins ~root_dir ~switch_path =
824820 loop () in
825821 loop ()
826822
827- let watch_datapath_plugins ~root_dir =
828- let root_dir = Filename. concat root_dir " datapath" in
823+ let watch_datapath_plugins ~root_dir ~pipe =
829824 let sync ~root_dir =
830825 Sys. readdir root_dir
831826 >> = fun names ->
@@ -834,11 +829,8 @@ let watch_datapath_plugins ~root_dir =
834829 Deferred. all_ignore (List. map ~f: (Datapath_plugins. register root_dir) (diff needed got_already))
835830 >> = fun () ->
836831 Deferred. all_ignore (List. map ~f: (Datapath_plugins. unregister root_dir) (diff got_already needed)) in
837- Async_inotify. create ~recursive: false ~watch_new_dirs: false root_dir
838- >> = fun (watch , _ ) ->
839832 sync ~root_dir
840833 >> = fun () ->
841- let pipe = Async_inotify. pipe watch in
842834 let open Async_inotify.Event in
843835 let rec loop () =
844836 ( Pipe. read pipe >> = function
@@ -866,14 +858,32 @@ let watch_datapath_plugins ~root_dir =
866858let main ~root_dir ~state_path ~switch_path =
867859 Attached_SRs. reload state_path
868860 >> = fun () ->
869- Deferred. all_unit [
870- watch_volume_plugins ~root_dir ~switch_path ;
871- watch_datapath_plugins ~root_dir
872- ]
861+ let datapath_root = Filename. concat root_dir " datapath" in
862+ Async_inotify. create ~recursive: false ~watch_new_dirs: false datapath_root
863+ >> = fun (watch , _ ) ->
864+ let datapath = Async_inotify. pipe watch in
865+ let volume_root = Filename. concat root_dir " volume" in
866+ Async_inotify. create ~recursive: false ~watch_new_dirs: false volume_root
867+ >> = fun (watch , _ ) ->
868+ let volume = Async_inotify. pipe watch in
873869
874- let main ~root_dir ~state_path ~switch_path =
875- let (_: unit Deferred.t ) = main ~root_dir ~state_path ~switch_path in
876- never_returns (Scheduler. go () )
870+ let rec loop () =
871+ Monitor. try_with
872+ (fun () ->
873+ Deferred. all_unit [
874+ watch_volume_plugins ~root_dir: volume_root ~switch_path ~pipe: volume;
875+ watch_datapath_plugins ~root_dir: datapath_root ~pipe: datapath
876+ ]
877+ )
878+ >> = function
879+ | Ok () ->
880+ info " main thread shutdown cleanly" ;
881+ return ()
882+ | Error x ->
883+ error " main thread failed with %s" (Exn. to_string x);
884+ Clock. after (Time.Span. of_sec 5. ) >> = fun () ->
885+ loop () in
886+ loop ()
877887
878888open Xcp_service
879889
@@ -918,5 +928,20 @@ let _ =
918928 use_syslog := true ;
919929 info " Daemonisation successful." ;
920930 end ;
921- main ~root_dir: ! root_dir ~state_path: ! state_path ~switch_path: ! Xcp_client. switch_path
931+ let (_: unit Deferred.t ) =
932+ let rec loop () =
933+ Monitor. try_with
934+ (fun () ->
935+ main ~root_dir: ! root_dir ~state_path: ! state_path ~switch_path: ! Xcp_client. switch_path
936+ )
937+ >> = function
938+ | Ok () ->
939+ info " main thread shutdown cleanly" ;
940+ return ()
941+ | Error x ->
942+ error " main thread failed with %s" (Exn. to_string x);
943+ Clock. after (Time.Span. of_sec 5. ) >> = fun () ->
944+ loop () in
945+ loop () in
946+ never_returns (Scheduler. go () )
922947
0 commit comments