Schedule a job
This program counts last week’s orders. The server runs it every Monday at 7:00.
<?nvs// This program is a job that the server runs every Monday morning.class WeeklyReport{ // Returns the text of the report for the given orders. public static function text(array<uint> $orders): string { uint $total = 0; foreach ($orders as uint $count) { $total += $count; } return 'orders last week: ' . $total; }}
// A real job would read these numbers from the database.echo WeeklyReport::text([12, 9, 15, 11, 20, 4, 3]), "\n";orders last week: 74The nvs.toml in the same folder sets the time:
[[schedule]]name = "weekly-report"cron = "0 7 * * 1"script = "schedule-job.nvs"scope = "host"nvs serve reads this file and runs the job at that time. The script is in the app folder, so it
needs no permission to start. 0 7 * * 1 means minute 0 of hour 7,
on day 1 of the week, which is Monday. To run the job once by hand, use nvs run schedule-job.nvs.
Run a job when the server starts or stops
Section titled “Run a job when the server starts or stops”Write on instead of cron to run a job when the server starts or stops:
[[schedule]]name = "announce-start"on = "start"script = "announce.nvs"scope = "host"
[[schedule]]name = "announce-stop"on = "stop"script = "announce.nvs"scope = "host"
[capabilities.net]connect = ["hooks.example.com"]The script can post a message to https://hooks.example.com/server. Core\Script::arguments()["on"]
is "start" or "stop". A start job runs after the server begins to serve. A stop job runs
when the server begins to stop. It does not run when the server crashes.
A scheduled job is a longer example. Read
schedule for every setting of a [[schedule]] entry.
