Implement pre functions.
[whenjobs.git] / lib / whentools.ml
index cac698a..569421d 100644 (file)
@@ -39,6 +39,24 @@ let set_variable_float name value =
   check_valid_variable_name name;
   Whenfile.set_variable name (T_float value)
 
+type preinfo = Whenexpr.preinfo
+
+let max n preinfo =
+  let name = preinfo.pi_job_name in
+
+  (* Count running jobs with the same name. *)
+  let count = List.fold_left (
+    fun count ->
+      function
+      | { pirun_job_name = n } when n = name -> count + 1
+      | _ -> count
+  ) 0 preinfo.pi_running in
+
+  (* Only let this job run if there are fewer than n already running. *)
+  count < n
+
+let one () preinfo = max 1 preinfo
+
 type result = Whenexpr.result
 
 let mailto ?(only_on_failure = false) ?from email result =