2 * Copyright (C) 2012 Red Hat Inc.
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.
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.
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.
24 (* All jobs that are loaded. Maps name -> [job] structure. *)
25 let jobs = ref StringMap.empty
27 (* Map variable names to jobs which depend on that variable. This
28 * gives us a quick way to tell which jobs might need to be reevaluated
29 * when a variable is set.
31 let dependencies = ref StringMap.empty
33 (* Current values of variables. Using the referentially transparent
34 * type Map is very useful here because it lets us cheaply keep
35 * previous values of variables.
37 let variables : variables ref = ref StringMap.empty
39 (* Last time that an every job ran. See schedule_next_everyjob below. *)
40 let last_t = ref (time ())
45 (* Was debugging requested on the command line? *)
48 let esys = Unixqueue.standard_event_system ()
49 let timer_group = ref None
55 Whenlock.create_lock !jobsdir;
57 (* Remove old socket if it exists. *)
58 let addr = sprintf "%s/socket" !jobsdir in
59 (try unlink addr with Unix_error _ -> ());
62 Whenproto_srv.When.V1.create_server
66 ~proc_get_variable_names
67 (Rpc_server.Unix addr)
68 Rpc.Tcp (* not TCP, this is the same as SOCK_STREAM *)
73 and proc_reload_file () =
74 if !debug then Syslog.notice "remote call: reload_file";
76 try reload_file (); `ok
77 with Failure err -> `error err
79 and proc_set_variable (name, value) =
80 if !debug then Syslog.notice "remote call: set_variable %s" name;
82 let value = variable_of_rpc value in
83 variables := StringMap.add name value !variables;
85 (* Which jobs need to be re-evaluated? *)
86 let jobnames = try StringMap.find name !dependencies with Not_found -> [] in
87 reevaluate_whenjobs jobnames
89 and proc_get_variable name =
90 if !debug then Syslog.notice "remote call: get_variable %s" name;
92 try rpc_of_variable (StringMap.find name !variables)
93 with (* all non-existent variables are empty strings *)
94 Not_found -> `string_t ""
96 and proc_get_variable_names () =
97 if !debug then Syslog.notice "remote call: get_variable_names";
99 (* Only return variables that are non-empty. *)
100 let vars = StringMap.fold (
101 fun name value xs -> if value <> T_string "" then name :: xs else xs
103 let vars = Array.of_list vars in
104 Array.sort compare vars;
107 (* Reload the jobs file. *)
109 let file = sprintf "%s/jobs.cmo" !jobsdir in
114 Dynlink.loadfile file;
115 let jobs = Whenfile.get_jobs () in
116 Syslog.notice "loaded %d job(s) from %s" (List.length jobs) file;
119 | Dynlink.Error err ->
120 let err = Dynlink.error_message err in
121 Syslog.error "error loading jobs: %s" err;
124 failwith (Printexc.to_string exn) in
126 (* Set 'jobs' and related global variables. *)
128 let map = List.fold_left (
130 let name = j.job_name in
131 StringMap.add name j map
132 ) StringMap.empty js in
136 let map = List.fold_left (
138 let deps = dependencies_of_job j in
139 let name = j.job_name in
142 let names = try StringMap.find d map with Not_found -> [] in
143 StringMap.add d (name :: names) map
145 ) StringMap.empty js in
146 dependencies := map in
148 (* Re-evaluate all when jobs. *)
149 reevaluate_whenjobs (StringMap.keys !jobs);
151 (* Schedule the next every job to run. *)
153 schedule_next_everyjob ()
155 (* Re-evaluate each named when-statement job, in a loop until we reach
156 * a fixpoint. Run those that need to be run. every-statement jobs
159 and reevaluate_whenjobs jobnames =
160 let rec loop set jobnames =
165 try StringMap.find jobname !jobs
166 with Not_found -> assert false in
167 assert (jobname = job.job_name);
169 let r, job' = job_evaluate job !variables in
170 jobs := StringMap.add jobname job' !jobs;
173 Syslog.notice "evaluate %s -> %b\n" jobname r;
175 if r then StringSet.add jobname set else set
177 if StringSet.compare set set' <> 0 then
182 let set = loop StringSet.empty jobnames in
183 let jobnames = StringSet.elements set in
185 (List.map (fun jobname -> StringMap.find jobname !jobs) jobnames)
187 (* Schedule the next every-statement job to run, if there is one. We
188 * look at the every jobs, work out the time that each must run at,
189 * pick the job(s) which must run soonest, and schedule a timer to run
190 * them. When the timer fires, it runs those jobs, then call this
191 * function again. 'last_t' is the base time used for scheduling (or
192 * the time that the file was last reloaded).
194 and schedule_next_everyjob () =
195 (* Get only everyjobs. *)
196 let jobs = StringMap.values !jobs in
197 let jobs = filter_map (
199 | { job_cond = Every_job period } as job -> Some (job, period)
200 | { job_cond = When_job _ } -> None
203 (* Map everyjob to next time it must run. *)
204 let jobs = List.map (
206 let t' = next_periodexpr !last_t period in
207 assert (t' > !last_t); (* serious bug in next_periodexpr if false *)
211 (* Sort, soonest first. *)
212 let jobs = List.sort (fun (_,a) (_,b) -> compare a b) jobs in
217 Syslog.notice "%s: next scheduled run at %s"
218 job.job_name (string_of_time_t t)
222 (* Pick the job(s) which run soonest. *)
223 let rec pick = function
226 | (j1, t) :: (j2, t') :: _ when t < t' -> t, [j1]
227 | (j1, t) :: (((j2, t') :: _) as rest) -> t, (j1 :: snd (pick rest))
229 let t, jobs = pick jobs in
236 Syslog.notice "scheduling job(s) %s to run at %s"
237 (String.concat ", " (List.map (fun { job_name = name } -> name) jobs))
238 (string_of_time_t t);
240 (* Schedule them to run at time t. *)
241 let g = new_timer_group () in
242 let w = Unixqueue.new_wait_id esys in
243 let t_diff = t -. Unix.time () in
244 let t_diff = if t_diff < 0. then 0. else t_diff in
245 Unixqueue.add_resource esys g (Unixqueue.Wait w, t_diff);
247 List.iter run_job jobs;
249 schedule_next_everyjob ();
251 Unixqueue.add_handler esys g run_jobs;
255 and string_of_time_t t =
257 sprintf "%04d-%02d-%02d %02d:%02d:%02d UTC"
258 (1900+tm.tm_year) (1+tm.tm_mon) tm.tm_mday
259 tm.tm_hour tm.tm_min tm.tm_sec
261 and new_timer_group () =
263 let g = Unixqueue.new_group esys in
264 timer_group := Some g;
267 and delete_timer () =
268 match !timer_group with
271 Unixqueue.clear esys g;
275 Syslog.notice "running %s" job.job_name;