Rename 'cleanup' to 'post'.
[whenjobs.git] / lib / whenexpr.mli
index 3c05826..818c75d 100644 (file)
@@ -39,6 +39,7 @@ type whenexpr =
   | Expr_mul of whenexpr * whenexpr     (** arithmetic multiplication *)
   | Expr_div of whenexpr * whenexpr     (** arithmetic division *)
   | Expr_mod of whenexpr * whenexpr     (** arithmetic modulo *)
+  | Expr_len of whenexpr                (** length *)
   | Expr_changes of string              (** changes var *)
   | Expr_increases of string            (** increases var *)
   | Expr_decreases of string            (** decreases var *)
@@ -59,6 +60,26 @@ type shell_script = {
 }
 (** A shell script. *)
 
+type preinfo = {
+  pi_job_name : string;                 (** Job name. *)
+  pi_serial : Big_int.big_int;          (** Job serial number. *)
+}
+(** Information available to pre function before the job runs. *)
+
+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. *)
+  res_start_time : float;               (** When the job started. *)
+}
+(** Result of the run of a job. *)
+
+type pre = preinfo -> bool
+type post = result -> unit
+(** Pre and post functions. *)
+
 type variable =
   | T_unit
   | T_bool of bool
@@ -75,9 +96,6 @@ val rpc_of_variable : variable -> Whenproto_aux.variable
 type variables = variable Whenutils.StringMap.t
 (** A set of variables. *)
 
-type job_private
-(** Private state associated with a job, used for evaluation. *)
-
 type job_cond =
   | When_job of whenexpr                (** when ... : << >> *)
   | Every_job of periodexpr             (** every ... : << >> *)
@@ -85,19 +103,12 @@ type job_cond =
 type job = {
   job_loc : Camlp4.PreCast.Loc.t;
   job_name : string;
+  job_pre : pre option;
+  job_post : post option;
   job_cond : job_cond;
   job_script : shell_script;
-  job_private : job_private;
 }
-(** A job.  Note that because of the [job_private] field, these cannot
-    be constructed directly.  Use {!make_when_job} or {!make_every_job}
-    to construct one. *)
-
-val make_when_job : Camlp4.PreCast.Loc.t -> string -> whenexpr -> shell_script -> job
-(** Make a when-statement job. *)
-
-val make_every_job : Camlp4.PreCast.Loc.t -> string -> periodexpr -> shell_script -> job
-(** Make an every-statement job. *)
+(** A job. *)
 
 val expr_of_ast : Camlp4.PreCast.Ast.Loc.t -> Camlp4.PreCast.Ast.expr -> whenexpr
 (** Convert OCaml AST to an expression.  Since OCaml ASTs are much
@@ -117,14 +128,26 @@ val dependencies_of_whenexpr : whenexpr -> string list
 val dependencies_of_job : job -> string list
 (** Which variables does this job depend on? *)
 
-val job_evaluate : job -> variables -> bool -> bool * job
-(** [job_evaluate job variables onload] evaluates [job]'s condition in
-    the context of the [variables], and return [true] iff it should be
-    run now.
+val eval_whenexpr : variables -> variables option -> bool -> whenexpr -> variable
+val eval_whenexpr_as_bool : variables -> variables option -> bool -> whenexpr -> bool
+(** [eval_whenexpr variables prev_variables onload expr] is the
+    evaluation function for when expressions.  It full evaluates
+    [expr], returning its typed value.  It can also throw exceptions
+    (at least [Invalid_argument] and [Failure]).
+
+    [eval_whenexpr_as_bool] is the same but it forces the returned
+    value to be a boolean.
+
+    The other parameters represent the current and past state:
 
-    Note that this returns a possibly-updated [job] structure.
+    [variables] is the current set of variables and their values.
 
-    This is a no-op for 'every' jobs. *)
+    [prev_variables] is the set of variables from the previous
+    run.  It is used to implement {i prev}, {i changes} etc operators.
+    This can be [None], meaning there is no previous state.
+
+    [onload] is used to implement the {i reloaded} operator.  It is
+    true if the file is being reloaded, and false otherwise. *)
 
 val next_periodexpr : float -> periodexpr -> float
 (** [next_periodexpr t period] returns the earliest event of [period]
@@ -160,3 +183,7 @@ val next_periodexpr : float -> periodexpr -> float
     midnight UTC on the 1st day of the year when
     [(y - 1970) mod i == 0].  This returns midnight on the
     1st day of the next year > t. *)
+
+val check_valid_variable_name : string -> unit
+(** Check that [name] is a valid variable name that users are permitted
+    to set, and raise [Failure] if it is not.  *)