From c8bff8ac923645d517183b130158d61f85540b33 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 10 Mar 2012 12:32:32 +0000 Subject: [PATCH] daemon: Implement 'test_variables'. This is the same as 'set_variables', but it doesn't update the state. It just returns the names of the jobs that *would* run if these variables were set. --- daemon/daemon.ml | 48 ++++++++++++++++++++++++++++++++++++++++++++---- lib/whenproto.x | 1 + 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/daemon/daemon.ml b/daemon/daemon.ml index d9393e7..5b9fc3b 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -81,6 +81,7 @@ let rec init j d = ~proc_get_job ~proc_set_variables ~proc_get_job_names + ~proc_test_variables (Rpc_server.Unix addr) Rpc.Tcp (* not TCP, this is the same as SOCK_STREAM *) Rpc.Socket @@ -110,7 +111,8 @@ and proc_set_variable (name, value) = (* Which jobs need to be re-evaluated? *) let jobs = Whenstate.get_dependencies !state [name] in - let state' = reevaluate_whenjobs !state jobs in + let jobnames, state' = reevaluate_whenjobs !state jobs in + let state' = run_whenjobs state' jobnames in state := state'; `ok @@ -210,7 +212,8 @@ and proc_set_variables vars = (* Which jobs need to be re-evaluated? *) let jobs = Whenstate.get_dependencies !state (List.map fst vars) in - let state' = reevaluate_whenjobs !state jobs in + let jobnames, state' = reevaluate_whenjobs !state jobs in + let state' = run_whenjobs state' jobnames in state := state'; `ok @@ -220,6 +223,39 @@ and proc_set_variables vars = and proc_get_job_names () = Array.of_list (Whenstate.get_job_names !state) +and proc_test_variables vars = + (* This is the same as proc_set_variables, except that it doesn't + * update the state, it just returns the jobs that *would* run if + * these variables were set to these values. + *) + 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: test_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 state = List.fold_left ( + fun s (name, value) -> Whenstate.set_variable s name value + ) !state vars in + + (* Which jobs WOULD be re-evaluated? *) + let jobs = Whenstate.get_dependencies state (List.map fst vars) in + let jobnames, _ = reevaluate_whenjobs state jobs in + + (* Return the names. *) + Array.of_list jobnames + (* Reload the jobs file. *) and reload_file () = let file = sprintf "%s/jobs.cmo" !jobsdir in @@ -250,14 +286,16 @@ and reload_file () = (* Re-evaluate all when jobs. *) let jobs = Whenstate.get_whenjobs !state in - let state' = reevaluate_whenjobs ~onload:true !state jobs in + let jobnames, state' = reevaluate_whenjobs ~onload:true !state jobs in + let state' = run_whenjobs state' jobnames in state := state'; (* Schedule the next every job to run. *) schedule_next_everyjob () (* Re-evaluate each when-statement job, in a loop until we reach - * a fixpoint. Run the jobs and return the updated state. + * a fixpoint. Return the list of job names that should run and + * the updated state. *) and reevaluate_whenjobs ?onload state jobs = let rec loop (set, state) jobs = @@ -287,7 +325,9 @@ and reevaluate_whenjobs ?onload state jobs = (* Ensure the jobs always run in predictable (name) order. *) let jobnames = List.sort compare_jobnames jobnames in + jobnames, state +and run_whenjobs state jobnames = (* Run the jobs. *) let jobs = List.map (Whenstate.get_job state) jobnames in List.fold_left run_job state jobs diff --git a/lib/whenproto.x b/lib/whenproto.x index 93173cf..1d8980a 100644 --- a/lib/whenproto.x +++ b/lib/whenproto.x @@ -101,5 +101,6 @@ program When { job get_job (string_big_int) = 9; status set_variables (set_variable_list) = 10; job_name_list get_job_names (void) = 11; + job_name_list test_variables (set_variable_list) = 12; } = 1; } = 0x20008081; -- 1.8.3.1