Skip to content

nvs reload

nvs reload makes a running server use the files on disk now. The server checks its configuration and every loaded program. The command returns when every compile has ended.

It prints one line for each server, and an error: line with the file and line of each error. The exit code is:

  • 0 when every server replied and uses the new files.
  • 1 when a file has an error, or a server did not reply.
  • 2 when no server is running for this data folder, or every server is shutting down.

Only the account that runs the server, or an administrator, can run nvs reload. A request cannot start a reload.

The server compiles only the functions that changed. In development it also checks only the changed files and the files that use them (see [opcache] incremental), each one once.

A reload during a compile stops that compile and starts a new one.

Good to know: a program with an error does not run, and neither does its older version. Its requests fail until the file is fixed.

Examples

The examples below run with this configuration file.

nvs.toml
# The server checks the files of a program by itself only once an hour.
# You run `nvs reload` after each batch of edits instead.
[opcache]
revalidate_freq = "1h"

A deploy script checks the exit code

<?nvs
// Shows a deploy script that copies a new release into place and then runs `nvs reload`.
class Deploy
{
// Returns what the deploy reports for each exit code of `nvs reload`.
public static function outcome(int $code): string
{
return match ($code) {
0 => 'live: every server runs the new release',
1 => 'not live: fix the errors that nvs reload printed',
2 => 'no server is running: the next start uses the new release',
default => 'nvs reload stopped with exit code ' . ($code as string),
};
}
}
// The deploy writes the files of the new release into the app folder.
string $app = Core\Path::join(Core\IO::createTemporaryDirectory(), 'shop');
Core\IO::makeDirectory($app);
Core\IO::write(Core\Path::join($app, 'index.nvs'), "<?nvs\necho 'Shop, release 2';\n");
echo 'copied: ', Core\Arr::count(Core\IO::list($app)), " file\n";
// A real deploy then runs the command and reads its exit code:
// int $code = Core\Process::run('nvs', ['reload'])->exitCode();
// No server runs this example, so it prints the report for each exit code.
array<int> $codes = [0, 1, 2];
foreach ($codes as int $code) {
echo $code, ' = ', Deploy::outcome($code), "\n";
}
Output
copied: 1 file
0 = live: every server runs the new release
1 = not live: fix the errors that nvs reload printed
2 = no server is running: the next start uses the new release

An edit is live at once

<?nvs
// Shows the setting for a project where you run `nvs reload` after each batch of edits.
// `nvs.toml` beside this example sets `revalidate_freq` to `1h`.
// The server then checks the files by itself once an hour. This prints `1h`.
echo 'check the files every: ', Core\Config::get('opcache.revalidate_freq') ?? '(no value)', "\n";
// This is an edit. After you save it, run `nvs reload`.
// The command returns when the new code is compiled, and the next request runs it.
string $page = Core\Path::join(Core\IO::createTemporaryDirectory(), 'hello.nvs');
Core\IO::write($page, "<?nvs\necho 'Hello, version 2';\n");
echo 'saved: ', Core\Path::fileName($page), "\n";
// If you forget `nvs reload`, the server still finds the edit within one hour.
Output
check the files every: 1h
saved: hello.nvs

Changelog

VersionChange
0.0.5nvs reload makes a running nvs serve use the code and configuration on disk at once, and waits until the server has compiled each changed program. It prints each error with its file and line. The exit code is 0 when the server uses every new file, 1 when a file has an error and 2 when no server runs.
0.0.5A [[schedule]] entry with on = "reload" also runs when a check of the program files replaces a program or finds an error. It runs once for the whole check, not once for each program. Core\Script::args() has two more keys: programs, the number of programs replaced, and failed, the number of programs with errors.
0.0.5The server writes one program reloaded line to its log for each program it replaces. A program that does not compile writes one program reload failed line, with the first error and its file and line.
0.0.6A reload compiles only the functions that changed, and uses the saved code of every other function. The program reloaded line in the log counts the files checked and the functions compiled and reused.
0.0.6nvs reload exits with 1 when a server does not reply. Its line says could not be reached or closed the connection without a reply. A server that is shutting down prints a line that says it is stopping.