X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fdaemon.ml;h=078aa3ab129b965a1efa961ba072434d5eedca79;hb=1d0b030e3a62a905dce16d05e8816cb3da8c49eb;hp=c04bfcbac06cce362b20ac81cace23d6c30ae2e0;hpb=77707acb12b5424488757569f376e1d9b58e5a22;p=whenjobs.git diff --git a/daemon/daemon.ml b/daemon/daemon.ml index c04bfcb..078aa3a 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -145,6 +145,7 @@ and proc_exit_daemon () = | Some s -> Rpc_server.stop_server ~graceful:true s; server := None; + Gc.compact (); (* force the server handle to get cleaned up now *) `ok and proc_get_jobs () = @@ -292,16 +293,40 @@ and proc_whisper_variables vars = (* Reload the jobs file(s). *) and reload_files () = - (* Get dir/*.cmo *) - let dir = !jobsdir in - let files = Array.to_list (Sys.readdir dir) in - let files = List.filter ( - fun file -> - let n = String.length file in - n >= 5 && String.sub file (n-4) 4 = ".cmo" - ) files in - let files = List.map (fun file -> dir // file) files in - let files = List.sort compare files in + (* 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 @@ -311,19 +336,24 @@ and reload_files () = Whenfile.init s; let s = - try - List.iter Dynlink.loadfile files; - let s = Whenfile.get_state () in - Syslog.notice "loaded %d job(s) from %d file(s)" - (Whenstate.nr_jobs s) (List.length files); + 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;