X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=lib%2Fwhentools.ml;h=569421df37d9828d1be1ec1552e0d21ec0941c7b;hp=979124f7aa243efd085f96878e3503788c2cee14;hb=HEAD;hpb=f56882eb71425b2100edea0cabb478457a8a6a16 diff --git a/lib/whentools.ml b/lib/whentools.ml index 979124f..569421d 100644 --- a/lib/whentools.ml +++ b/lib/whentools.ml @@ -16,9 +16,11 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) -open Big_int open Whenexpr +open Big_int +open Printf + let set_variable name value = check_valid_variable_name name; Whenfile.set_variable name (T_string value) @@ -36,3 +38,48 @@ let set_variable_string = set_variable 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 = + if result.res_code <> 0 || not only_on_failure then ( + let subject = + sprintf "%s: %s (return code %d)" + result.res_job_name + (if result.res_code = 0 then "successful" else "FAILED") + result.res_code in + + let cmd = sprintf "%s -s %s -a %s" + Config.mailx + (Filename.quote subject) + (Filename.quote result.res_output) in + + let cmd = + match from with + | None -> cmd + | Some from -> sprintf "%s -r %s" cmd from in + + let cmd = + sprintf "%s %s 0 then + failwith "Whentools.mailto: mailx command failed"; + )