X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fdaemon.ml;h=bc4f51a2f1177e5be2692a233d7f2b850f9a55bc;hb=HEAD;hp=64d4012746771941379030df9a880be2d5951d1f;hpb=de72662854c3db9365296dd45cade2253910be7f;p=whenjobs.git diff --git a/daemon/daemon.ml b/daemon/daemon.ml index 64d4012..f5ba4ff 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -29,9 +29,41 @@ external _exit : int -> 'a = "whenjobs__exit" (* $HOME/.whenjobs *) let jobsdir = ref "" -(* The state. *) +(* The state. + * + * Note that whenever this is updated, you need to consider if you + * should call 'save_variables ()' (which persists the variables to a + * file). XXX We should replace this ref with an accessor + * function. + *) let state = ref Whenstate.empty +(* Format used to save variables. Note we can't allow any internal + * types to "escape" into this definition, else the file format will + * change when parts of the program change. + *) +type variables_file_v1 = (string * variable_v1) list +and variable_v1 = + | Vv1_unit + | Vv1_bool of bool + | Vv1_string of string + | Vv1_int of big_int + | Vv1_float of float + +let variable_of_variable_v1 = function + | Vv1_unit -> T_unit + | Vv1_bool b -> T_bool b + | Vv1_string s -> T_string s + | Vv1_int i -> T_int i + | Vv1_float f -> T_float f + +let variable_v1_of_variable = function + | T_unit -> Vv1_unit + | T_bool b -> Vv1_bool b + | T_string s -> Vv1_string s + | T_int i -> Vv1_int i + | T_float f -> Vv1_float f + (* Jobs that are running: a map of PID -> (job, tmpdir, serial, start_time). * Note that the job may no longer exist *OR* it may have been renamed, * eg. if the jobs file was reloaded. @@ -93,8 +125,48 @@ let rec init j d = (* Handle SIGCHLD to clean up jobs. *) Sys.set_signal Sys.sigchld (Sys.Signal_handle handle_sigchld); - (* Initialize the variables. *) - state := Whenstate.set_variable !state "JOBSERIAL" (T_int zero_big_int) + (* Load or initialize the variables. *) + let variables_file = sprintf "%s/variables" !jobsdir in + state := + try + let chan = open_in variables_file in + let r = load_variables !state chan in + close_in chan; + r + with + | Sys_error _ -> + Whenstate.set_variable !state "JOBSERIAL" (T_int zero_big_int) + +(* Try to load the variables from the file. If the file exists and + * cannot be read, raise an exception. + *) +and load_variables state chan = + let signature = input_line chan in + if signature = "WHENJOBS VARIABLES VERSION 1" then ( + let variables : variables_file_v1 = input_value chan in + List.fold_left ( + fun state (n, v) -> + Whenstate.set_variable state n (variable_of_variable_v1 v) + ) state variables + ) else (* in future, other signatures, but for now ... *) + failwith (sprintf "cannot read variables file: invalid signature: %s" + signature) + +and save_variables () = + let variables_file = sprintf "%s/variables" !jobsdir in + let new_file = variables_file ^ ".new" in + let chan = open_out new_file in + fprintf chan "WHENJOBS VARIABLES VERSION 1\n"; + let variables = Whenstate.get_variables !state in + let variables = + List.map (fun (n, v) -> n, variable_v1_of_variable v) variables in + output_value chan variables; + + (* Try to arrange that the new file is updated atomically. *) + flush chan; + Netsys_posix.fsync (descr_of_out_channel chan); + close_out chan; + rename new_file variables_file and proc_reload_file () = if !debug then Syslog.notice "remote call: reload_file"; @@ -116,6 +188,7 @@ and proc_set_variable (name, value) = let jobnames, state' = reevaluate_whenjobs !state jobs in let state' = run_whenjobs state' jobnames in state := state'; + save_variables (); `ok with @@ -145,6 +218,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 () = @@ -171,6 +245,7 @@ and proc_start_job jobname = let job = Whenstate.get_job !state jobname in let state' = run_job !state job in state := state'; + save_variables (); `ok with | Not_found -> `error "job not found" @@ -217,6 +292,7 @@ and proc_set_variables vars = let jobnames, state' = reevaluate_whenjobs !state jobs in let state' = run_whenjobs state' jobnames in state := state'; + save_variables (); `ok with @@ -283,6 +359,7 @@ and proc_whisper_variables vars = fun s (name, value) -> Whenstate.set_variable s name value ) !state vars in state := s; + save_variables (); (* .. but don't reevaluate or run jobs. *) @@ -309,7 +386,9 @@ and reload_files () = else ( let len = String.length file in let t = String.sub file 6 (len-slen-6) in - try Some (int_of_string t) with Failure "int_of_string" -> None + (* 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 @@ -319,11 +398,11 @@ and reload_files () = (* Unlink the older files. *) List.iter ( fun t -> - try unlink (dir // sprintf "jobs__%d%s" t suffix) + try unlink (dir // sprintf "jobs__%Ld%s" t suffix) with Unix_error _ -> () ) xs; (* Return the newest (highest numbered) file. *) - Some (dir // sprintf "jobs__%d%s" x suffix) in + 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 @@ -360,6 +439,7 @@ and reload_files () = let jobnames, state' = reevaluate_whenjobs ~onload:true !state jobs in let state' = run_whenjobs state' jobnames in state := state'; + save_variables (); (* Schedule the next every job to run. *) schedule_next_everyjob () @@ -468,6 +548,7 @@ and schedule_next_everyjob () = delete_timer_group (); (* Delete the timer. *) let state' = List.fold_left run_job !state jobs in state := state'; + save_variables (); schedule_next_everyjob () in Unixqueue.weak_once esys g t_diff run_jobs;