(* Handle a top level statement. *)
let rec call_stmt name (_loc, stmt, sh) =
- let name = if name <> "" then name else unique_job_name () in
- let name = <:expr< $str:name$ >> in
+ let name =
+ match name with
+ | None -> let name = unique_job_name () in <:expr< $str:name$ >>
+ | Some name -> name in
match stmt with
| `When e -> when_stmt _loc name e sh
| `Every p -> every_stmt _loc name p sh
(* "str_item" is a top level statement in an OCaml program. *)
str_item: LEVEL "top" [
- [ s = statement -> call_stmt "" s ]
- | [ "job"; name = STRING; s = statement -> call_stmt name s ]
+ [ s = statement -> call_stmt None s ]
+ | [ "job"; name = expr; s = statement -> call_stmt (Some name) s ]
];
END
EXTRA_DIST = $(SOURCES) test_load.ml
-SOURCES = t010_load.ml t020_simple.ml t030_jobnames.ml
+SOURCES = t010_load.ml t020_simple.ml t030_jobnames.ml t040_ocaml_jobnames.ml
tests = $(SOURCES:.ml=.cmo)
--- /dev/null
+(* whenjobs
+ * Copyright (C) 2012 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Test OCaml job names. *)
+
+let prefix = "foo_"
+
+job (prefix ^ "check")
+when foo = "value" :
+<<
+ # nothing
+>>
+
+job (prefix ^ "poll")
+every minute :
+<<
+ # nothing
+>>
+
+(* no job name *)
+when bar = "value" :
+<<
+ # nothing
+>>
The job name is passed to the shell script in the C<$JOBNAME>
environment variable.
+=head2 OCAML EXPRESSIONS
+
+As well as simple "every" and "when" expressions, advanced users may
+want to use arbitrary OCaml expressions, functions, etc in the jobs
+script. These are useful for factoring common code or strings, for
+setting the initial values of variables, or for defining cleanup
+functions.
+
+A simple example of an OCaml expression is:
+
+ let prefix = "daily_"
+
+ job (prefix ^ "virus_scan")
+ every day :
+ <<
+ # ...
+ >>
+
+ job (prefix ^ "disk_check")
+ every day :
+ <<
+ # ...
+ >>
+
+which creates two jobs called C<"daily_virus_scan"> and
+C<"daily_disk_check"> (C<^> is the OCaml string concatenation
+operator).
+
+The OCaml expressions run once, when the jobs file is being loaded or
+reloaded.
+