X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=daemon%2Fdaemon.ml;h=bbaab131a1270c40ce237a6f431e7995b9f9c4b0;hp=e18574d3bbab6bf5cf5ec346d3bb072305d5eaeb;hb=d6da1b74e241e79eb0af9c01e390e98ceead3a49;hpb=550bd0df9a6c9067dd0e2f9e77ae650efdf62454 diff --git a/daemon/daemon.ml b/daemon/daemon.ml index e18574d..bbaab13 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -99,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) = @@ -290,9 +290,42 @@ and proc_whisper_variables vars = with Failure msg -> `error msg -(* Reload the jobs file. *) -and reload_file () = - let file = sprintf "%s/jobs.cmo" !jobsdir in +(* Reload the jobs file(s). *) +and reload_files () = + (* Get the highest numbered dir/jobs__*.cmo (bytecode) or + * dir/jobs__*.cmxs (native code) file and load it. Delete + * lower-numbered (== older) files. + *) + let filename = + let suffix, slen = + if not Dynlink.is_native then ".cmo", 4 else ".cmxs", 5 in + let dir = !jobsdir in + let files = Array.to_list (Sys.readdir dir) in + let times = filter_map ( + fun file -> + if not (string_startswith file "jobs__") || + not (string_endswith file suffix) then + None + else ( + let len = String.length file in + let t = String.sub file 6 (len-slen-6) in + (* Use int64 because t won't necessarily fit into 31 bit int. *) + try Some (Int64.of_string t) + with Failure "int_of_string" -> assert false + ) + ) files in + let times = List.rev (List.sort compare times) in + match times with + | [] -> None + | x::xs -> + (* Unlink the older files. *) + List.iter ( + fun t -> + try unlink (dir // sprintf "jobs__%Ld%s" t suffix) + with Unix_error _ -> () + ) xs; + (* Return the newest (highest numbered) file. *) + Some (dir // sprintf "jobs__%Ld%s" x suffix) 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 @@ -302,18 +335,24 @@ and reload_file () = Whenfile.init s; let s = - try - Dynlink.loadfile file; - let s = Whenfile.get_state () in - Syslog.notice "loaded %d job(s) from %s" (Whenstate.nr_jobs s) file; + match filename with + | None -> + (* no jobs file, return the same state *) + Syslog.notice "no jobs file found"; s - with - | Dynlink.Error err -> - let err = Dynlink.error_message err in - Syslog.error "error loading jobs: %s" err; - failwith err - | exn -> - failwith (Printexc.to_string exn) in + | Some filename -> + try + Dynlink.loadfile filename; + let s = Whenfile.get_state () in + Syslog.notice "loaded %d job(s)" (Whenstate.nr_jobs s); + s + with + | Dynlink.Error err -> + let err = Dynlink.error_message err in + Syslog.error "error loading jobs: %s" err; + failwith err + | exn -> + failwith (Printexc.to_string exn) in let s = Whenstate.copy_prev_state !state s in state := s;