From cffe87109d9c868eefd48f7d5255a4863a578e4d Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 23 Feb 2012 20:28:25 +0000 Subject: [PATCH] Pass JOBSERIAL through to the cleanup function. --- daemon/daemon.ml | 36 ++++++++++++++++++------------------ lib/whenexpr.ml | 1 + lib/whenexpr.mli | 1 + tools/whenjobs.pod | 1 + 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/daemon/daemon.ml b/daemon/daemon.ml index 9e972ac..42c32de 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -32,8 +32,8 @@ let jobsdir = ref "" (* The state. *) let state = ref Whenstate.empty -(* Jobs that are running; map of PID -> (job, other data). Note that - * the job may no longer exist *OR* it may have been renamed, +(* Jobs that are running: a map of PID -> (job, tmpdir, serial). + * Note that the job may no longer exist *OR* it may have been renamed, * eg. if the jobs file was reloaded. *) let running = ref IntMap.empty @@ -292,18 +292,17 @@ and string_of_time_t t = tm.tm_hour tm.tm_min tm.tm_sec and run_job job = - let () = - (* Increment JOBSERIAL. *) - let serial = - 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 - | _ -> assert false in - - Syslog.notice "running %s (JOBSERIAL=%s)" - job.job_name (string_of_big_int serial) in + (* Increment JOBSERIAL. *) + let serial = + 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 + | _ -> assert false in + + Syslog.notice "running %s (JOBSERIAL=%s)" + job.job_name (string_of_big_int serial); (* Create a temporary directory. The current directory of the job * will be in this directory. The directory is removed when the @@ -351,7 +350,7 @@ and run_job job = (* Remember this PID, the job and the temporary directory, so we * can clean up when the child exits. *) - running := IntMap.add pid (job, dir) !running + running := IntMap.add pid (job, dir, serial) !running and tmpdir () = let chan = open_in "/dev/urandom" in @@ -369,13 +368,13 @@ and handle_sigchld _ = let pid, status = waitpid [WNOHANG] 0 in if pid > 0 then ( (* Look up the PID in the running jobs map. *) - let job, dir = IntMap.find pid !running in + let job, dir, serial = IntMap.find pid !running in running := IntMap.remove pid !running; - cleanup_job job dir status + cleanup_job job dir serial status ) with Unix_error _ | Not_found -> () -and cleanup_job job dir status = +and cleanup_job job dir serial status = (* If there is a cleanup function, run it. *) (match job.job_cleanup with | None -> () @@ -386,6 +385,7 @@ and cleanup_job job dir status = | WSIGNALED s | WSTOPPED s -> 1 in let result = { res_job_name = job.job_name; + res_serial = serial; res_code = code; res_tmpdir = dir; res_output = dir // "output.txt" diff --git a/lib/whenexpr.ml b/lib/whenexpr.ml index f1f2a89..df1665b 100644 --- a/lib/whenexpr.ml +++ b/lib/whenexpr.ml @@ -85,6 +85,7 @@ type shell_script = { type result = { res_job_name : string; + res_serial : Big_int.big_int; res_code : int; res_tmpdir : string; res_output : string; diff --git a/lib/whenexpr.mli b/lib/whenexpr.mli index 437192f..f1b8e2e 100644 --- a/lib/whenexpr.mli +++ b/lib/whenexpr.mli @@ -62,6 +62,7 @@ type shell_script = { type result = { res_job_name : string; (** Job name. *) + res_serial : Big_int.big_int; (** Job serial number. *) res_code : int; (** Return code from the script. *) res_tmpdir : string; (** Temporary directory. *) res_output : string; (** Filename of output from job. *) diff --git a/tools/whenjobs.pod b/tools/whenjobs.pod index fd0a6e7..1c96648 100644 --- a/tools/whenjobs.pod +++ b/tools/whenjobs.pod @@ -665,6 +665,7 @@ fields: type result = { res_job_name : string; # job name + res_serial : big_int; # job serial (same as $JOBSERIAL) res_code : int; # return code from the shell script res_tmpdir : string; # temporary directory script ran in res_output : string; # filename of stdout/stderr output -- 1.8.3.1