From 82b61d8519571f6a3feadc8575958945292c6d9f Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 21 Feb 2012 14:47:00 +0000 Subject: [PATCH] Add a $JOBSERIAL environment variable when jobs run. This serial number counts up sequentially, allowing us to test parallel jobs more easily. It is implemented as an ordinary variable, so you can write conditions against it. --- daemon/daemon.ml | 21 +++++++++++++++++++-- tests/jobs/t100_counter.ml | 4 ++-- tests/jobs/t100_counter.ml.expected | 8 ++++---- tests/jobs/t101_updown.ml | 8 ++++---- tests/jobs/t101_updown.ml.expected | 20 ++++++++++---------- tests/jobs/test_run.sh | 4 ++++ 6 files changed, 43 insertions(+), 22 deletions(-) diff --git a/daemon/daemon.ml b/daemon/daemon.ml index a6cc2fa..a8bd514 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -18,6 +18,7 @@ open Whenutils +open Big_int open Unix open Printf @@ -80,7 +81,12 @@ let rec init j d = ); (* Handle SIGCHLD to clean up jobs. *) - Sys.set_signal Sys.sigchld (Sys.Signal_handle handle_sigchld) + Sys.set_signal Sys.sigchld (Sys.Signal_handle handle_sigchld); + + (* Initialize the variables. XXX Eventually this will be saved + * and loaded from a persistent store. + *) + variables := StringMap.add "JOBSERIAL" (T_int zero_big_int) !variables and proc_reload_file () = if !debug then Syslog.notice "remote call: reload_file"; @@ -291,7 +297,18 @@ and string_of_time_t t = tm.tm_hour tm.tm_min tm.tm_sec and run_job job = - Syslog.notice "running %s" job.job_name; + let () = + (* Increment JOBSERIAL. *) + let serial = + match StringMap.find "JOBSERIAL" !variables with + | T_int serial -> + let serial = succ_big_int serial in + variables := StringMap.add "JOBSERIAL" (T_int serial) !variables; + serial + | _ -> assert false in + + Syslog.notice "running %s (JOBSERIAL=%s)" + job.job_name (string_of_big_int serial) in (* Create a temporary directory. The current directory of the job * will be in this directory. The directory is removed when the diff --git a/tests/jobs/t100_counter.ml b/tests/jobs/t100_counter.ml index d25f6c7..fd264f1 100644 --- a/tests/jobs/t100_counter.ml +++ b/tests/jobs/t100_counter.ml @@ -20,12 +20,12 @@ every 2 seconds : << - echo $JOBNAME $counter >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME $counter >\> $HOME/test_output whenjobs --set counter $(($counter+1)) --type int >> when counter = 3 : << - echo $JOBNAME $counter >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME $counter >\> $HOME/test_output whenjobs --daemon-stop >> diff --git a/tests/jobs/t100_counter.ml.expected b/tests/jobs/t100_counter.ml.expected index 48c981e..a8c6141 100644 --- a/tests/jobs/t100_counter.ml.expected +++ b/tests/jobs/t100_counter.ml.expected @@ -1,4 +1,4 @@ -job$1 -job$1 1 -job$1 2 -job$2 3 +1 job$1 +2 job$1 1 +3 job$1 2 +4 job$2 3 diff --git a/tests/jobs/t101_updown.ml b/tests/jobs/t101_updown.ml index 75695ce..af9425d 100644 --- a/tests/jobs/t101_updown.ml +++ b/tests/jobs/t101_updown.ml @@ -22,22 +22,22 @@ every second : << - echo $JOBNAME >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME >\> $HOME/test_output whenjobs --set counter $(($counter+1)) --type int >> when counter < 5 && counter mod 2 == 0 : << - echo $JOBNAME >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME >\> $HOME/test_output >> when counter < 5 && counter mod 2 == 1 : << - echo $JOBNAME >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME >\> $HOME/test_output >> when counter == 5 : << - echo $JOBNAME >\> $HOME/test_output + echo $JOBSERIAL $JOBNAME >\> $HOME/test_output whenjobs --daemon-stop >> diff --git a/tests/jobs/t101_updown.ml.expected b/tests/jobs/t101_updown.ml.expected index ce4c74e..277fd32 100644 --- a/tests/jobs/t101_updown.ml.expected +++ b/tests/jobs/t101_updown.ml.expected @@ -1,10 +1,10 @@ -job$1 -job$3 -job$1 -job$2 -job$1 -job$3 -job$1 -job$2 -job$1 -job$4 +1 job$1 +2 job$3 +3 job$1 +4 job$2 +5 job$1 +6 job$3 +7 job$1 +8 job$2 +9 job$1 +10 job$4 diff --git a/tests/jobs/test_run.sh b/tests/jobs/test_run.sh index 13cd844..ad2bcde 100755 --- a/tests/jobs/test_run.sh +++ b/tests/jobs/test_run.sh @@ -60,6 +60,10 @@ done trap - INT TERM QUIT EXIT rm -rf "$testdir/.whenjobs" +# Sort the output by job serial number, so that jobs that happened to +# fire off in parallel are still recorded in order. +sort -n -o "$testdir/test_output" "$testdir/test_output" + # Check the test output matches the expected output. if ! cmp -s "$testdir/test_output" "$1.expected"; then echo "$0: $1: test output did not match expected output" -- 1.8.3.1