@@ -765,8 +765,7 @@ let rec diff a b = match a with
765
765
| a :: aa ->
766
766
if List. mem b a then diff aa b else a :: (diff aa b)
767
767
768
- let watch_volume_plugins ~root_dir ~switch_path =
769
- let root_dir = Filename. concat root_dir " volume" in
768
+ let watch_volume_plugins ~root_dir ~switch_path ~pipe =
770
769
let create switch_path root_dir name =
771
770
if Hashtbl. mem servers name
772
771
then return ()
@@ -794,11 +793,8 @@ let watch_volume_plugins ~root_dir ~switch_path =
794
793
Deferred. all_ignore (List. map ~f: (create switch_path root_dir) (diff needed got_already))
795
794
>> = fun () ->
796
795
Deferred. all_ignore (List. map ~f: (destroy switch_path) (diff got_already needed)) in
797
- Async_inotify. create ~recursive: false ~watch_new_dirs: false root_dir
798
- >> = fun (watch , _ ) ->
799
796
sync ~root_dir ~switch_path
800
797
>> = fun () ->
801
- let pipe = Async_inotify. pipe watch in
802
798
let open Async_inotify.Event in
803
799
let rec loop () =
804
800
( Pipe. read pipe >> = function
@@ -823,8 +819,7 @@ let watch_volume_plugins ~root_dir ~switch_path =
823
819
loop () in
824
820
loop ()
825
821
826
- let watch_datapath_plugins ~root_dir =
827
- let root_dir = Filename. concat root_dir " datapath" in
822
+ let watch_datapath_plugins ~root_dir ~pipe =
828
823
let sync ~root_dir =
829
824
Sys. readdir root_dir
830
825
>> = fun names ->
@@ -833,11 +828,8 @@ let watch_datapath_plugins ~root_dir =
833
828
Deferred. all_ignore (List. map ~f: (Datapath_plugins. register root_dir) (diff needed got_already))
834
829
>> = fun () ->
835
830
Deferred. all_ignore (List. map ~f: (Datapath_plugins. unregister root_dir) (diff got_already needed)) in
836
- Async_inotify. create ~recursive: false ~watch_new_dirs: false root_dir
837
- >> = fun (watch , _ ) ->
838
831
sync ~root_dir
839
832
>> = fun () ->
840
- let pipe = Async_inotify. pipe watch in
841
833
let open Async_inotify.Event in
842
834
let rec loop () =
843
835
( Pipe. read pipe >> = function
@@ -865,14 +857,32 @@ let watch_datapath_plugins ~root_dir =
865
857
let main ~root_dir ~state_path ~switch_path =
866
858
Attached_SRs. reload state_path
867
859
>> = fun () ->
868
- Deferred. all_unit [
869
- watch_volume_plugins ~root_dir ~switch_path ;
870
- watch_datapath_plugins ~root_dir
871
- ]
860
+ let datapath_root = Filename. concat root_dir " datapath" in
861
+ Async_inotify. create ~recursive: false ~watch_new_dirs: false datapath_root
862
+ >> = fun (watch , _ ) ->
863
+ let datapath = Async_inotify. pipe watch in
864
+ let volume_root = Filename. concat root_dir " volume" in
865
+ Async_inotify. create ~recursive: false ~watch_new_dirs: false volume_root
866
+ >> = fun (watch , _ ) ->
867
+ let volume = Async_inotify. pipe watch in
872
868
873
- let main ~root_dir ~state_path ~switch_path =
874
- let (_: unit Deferred.t ) = main ~root_dir ~state_path ~switch_path in
875
- never_returns (Scheduler. go () )
869
+ let rec loop () =
870
+ Monitor. try_with
871
+ (fun () ->
872
+ Deferred. all_unit [
873
+ watch_volume_plugins ~root_dir: volume_root ~switch_path ~pipe: volume;
874
+ watch_datapath_plugins ~root_dir: datapath_root ~pipe: datapath
875
+ ]
876
+ )
877
+ >> = function
878
+ | Ok () ->
879
+ info " main thread shutdown cleanly" ;
880
+ return ()
881
+ | Error x ->
882
+ error " main thread failed with %s" (Exn. to_string x);
883
+ Clock. after (Time.Span. of_sec 5. ) >> = fun () ->
884
+ loop () in
885
+ loop ()
876
886
877
887
open Xcp_service
878
888
@@ -917,5 +927,20 @@ let _ =
917
927
use_syslog := true ;
918
928
info " Daemonisation successful." ;
919
929
end ;
920
- main ~root_dir: ! root_dir ~state_path: ! state_path ~switch_path: ! Xcp_client. switch_path
930
+ let (_: unit Deferred.t ) =
931
+ let rec loop () =
932
+ Monitor. try_with
933
+ (fun () ->
934
+ main ~root_dir: ! root_dir ~state_path: ! state_path ~switch_path: ! Xcp_client. switch_path
935
+ )
936
+ >> = function
937
+ | Ok () ->
938
+ info " main thread shutdown cleanly" ;
939
+ return ()
940
+ | Error x ->
941
+ error " main thread failed with %s" (Exn. to_string x);
942
+ Clock. after (Time.Span. of_sec 5. ) >> = fun () ->
943
+ loop () in
944
+ loop () in
945
+ never_returns (Scheduler. go () )
921
946
0 commit comments