Skip to content

Core\Signal::onShutdown

Core\Signal::onShutdown registers a function that runs when the process is asked to stop. This happens when an operator stops the server, or when the program gets a stop signal such as Ctrl-C or SIGTERM. All of these stop signals have the same effect.

The function runs once, between two statements of your program. At that time the server already accepts no new requests, and Core\Server::isDraining() returns true. The function gets no arguments. It cannot stop the shutdown or delay it. If it throws an error, the rest of it does not run. After it returns, your program continues and should finish its work quickly.

Each request has its own function. A second call replaces the first one, and the function is deleted when the request ends. If nothing asks the process to stop, the function never runs.

The examples below register a message, replace one function with another, and let a worker stop between two jobs.

Parameters

  • $handlercallable(): mixedThe function to run. It gets no arguments and returns nothing. Every stop signal has the same effect, so the function cannot tell which one arrived. If it throws an error or reaches the request's memory limit, the rest of it does not run.

Return value

Returns void. Nothing. Each request has its own function, and a second call replaces the first. The function is deleted when the request ends, and other requests cannot see it. It cannot stop the shutdown or delay it.

Examples

Print a message on shutdown

<?nvs
// Shows how to print a message when the program is asked to stop.
// This function runs only if the program gets a stop signal, such as Ctrl-C.
Core\Signal::onShutdown(fn(): void => {
echo "Stopping. Goodbye.\n";
});
// Nothing asks this program to stop, so the message above is not printed.
echo "The program is running.\n";
echo "The program is done.\n";
Output
The program is running.
The program is done.

A second call replaces the first

<?nvs
// Shows that a second call to `Core\Signal::onShutdown` replaces the first function.
Core\Signal::onShutdown(fn(): void => {
echo "first function\n";
});
// This call replaces the function above. On a stop signal, only "second function" is printed.
Core\Signal::onShutdown(fn(): void => {
echo "second function\n";
});
// Registering a function does not start a shutdown. This prints `false`.
echo 'draining: ', Core\Server::isDraining() ? 'true' : 'false', "\n";
Output
draining: false

A worker stops between two jobs

<?nvs
// Shows a worker that finishes its current job and then stops when it is asked to.
final class Worker
{
// The shutdown function reads this to say how far the worker got.
public static int $done = 0;
}
Core\Signal::onShutdown(fn(): void => {
echo 'Asked to stop after ', Worker::$done, " jobs.\n";
});
array<string> $jobs = ['resize photo', 'send invoice', 'update stock'];
foreach ($jobs as string $job) {
// After a stop signal, `Core\Server::isDraining()` returns `true`.
// The worker then starts no new job.
if (Core\Server::isDraining()) {
break;
}
echo 'Done: ', $job, "\n";
Worker::$done = Worker::$done + 1;
}
// Nothing asks this program to stop, so all three jobs run.
echo 'Jobs done: ', Worker::$done, "\n";
Output
Done: resize photo
Done: send invoice
Done: update stock
Jobs done: 3