Scheduling of every-jobs.
[whenjobs.git] / lib / whenutils.mli
1 (* whenjobs
2  * Copyright (C) 2012 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 (** Types and utility functions. *)
20
21 module StringMap : sig
22   type key = String.t
23   type 'a t = 'a Map.Make(String).t
24   val empty : 'a t
25   val is_empty : 'a t -> bool
26   val mem : key -> 'a t -> bool
27   val add : key -> 'a -> 'a t -> 'a t
28   (*val singleton : key -> 'a -> 'a t*)
29   val remove : key -> 'a t -> 'a t
30   (*val merge :
31     (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t*)
32   val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
33   val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
34   val iter : (key -> 'a -> unit) -> 'a t -> unit
35   val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
36   (*val for_all : (key -> 'a -> bool) -> 'a t -> bool
37   val exists : (key -> 'a -> bool) -> 'a t -> bool
38   val filter : (key -> 'a -> bool) -> 'a t -> 'a t
39   val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
40   val cardinal : 'a t -> int
41   val bindings : 'a t -> (key * 'a) list
42   val min_binding : 'a t -> key * 'a
43   val max_binding : 'a t -> key * 'a
44   val choose : 'a t -> key * 'a
45   val split : key -> 'a t -> 'a t * 'a option * 'a t*)
46   val find : key -> 'a t -> 'a
47   val map : ('a -> 'b) -> 'a t -> 'b t
48   val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
49   val keys : 'a t -> key list
50   val values : 'a t -> 'a list
51 end
52 (** A map from string to any type. *)
53
54 module StringSet : sig
55   type elt = String.t
56   type t = Set.Make(String).t
57   val empty : t
58   val is_empty : t -> bool
59   val mem : elt -> t -> bool
60   val add : elt -> t -> t
61   val singleton : elt -> t
62   val remove : elt -> t -> t
63   val union : t -> t -> t
64   val inter : t -> t -> t
65   val diff : t -> t -> t
66   val compare : t -> t -> int
67   val equal : t -> t -> bool
68   val subset : t -> t -> bool
69   val iter : (elt -> unit) -> t -> unit
70   val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
71   val for_all : (elt -> bool) -> t -> bool
72   val exists : (elt -> bool) -> t -> bool
73   val filter : (elt -> bool) -> t -> t
74   val partition : (elt -> bool) -> t -> t * t
75   val cardinal : t -> int
76   val elements : t -> elt list
77   val min_elt : t -> elt
78   val max_elt : t -> elt
79   val choose : t -> elt
80   val split : elt -> t -> t * bool * t
81 end
82 (** A set of strings. *)
83
84 val filter_map : ('a -> 'b option) -> 'a list -> 'b list
85 (** Filter + map. *)
86
87 type whenexpr =
88   | Expr_bool of bool                   (** A boolean constant. *)
89   | Expr_str of string                  (** A string constant. *)
90   | Expr_int of Big_int.big_int         (** An integer constant. *)
91   | Expr_float of float                 (** A float constant. *)
92   | Expr_var of string                  (** A variable name. *)
93   | Expr_and of whenexpr * whenexpr     (** && *)
94   | Expr_or of whenexpr * whenexpr      (** || *)
95   | Expr_eq of whenexpr * whenexpr      (** == *)
96   | Expr_not of whenexpr                (** ! *)
97   | Expr_changes of string              (** changes var *)
98 (** Internal type used to represent 'when' expressions. *)
99
100 type periodexpr =
101   | Every_seconds of int
102   | Every_days of int
103   | Every_months of int
104   | Every_years of int
105 (** Internal type used to represent 'every' expressions. *)
106
107 type shell_script = {
108   sh_loc : Camlp4.PreCast.Loc.t;
109   sh_script : string;
110 }
111 (** A shell script. *)
112
113 type variable =
114   | T_bool of bool
115   | T_string of string
116   | T_int of Big_int.big_int
117   | T_float of float
118 (** Typed variable (see also [whenproto.x]) *)
119
120 val variable_of_rpc : Whenproto_aux.variable -> variable
121 val rpc_of_variable : variable -> Whenproto_aux.variable
122
123 type variables = variable StringMap.t
124 (** A set of variables. *)
125
126 type job_private
127 (** Private state associated with a job, used for evaluation. *)
128
129 val no_job_private : job_private
130 (* XXX any use of no_job_private is wrong XXX *)
131
132 type job_cond =
133   | When_job of whenexpr                (** when ... : << >> *)
134   | Every_job of periodexpr             (** every ... : << >> *)
135
136 type job = {
137   job_loc : Camlp4.PreCast.Loc.t;
138   job_name : string;
139   job_cond : job_cond;
140   job_script : shell_script;
141   job_private : job_private;
142 }
143 (** A job. *)
144
145 val expr_of_ast : Camlp4.PreCast.Ast.Loc.t -> Camlp4.PreCast.Ast.expr -> whenexpr
146 (** Convert OCaml AST to an expression.  Since OCaml ASTs are much
147     more general than the expressions we can use, this can raise
148     [Invalid_argument] in many different situations. *)
149
150 val string_of_whenexpr : whenexpr -> string
151 (** Pretty-print an expression to a string. *)
152
153 val string_of_periodexpr : periodexpr -> string
154 (** Pretty-print a period expression to a string. *)
155
156 val dependencies_of_whenexpr : whenexpr -> string list
157 (** Return list of variables that an expression depends on.  This is
158     used to work out when an expression needs to be reevaluated. *)
159
160 val dependencies_of_job : job -> string list
161 (** Which variables does this job depend on? *)
162
163 val job_evaluate : job -> variables -> bool * job
164 (** Evaluate [job]'s condition in the context of the [variables], and
165     return [true] iff it should be run now.  Note that this returns a
166     possibly-updated [job] structure.
167
168     This is a no-op for 'every' jobs. *)
169
170 val next_periodexpr : float -> periodexpr -> float
171 (** [next_periodexpr t period] returns the earliest event of [period]
172     strictly after time [t].
173
174     Visualising periods as repeated events on a timeline, this
175     returns [t']:
176
177     {v
178     events:  ---+---------+---------+---------+---------+---------+-----
179     times:          t     t'
180     }
181
182     Note that [periodexpr] events are not necessarily regular.
183     eg. The start of a month is not a fixed number of seconds
184     after the start of the previous month.  'Epoch' refers
185     to the Unix Epoch (ie. 1970-01-01 00:00:00 UTC).
186
187     If [period = Every_seconds i] then events are when
188     [t' mod i == 0] when t' is the number of seconds since
189     the Epoch.  This returns the next t' > t.
190
191     If [period = Every_days i] then events happen at
192     midnight UTC every [i] days since the Epoch.
193     This returns the next midnight > t.
194
195     If [period = Every_months i] then events happen at
196     midnight UTC on the 1st day of the month every [i] months
197     since the Epoch.  This returns midnight on the
198     1st day of the next month > t.
199
200     If [period = Every_years i] then events happen at
201     midnight UTC on the 1st day of the year when
202     [(y - 1970) mod i == 0].  This returns midnight on the
203     1st day of the next year > t. *)