X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=daemon%2Fdaemon.ml;h=ed5e067f802558f4ba3b55bbe677b23119156d6d;hp=b38481cc894b2407bfaf05e080a6f74b03c9a0d9;hb=57d21a4a716b513d93790262db4a663c169814bd;hpb=ff70665ff06a3b13fb0a64e6f290321dd588d21d diff --git a/daemon/daemon.ml b/daemon/daemon.ml index b38481c..ed5e067 100644 --- a/daemon/daemon.ml +++ b/daemon/daemon.ml @@ -17,6 +17,7 @@ *) open Whenutils +open Whenexpr open Big_int open Unix @@ -25,29 +26,20 @@ open Printf (* See [exit.c]. *) external _exit : int -> 'a = "whenjobs__exit" -(* All jobs that are loaded. Maps name -> [job] structure. *) -let jobs = ref StringMap.empty - -(* Map variable names to jobs which depend on that variable. This - * gives us a quick way to tell which jobs might need to be reevaluated - * when a variable is set. - *) -let dependencies = ref StringMap.empty - -(* Current values of variables. Using the referentially transparent - * type Map is very useful here because it lets us cheaply keep - * previous values of variables. - *) -let variables : variables ref = ref StringMap.empty - (* $HOME/.whenjobs *) let jobsdir = ref "" -(* Jobs that are running; map of PID -> (job, other data). Note that - * the job may no longer exist *OR* it may have been renamed, +(* The state. *) +let state = ref Whenstate.empty + +(* Jobs that are running: a map of PID -> (job, tmpdir, serial, start_time). + * Note that the job may no longer exist *OR* it may have been renamed, * eg. if the jobs file was reloaded. *) -let running = ref IntMap.empty +let runningmap = ref IntMap.empty + +(* Serial numbers of running jobs. Map of serial -> PID (in runningmap). *) +let serialmap = ref BigIntMap.empty (* Was debugging requested on the command line? *) let debug = ref false @@ -75,6 +67,7 @@ let rec init j d = let addr = sprintf "%s/socket" !jobsdir in (try unlink addr with Unix_error _ -> ()); + (* Create the Unix domain socket server. *) server := Some ( Whenproto_srv.When.V1.create_server ~proc_reload_file @@ -82,6 +75,10 @@ let rec init j d = ~proc_get_variable ~proc_get_variable_names ~proc_exit_daemon + ~proc_get_jobs + ~proc_cancel_job + ~proc_start_job + ~proc_get_job (Rpc_server.Unix addr) Rpc.Tcp (* not TCP, this is the same as SOCK_STREAM *) Rpc.Socket @@ -91,10 +88,8 @@ let rec init j d = (* Handle SIGCHLD to clean up jobs. *) Sys.set_signal Sys.sigchld (Sys.Signal_handle handle_sigchld); - (* Initialize the variables. XXX Eventually this will be saved - * and loaded from a persistent store. - *) - variables := StringMap.add "JOBSERIAL" (T_int zero_big_int) !variables + (* Initialize the variables. *) + state := Whenstate.set_variable !state "JOBSERIAL" (T_int zero_big_int) and proc_reload_file () = if !debug then Syslog.notice "remote call: reload_file"; @@ -106,30 +101,14 @@ and proc_set_variable (name, value) = if !debug then Syslog.notice "remote call: set_variable %s" name; try - (* Don't permit certain names. *) - if name = "JOBSERIAL" then - failwith "JOBSERIAL variable cannot be set"; - - let len = String.length name in - if len = 0 then - failwith "variable name is an empty string"; - if name.[0] <> '_' && not (isalpha name.[0]) then - failwith "variable name must start with alphabetic character or underscore"; - - let rec loop i = - if i >= len then () - else if name.[i] <> '_' && not (isalnum name.[i]) then - failwith "variable name contains non-alphanumeric non-underscore character" - else loop (i+1) - in - loop 1; + check_valid_variable_name name; let value = variable_of_rpc value in - variables := StringMap.add name value !variables; + state := Whenstate.set_variable !state name value; (* Which jobs need to be re-evaluated? *) - let jobnames = try StringMap.find name !dependencies with Not_found -> [] in - reevaluate_whenjobs jobnames; + let jobs = Whenstate.get_dependencies !state [name] in + reevaluate_whenjobs jobs; `ok with @@ -138,17 +117,14 @@ and proc_set_variable (name, value) = and proc_get_variable name = if !debug then Syslog.notice "remote call: get_variable %s" name; - try rpc_of_variable (StringMap.find name !variables) - with (* all non-existent variables are empty strings *) - Not_found -> `string_t "" + rpc_of_variable (Whenstate.get_variable !state name) and proc_get_variable_names () = if !debug then Syslog.notice "remote call: get_variable_names"; - (* Only return variables that are non-empty. *) - let vars = StringMap.fold ( - fun name value xs -> if value <> T_string "" then name :: xs else xs - ) !variables [] in + let vars = Whenstate.get_variable_names !state in + + (* Return variable names as a sorted array. *) let vars = Array.of_list vars in Array.sort compare vars; vars @@ -164,17 +140,63 @@ and proc_exit_daemon () = server := None; `ok +and proc_get_jobs () = + let running = Array.of_list (IntMap.values !runningmap) in + Array.map ( + fun (job, dir, serial, start_time) -> + { Whenproto_aux.job_name = job.job_name; + job_serial = string_of_big_int serial; + job_tmpdir = dir; job_start_time = Int64.of_float start_time } + ) running + +and proc_cancel_job serial = + try + let serial = big_int_of_string serial in + let pid = BigIntMap.find serial !serialmap in + kill pid 15; + `ok + with + | Not_found -> `error "job not found" + | exn -> `error (Printexc.to_string exn) + +and proc_start_job jobname = + try + let job = Whenstate.get_job !state jobname in + run_job job; + `ok + with + | Not_found -> `error "job not found" + | exn -> `error (Printexc.to_string exn) + +and proc_get_job serial = + try + let serial = big_int_of_string serial in + let pid = BigIntMap.find serial !serialmap in + let job, dir, serial, start_time = IntMap.find pid !runningmap in + { Whenproto_aux.job_name = job.job_name; + job_serial = string_of_big_int serial; + job_tmpdir = dir; job_start_time = Int64.of_float start_time } + with + | Not_found -> failwith "job not found" + | exn -> failwith (Printexc.to_string exn) + (* Reload the jobs file. *) and reload_file () = let file = sprintf "%s/jobs.cmo" !jobsdir in - Whenfile.init (); - let js = + (* As we are reloading the file, we want to create a new state + * that has no jobs, but has all the variables from the previous + * state. + *) + let s = Whenstate.copy_variables !state Whenstate.empty in + Whenfile.init s; + + let s = try Dynlink.loadfile file; - let jobs = Whenfile.get_jobs () in - Syslog.notice "loaded %d job(s) from %s" (List.length jobs) file; - jobs + let s = Whenfile.get_state () in + Syslog.notice "loaded %d job(s) from %s" (Whenstate.nr_jobs s) file; + s with | Dynlink.Error err -> let err = Dynlink.error_message err in @@ -183,73 +205,50 @@ and reload_file () = | exn -> failwith (Printexc.to_string exn) in - (* Set 'jobs' and related global variables. *) - let () = - let map = List.fold_left ( - fun map j -> - let name = j.job_name in - StringMap.add name j map - ) StringMap.empty js in - jobs := map in - - let () = - let map = List.fold_left ( - fun map j -> - let deps = dependencies_of_job j in - let name = j.job_name in - List.fold_left ( - fun map d -> - let names = try StringMap.find d map with Not_found -> [] in - StringMap.add d (name :: names) map - ) map deps - ) StringMap.empty js in - dependencies := map in + let s = Whenstate.copy_prev_state !state s in + state := s; (* Re-evaluate all when jobs. *) - reevaluate_whenjobs ~onload:true (StringMap.keys !jobs); + reevaluate_whenjobs ~onload:true (Whenstate.get_whenjobs !state); (* Schedule the next every job to run. *) schedule_next_everyjob () -(* Re-evaluate each named when-statement job, in a loop until we reach - * a fixpoint. Run those that need to be run. every-statement jobs - * are ignored here. +(* Re-evaluate each when-statement job, in a loop until we reach + * a fixpoint. Run those that need to be run. *) -and reevaluate_whenjobs ?(onload=false) jobnames = - let rec loop set jobnames = +and reevaluate_whenjobs ?onload jobs = + let rec loop set jobs = let set' = List.fold_left ( - fun set jobname -> - let job = - try StringMap.find jobname !jobs - with Not_found -> assert false in - assert (jobname = job.job_name); - - let r, job' = - try job_evaluate job !variables onload + fun set job -> + let r, state' = + try Whenstate.evaluate_whenjob ?onload !state job with Invalid_argument err | Failure err -> Syslog.error "error evaluating job %s (at %s): %s" - jobname (Camlp4.PreCast.Ast.Loc.to_string job.job_loc) err; - false, job in + job.job_name (Camlp4.PreCast.Ast.Loc.to_string job.job_loc) err; + false, !state in - jobs := StringMap.add jobname job' !jobs; + state := state'; if !debug then - Syslog.notice "evaluate %s -> %b\n" jobname r; + Syslog.notice "evaluate %s -> %b\n" job.job_name r; - if r then StringSet.add jobname set else set - ) set jobnames in + if r then StringSet.add job.job_name set else set + ) set jobs in if StringSet.compare set set' <> 0 then - loop set' jobnames + loop set' jobs else set' in - let set = loop StringSet.empty jobnames in + let set = loop StringSet.empty jobs in let jobnames = StringSet.elements set in + (* Ensure the jobs always run in predictable (name) order. *) let jobnames = List.sort compare_jobnames jobnames in - List.iter run_job - (List.map (fun jobname -> StringMap.find jobname !jobs) jobnames) + + (* Run the jobs. *) + List.iter run_job (List.map (Whenstate.get_job !state) jobnames) (* Schedule the next every-statement job to run, if there is one. We * look at the every jobs, work out the time that each must run at, @@ -261,11 +260,11 @@ and schedule_next_everyjob () = let t = time () in (* Get only everyjobs. *) - let jobs = StringMap.values !jobs in - let jobs = filter_map ( + let jobs = Whenstate.get_everyjobs !state in + let jobs = List.map ( function - | { job_cond = Every_job period } as job -> Some (job, period) - | { job_cond = When_job _ } -> None + | { job_cond = Every_job period } as job -> (job, period) + | { job_cond = When_job _ } -> assert false ) jobs in (* Map everyjob to next time it must run. *) @@ -334,65 +333,97 @@ and delete_timer_group () = Unixqueue.clear esys g; timer_group := None -and string_of_time_t t = - let tm = gmtime t in - sprintf "%04d-%02d-%02d %02d:%02d:%02d UTC" - (1900+tm.tm_year) (1+tm.tm_mon) tm.tm_mday - tm.tm_hour tm.tm_min tm.tm_sec - and run_job job = - let () = - (* Increment JOBSERIAL. *) - let serial = - match StringMap.find "JOBSERIAL" !variables with - | T_int serial -> - let serial = succ_big_int serial in - variables := StringMap.add "JOBSERIAL" (T_int serial) !variables; - serial - | _ -> assert false in - - Syslog.notice "running %s (JOBSERIAL=%s)" - job.job_name (string_of_big_int serial) in - - (* Create a temporary directory. The current directory of the job - * will be in this directory. The directory is removed when the - * child process exits. + (* Increment JOBSERIAL. *) + let serial = + match Whenstate.get_variable !state "JOBSERIAL" with + | T_int serial -> + let serial = succ_big_int serial in + state := Whenstate.set_variable !state "JOBSERIAL" (T_int serial); + serial + | _ -> assert false in + + (* Call the pre-condition script. Note this may decide not to run + * the job by returning false. *) - let dir = tmpdir () in - - let pid = fork () in - if pid = 0 then ( (* child process running the job *) - chdir dir; - - (* Set environment variables corresponding to each variable. *) - StringMap.iter - (fun name value -> putenv name (string_of_variable value)) !variables; - - (* Set the $JOBNAME environment variable. *) - putenv "JOBNAME" job.job_name; - - (* Create a temporary file containing the shell script fragment. *) - let script = dir // "script" in - let chan = open_out script in - fprintf chan "set -e\n"; (* So that jobs exit on error. *) - output_string chan job.job_script.sh_script; - close_out chan; - chmod script 0o700; - - let shell = try getenv "SHELL" with Not_found -> "/bin/sh" in - - (* Execute the shell script. *) - (try execvp shell [| shell; "-c"; script |]; - with Unix_error (err, fn, _) -> - Syslog.error "%s failed: %s: %s" fn script (error_message err) + let pre_condition () = + match job.job_pre with + | None -> true + | Some pre -> + let rs = ref [] in + IntMap.iter ( + fun pid (job, _, serial, start_time) -> + let r = { pirun_job_name = job.job_name; + pirun_serial = serial; + pirun_start_time = start_time; + pirun_pid = pid } in + rs := r :: !rs + ) !runningmap; + let preinfo = { + pi_job_name = job.job_name; + pi_serial = serial; + pi_variables = Whenstate.get_variables !state; + pi_running = !rs; + } in + pre preinfo + in + if pre_condition () then ( + Syslog.notice "running %s (JOBSERIAL=%s)" + job.job_name (string_of_big_int serial); + + (* Create a temporary directory. The current directory of the job + * will be in this directory. The directory is removed when the + * child process exits. + *) + let dir = tmpdir () in + + let pid = fork () in + if pid = 0 then ( (* child process running the job *) + chdir dir; + + (* Set environment variables corresponding to each variable. *) + List.iter + (fun (name, value) -> putenv name (string_of_variable value)) + (Whenstate.get_variables !state); + + (* Set the $JOBNAME environment variable. *) + putenv "JOBNAME" job.job_name; + + (* Create a temporary file containing the shell script fragment. *) + let script = dir // "script.sh" in + let chan = open_out script in + fprintf chan "set -e\n"; (* So that jobs exit on error. *) + output_string chan job.job_script.sh_script; + close_out chan; + chmod script 0o700; + + let shell = try getenv "SHELL" with Not_found -> "/bin/sh" in + + (* Set output to file. *) + let output = dir // "output.txt" in + let fd = openfile output [O_WRONLY; O_CREAT; O_TRUNC; O_NOCTTY] 0o600 in + dup2 fd stdout; + dup2 fd stderr; + close fd; + + (* Execute the shell script. *) + (try execvp shell [| shell; "-c"; script |]; + with Unix_error (err, fn, _) -> + Syslog.error "%s failed: %s: %s" fn script (error_message err) + ); + _exit 1 ); - _exit 1 - ); - (* Remember this PID, the job and the temporary directory, so we - * can clean up when the child exits. - *) - running := IntMap.add pid (job, dir) !running + (* Remember this PID, the job and the temporary directory, so we + * can clean up when the child exits. + *) + runningmap := IntMap.add pid (job, dir, serial, time ()) !runningmap; + serialmap := BigIntMap.add serial pid !serialmap + ) + else ( + Syslog.notice "not running %s (JOBSERIAL=%s) because pre() condition returned false" + job.job_name (string_of_big_int serial); + ) and tmpdir () = let chan = open_in "/dev/urandom" in @@ -410,13 +441,39 @@ and handle_sigchld _ = let pid, status = waitpid [WNOHANG] 0 in if pid > 0 then ( (* Look up the PID in the running jobs map. *) - let job, dir = IntMap.find pid !running in - running := IntMap.remove pid !running; - cleanup_job job dir + let job, dir, serial, time = IntMap.find pid !runningmap in + runningmap := IntMap.remove pid !runningmap; + serialmap := BigIntMap.remove serial !serialmap; + post_job job dir serial time status ) with Unix_error _ | Not_found -> () -and cleanup_job job dir = +and post_job job dir serial time status = + (* If there is a post function, run it. *) + (match job.job_post with + | None -> () + | Some post -> + let code = + match status with + | WEXITED c -> c + | WSIGNALED s | WSTOPPED s -> 1 in + let result = { + res_job_name = job.job_name; + res_serial = serial; + res_code = code; + res_tmpdir = dir; + res_output = dir // "output.txt"; + res_start_time = time + } in + try post result + with + | Failure msg -> + Syslog.error "job %s post function failed: %s" job.job_name msg + | exn -> + Syslog.error "job %s post function exception: %s" + job.job_name (Printexc.to_string exn) + ); + (* This should be safe because the path cannot contain shell metachars. *) let cmd = sprintf "rm -rf '%s'" dir in ignore (Sys.command cmd)