Use %global instead of %define in spec file (thanks Michael Scherer).
[whenjobs.git] / daemon / daemon.ml
index cf14188..bc4f51a 100644 (file)
@@ -83,6 +83,7 @@ let rec init j d =
       ~proc_get_job_names
       ~proc_test_variables
       ~proc_ping_daemon
+      ~proc_whisper_variables
       (Rpc_server.Unix addr)
       Rpc.Tcp (* not TCP, this is the same as SOCK_STREAM *)
       Rpc.Socket
@@ -98,7 +99,7 @@ let rec init j d =
 and proc_reload_file () =
   if !debug then Syslog.notice "remote call: reload_file";
 
-  try reload_file (); `ok
+  try reload_files (); `ok
   with Failure err -> `error err
 
 and proc_set_variable (name, value) =
@@ -259,9 +260,45 @@ and proc_test_variables vars =
 
 and proc_ping_daemon () = `ok
 
-(* Reload the jobs file. *)
-and reload_file () =
-  let file = sprintf "%s/jobs.cmo" !jobsdir in
+and proc_whisper_variables vars =
+  try
+    let vars = Array.map (
+      fun { Whenproto_aux.sv_name = name; sv_value = value } ->
+        name, variable_of_rpc value
+    ) vars in
+    let vars = Array.to_list vars in
+
+    if !debug then
+      Syslog.notice "remote call: whisper_variables (%s)"
+        (String.concat " "
+           (List.map (
+             fun (name, value) ->
+               sprintf "%s=%s" name (string_of_variable value)
+            ) vars));
+
+    List.iter (fun (name, _) -> check_valid_variable_name name) vars;
+
+    (* Update all the variables atomically. *)
+    let s = List.fold_left (
+      fun s (name, value) -> Whenstate.set_variable s name value
+    ) !state vars in
+    state := s;
+
+    (* .. but don't reevaluate or run jobs. *)
+
+    `ok
+  with
+    Failure msg -> `error msg
+
+(* Reload the jobs file(s). *)
+and reload_files () =
+  (* Get dir/*.cmo (bytecode) or dir/*.cmxs (native code) *)
+  let suffix = if not Dynlink.is_native then ".cmo" else ".cmxs" in
+  let dir = !jobsdir in
+  let files = Array.to_list (Sys.readdir dir) in
+  let files = List.filter (fun file -> string_endswith file suffix) files in
+  let files = List.map (fun file -> dir // file) files in
+  let files = List.sort compare files in
 
   (* As we are reloading the file, we want to create a new state
    * that has no jobs, but has all the variables from the previous
@@ -272,9 +309,10 @@ and reload_file () =
 
   let s =
     try
-      Dynlink.loadfile file;
+      List.iter Dynlink.loadfile files;
       let s = Whenfile.get_state () in
-      Syslog.notice "loaded %d job(s) from %s" (Whenstate.nr_jobs s) file;
+      Syslog.notice "loaded %d job(s) from %d file(s)"
+        (Whenstate.nr_jobs s) (List.length files);
       s
     with
     | Dynlink.Error err ->