From: Richard W.M. Jones Date: Sat, 10 Mar 2012 12:45:20 +0000 (+0000) Subject: Pass the state through reevaluate_whenjobs and run_job. X-Git-Tag: 0.6~6 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=6c41dfae8a0d60cab2fd3b82f73b762972496c2c;p=whenjobs.git Pass the state through reevaluate_whenjobs and run_job. --- diff --git a/daemon/daemon.ml b/daemon/daemon.ml index a4993a8..d9393e7 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -110,7 +110,8 @@ and proc_set_variable (name, value) = (* Which jobs need to be re-evaluated? *) let jobs = Whenstate.get_dependencies !state [name] in - reevaluate_whenjobs jobs; + let state' = reevaluate_whenjobs !state jobs in + state := state'; `ok with @@ -164,7 +165,8 @@ and proc_cancel_job serial = and proc_start_job jobname = try let job = Whenstate.get_job !state jobname in - run_job job; + let state' = run_job !state job in + state := state'; `ok with | Not_found -> `error "job not found" @@ -208,7 +210,8 @@ and proc_set_variables vars = (* Which jobs need to be re-evaluated? *) let jobs = Whenstate.get_dependencies !state (List.map fst vars) in - reevaluate_whenjobs jobs; + let state' = reevaluate_whenjobs !state jobs in + state := state'; `ok with @@ -246,46 +249,48 @@ and reload_file () = state := s; (* Re-evaluate all when jobs. *) - reevaluate_whenjobs ~onload:true (Whenstate.get_whenjobs !state); + let jobs = Whenstate.get_whenjobs !state in + let state' = reevaluate_whenjobs ~onload:true !state jobs 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 those that need to be run. + * a fixpoint. Run the jobs and return the updated state. *) -and reevaluate_whenjobs ?onload jobs = - let rec loop set jobs = - let set' = +and reevaluate_whenjobs ?onload state jobs = + let rec loop (set, state) jobs = + let set', state' = List.fold_left ( - fun set job -> + fun (set, state) job -> let r, state' = - try Whenstate.evaluate_whenjob ?onload !state job + try Whenstate.evaluate_whenjob ?onload state job with Invalid_argument err | Failure err -> Syslog.error "error evaluating job %s (at %s): %s" job.job_name (Camlp4.PreCast.Ast.Loc.to_string job.job_loc) err; - false, !state in - - state := state'; + false, state in if !debug then Syslog.notice "evaluate %s -> %b\n" job.job_name r; - if r then StringSet.add job.job_name set else set - ) set jobs in + (if r then StringSet.add job.job_name set else set), state' + ) (set, state) jobs in + (* reached a fixpoint? *) if StringSet.compare set set' <> 0 then - loop set' jobs + loop (set', state') jobs else - set' + (set', state') in - let set = loop StringSet.empty jobs in + let set, state = loop (StringSet.empty, state) jobs in let jobnames = StringSet.elements set in (* Ensure the jobs always run in predictable (name) order. *) let jobnames = List.sort compare_jobnames jobnames in (* Run the jobs. *) - List.iter run_job (List.map (Whenstate.get_job !state) jobnames) + let jobs = List.map (Whenstate.get_job state) jobnames in + List.fold_left run_job state jobs (* Schedule the next every-statement job to run, if there is one. We * look at the every jobs, work out the time that each must run at, @@ -350,7 +355,8 @@ and schedule_next_everyjob () = let t_diff = if t_diff < 0. then 0. else t_diff in let run_jobs () = delete_timer_group (); (* Delete the timer. *) - List.iter run_job jobs; + let state' = List.fold_left run_job !state jobs in + state := state'; schedule_next_everyjob () in Unixqueue.weak_once esys g t_diff run_jobs; @@ -370,14 +376,14 @@ and delete_timer_group () = Unixqueue.clear esys g; timer_group := None -and run_job job = +and run_job state job = (* Increment JOBSERIAL. *) - let serial = - match Whenstate.get_variable !state "JOBSERIAL" with + let serial, state = + match Whenstate.get_variable state "JOBSERIAL" with | T_int serial -> let serial = succ_big_int serial in - state := Whenstate.set_variable !state "JOBSERIAL" (T_int serial); - serial + let state' = Whenstate.set_variable state "JOBSERIAL" (T_int serial) in + serial, state' | _ -> assert false in (* Call the pre-condition script. Note this may decide not to run @@ -399,7 +405,7 @@ and run_job job = let preinfo = { pi_job_name = job.job_name; pi_serial = serial; - pi_variables = Whenstate.get_variables !state; + pi_variables = Whenstate.get_variables state; pi_running = !rs; } in pre preinfo @@ -421,7 +427,7 @@ and run_job job = (* Set environment variables corresponding to each variable. *) List.iter (fun (name, value) -> putenv name (string_of_variable value)) - (Whenstate.get_variables !state); + (Whenstate.get_variables state); (* Set the $JOBNAME environment variable. *) putenv "JOBNAME" job.job_name; @@ -455,11 +461,15 @@ and run_job job = * can clean up when the child exits. *) runningmap := IntMap.add pid (job, dir, serial, time ()) !runningmap; - serialmap := BigIntMap.add serial pid !serialmap + serialmap := BigIntMap.add serial pid !serialmap; + + state ) else ( Syslog.notice "not running %s (JOBSERIAL=%s) because pre() condition returned false" job.job_name (string_of_big_int serial); + + state ) and tmpdir () =