[schedule]
schedule
Runs a script file at fixed times, or when something happens to the server.
Each [[schedule]] entry in nvs.toml has a name, a script file, and either cron or on. cron is a time expression. on is one of four server events:
"start"runs once, after the server starts to accept requests."stop"runs when the server begins to stop. It does not run when the server crashes or is killed."reload"runs after the server reloads its configuration or a changed program, or fails to.nvs reloadstarts a reload too."failed"runs when scheduled scripts or queue jobs fail, at most once a minute.
The script runs isolated, like a request. It reads its entry's name and its on value from Core\Script::arguments().
In plain words: this is cron for your application, written in the same file as the other settings.
A program cannot add an entry and cannot read the list. The server checks every entry when it starts. An entry that can never run stops the start with an error.
Good to know: the scheduled file is a normal program, so nvs run jobs/report.nvs runs it by hand.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
Work that runs on a clock
<?nvs// You add scheduled work with a `[[schedule]]` entry in `nvs.toml`://// [[schedule]]// name = "nightly-report"// cron = "0 3 * * *"// script = "jobs/report.nvs"// scope = "fleet" # once on one server, not once on each server// timezone = "Europe/Vienna"// overlap = "skip" # skip a run if the last one is still running//// The server runs that script at those times. It is kept apart from everything// else, the same way a request is.
// `Core\Config::set('schedule', ...)` does not compile,// because a program may not change `schedule`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.string $name = 'schedule';echo 'schedule myself: ', Core\Config::set($name, '* * * * *') ? 'yes' : 'no', "\n";
// A program cannot read the list either, so the result is `null`. A scheduled// script gets its own entry from `Core\Script::arguments()`: its `name` and `"on" => "cron"`.echo 'read the list: ', Core\Config::get('schedule') ?? "(nothing — the schedule is not a program's to see)", "\n";
// A wrong `scope` value stops the server when it starts. An entry that never// runs would look like one whose time has not come yet.echo 'scope: ', Core\Config::get('schedule.scope') ?? '(nothing written here)', "\n";
// The scheduled script is a normal program. To run it by hand, for example to// test it, use `nvs run jobs/report.nvs`.schedule myself: noread the list: (nothing — the schedule is not a program's to see)scope: (nothing written here)Work that runs on a server event
<?nvs// Shows a script that runs when the server starts, stops, reloads or has failed work.//// [[schedule]]// name = "announce-start"// on = "start" # once, after the server starts to serve// script = "jobs/announce.nvs"// scope = "host" # an `on` entry always runs on each server//// [[schedule]]// name = "announce-stop"// on = "stop" # once, when the server begins to stop// script = "jobs/announce.nvs"// scope = "host"//// [[schedule]]// name = "announce-reload"// on = "reload" # after new code or configuration, or an error in it// script = "jobs/announce.nvs"// scope = "host"//// [[schedule]]// name = "report-failures"// on = "failed" # at most once a minute// script = "jobs/announce.nvs"// scope = "host"
// `Core\Script::arguments()` returns a map with the entry's `name` and the event in `on`.// Here you run the file by hand with `nvs run`, so the result is `null`.var $given = Core\Script::arguments();if ($given == null) { echo "started by hand, not by the server\n";} else { array<mixed> $run = $given as array<mixed>; tainted string $event = $run['on'] as string; echo 'entry ', $run['name'] as string, ' ran for: ', $event, "\n";
// A `failed` run also gets the names of the entries that failed. // It also gets the number of queue jobs that were moved to the dead-letter table. if ($event == 'failed') { array<mixed> $names = $run['schedule'] as array<mixed>; echo 'failed entries: ', Core\Arr::count($names), "\n"; echo 'dead-letter jobs: ', $run['dead_letter'] as int, "\n"; }
// A `reload` run gets the number of programs with new code, // and the number of programs that do not compile now. if ($event == 'reload') { echo 'programs reloaded: ', $run['programs'] as int, "\n"; echo 'programs with errors: ', $run['failed'] as int, "\n"; }}started by hand, not by the server