Skip to content

A scheduled job

This program deletes old log files and keeps the newest three. The server runs it every night at 3:00.

<?nvs
// This program deletes old log files and keeps the newest three. The server runs it every night.
class Cleanup
{
// Deletes all but the newest `$keep` files in `$dir`, and returns the names it deleted.
public static function run(string $dir, uint $keep): array<string>
{
// The names start with the date, so sorting them puts the oldest first.
array<string> $names = Core\Arr::sort(Core\IO::list($dir));
array<string> $deleted = [];
for (uint $i = 0; $i + $keep < Core\Arr::count($names); $i++) {
Core\IO::remove(Core\Path::join($dir, $names[$i]));
$deleted[] = $names[$i];
}
return $deleted;
}
}
// The program writes five log files first, so that it runs as it is.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'logs');
Core\IO::makeDirectory($dir);
array<string> $days = ['2026-09-28', '2026-09-29', '2026-09-30', '2026-10-01', '2026-10-02'];
foreach ($days as string $day) {
Core\IO::write(Core\Path::join($dir, $day . '.log'), "...\n");
}
foreach (Cleanup::run($dir, 3) as string $name) {
echo 'deleted ', $name, "\n";
}
echo 'kept ', Core\Arr::count(Core\IO::list($dir)), " files\n";
Output
deleted 2026-09-28.log
deleted 2026-09-29.log
kept 3 files

The nvs.toml in the same folder sets the time and allows the program to delete files:

[[schedule]]
name = "clean-up-logs"
cron = "0 3 * * *"
script = "scheduled-job.nvs"
scope = "host"
[capabilities.fs]
read = true
write = true

nvs serve reads this file and runs the job at those times. To run the job once by hand, for example to test it, use nvs run scheduled-job.nvs.

Core\IO::list returns the names of the files in a folder. The names start with a date, so Core\Arr::sort puts the oldest first. The loop deletes every file except the last three.

cron sets when the job runs. 0 3 * * * means minute 0 of hour 3, on every day. scope = "host" runs the job on every server. Use scope = "fleet" to run it once, on one of your servers.

The server may start a script in the app folder with no setting. A script in another folder needs that folder in [capabilities.script] spawn.

An entry can use on instead of cron. Then the job runs when something happens to the server. These two entries send a message to a chat when the server starts and when it 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"]

Both entries start the same script. Core\Script::arguments() returns the entry’s name and on. The value of on is the event, for example "start". When you run the script by hand, the result is null:

<?nvs
// This program sends a message to a chat when the server starts or stops.
// `Core\Script::arguments()` returns the entry's `name` and `on`. Run by hand, 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>;
Core\Http\Client::post('https://hooks.example.com/server', { json: ['job' => $run['name'], 'event' => $run['on']] });
}
Output
started by hand, not by the server

A start job runs once, after the server begins to accept requests. The server does not wait for it, so a slow job does not delay any request.

A stop job runs when the server begins to stop. The server waits for it, up to [server] drain_timeout. A stop job does not run when the server crashes or is killed. To get an alert when a server is down, use a monitor that runs on another computer.

An on entry needs scope = "host", and it cannot have a timezone. on can also be "reload" or "failed".

Read schedule for every setting of a [[schedule]] entry.