From 90eaa31d3acea2c640d662323142776a3eab517c Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 17 Sep 2013 23:30:18 +0100 Subject: [PATCH] Run all periodic jobs scheduled at the same time in series. --- goaljobs.ml | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/goaljobs.ml b/goaljobs.ml index 4a66780..b5981ad 100644 --- a/goaljobs.ml +++ b/goaljobs.ml @@ -58,7 +58,7 @@ let periodic_jobs = ref [] (* Register a periodic job. *) let every ?name i (j, t) f = let period = i*j, t in (* 5 minutes -> ((5 * 60), Seconds) *) - periodic_jobs := (period, name, f) :: !periodic_jobs + periodic_jobs := (period, (name, f)) :: !periodic_jobs (* [next_time t period] returns the earliest event of [period] strictly after time [t]. @@ -514,20 +514,31 @@ Options: while true do (* Find the next job to run. *) let now = time () in - let next = List.map ( - fun (period, name, f) -> - next_time now period, name, f + let jobs = List.map ( + fun (period, (_, _ as name_f)) -> + next_time now period, name_f ) !periodic_jobs in - let next = List.sort (fun (t1,_,_) (t2,_,_) -> compare t1 t2) next in - let next_t, name, f = List.hd next in - - let name = match name with Some name -> name | None -> "[unnamed]" in - - (* Run it after waiting for the appropriate amount of time. *) - let seconds = int_of_float (next_t -. now) in - eprintf "next job '%s' will run in %s\n%!" name (printable_seconds seconds); + let jobs = List.sort (fun (t1,_) (t2,_) -> compare t1 t2) jobs in + + (* Find all jobs that have the same next time. + * XXX When we can handle parallel jobs we can do better here, + * but until them run all the ones which have the same time + * in series. + *) + let next_t = int_of_float (fst (List.hd jobs)) in + let jobs = List.filter (fun (t, _) -> int_of_float t = next_t) jobs in + + (* Run next job(s) after waiting for the appropriate amount of time. *) + let seconds = next_t - int_of_float now in + eprintf "next job will run in %s\n%!" (printable_seconds seconds); sleep seconds; - ignore (guard f ()) + + List.iter ( + fun (_, (name, f)) -> + eprintf "running job: %s\n%!" + (match name with Some name -> name | None -> "[unnamed]"); + ignore (guard f ()) + ) jobs done and printable_seconds s = -- 1.8.3.1