Skip to content

Core\Script::onExit

Core\Script::onExit() adds a function that runs when the script ends. It runs after the last line of the script, after exit, after Core\Script::finish() and after an error that nothing caught. If you add several functions, they run in the order you added them, and each one runs once.

Each function receives a Core\Script\ExitReport. The report says how the script ended, which exit status the process will have, and which error ended it, if there was one. A function cannot change the ending. If a function throws an error, the error is logged and the next function still runs.

Two endings run no function: a FATAL error, such as a script that goes over its memory limit, and a script that was cancelled.

The examples below show two functions that run after the last line, a function that runs after exit, and a job that releases its lock when an error ends it.

Parameters

  • $hookcallable(Core\Script\ExitReport): mixedThe function to run. It receives one Core\Script\ExitReport that describes the ending, and its return value is not used. It may also take no parameter. If it throws an error, the error is logged and the next function still runs.

Return value

Returns void. Nothing. The list belongs to this request only. Adding the same function twice runs it twice. A function added while the list runs is added to the end of the same list. A function cannot change the ending, because the report is fixed before the first one runs. exit or Core\Script::finish() inside one of these functions stops that function, and the error is logged as a RuntimeError.

Examples

Run code after the last line

<?nvs
// Shows how to run a function when the script ends. The functions run after
// the last line of the script, in the order you added them.
// A function may take no parameter.
Core\Script::onExit(fn(): void => {
echo "first: the script has ended\n";
});
// A function may also take the report. `status()` returns the exit status,
// so this prints "second: status 0".
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
echo 'second: status ', $report->status(), "\n";
});
// This line prints before both functions.
echo "the last line of the script\n";
Output
the last line of the script
first: the script has ended
second: status 0

A script that calls exit

<?nvs
// Shows that the functions also run after `exit`. A `finally` block does not
// run after `exit`, so `Core\Script::onExit()` is the place for the last work.
// proof: exit 2
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
// This prints "closing: status 2".
echo 'closing: status ', $report->status(), "\n";
});
try {
echo "the input file is missing\n";
exit(2);
} finally {
// This line does not print.
echo "the finally block\n";
}
Output
the input file is missing
closing: status 2

Release a lock at every ending

<?nvs
// Shows a job that releases its lock however it ends. A job that stops with
// the lock still taken would block every later run.
// proof: exit 1
final class Lock
{
public static bool $taken = false;
public static function take(): void
{
Lock::$taken = true;
echo "lock taken\n";
// The release is added right after the lock is taken.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
Lock::$taken = false;
echo 'lock released, status ', $report->status(), "\n";
});
}
}
Lock::take();
echo "importing orders\n";
// Nothing catches this error, so the script ends here with status 1.
// The function above still runs and releases the lock.
throw new RuntimeError('the order file is not valid');
Output
lock taken
importing orders
lock released, status 1