Implement graceful shutdown for background task runner #2451

This commit is contained in:
Kijin Sung 2025-05-21 19:04:45 +09:00
parent 8014413163
commit bacf067f87
2 changed files with 58 additions and 1 deletions

View file

@ -52,6 +52,19 @@ if (PHP_SAPI !== 'cli')
}
}
// Set up a signal handler.
if (PHP_SAPI === 'cli' && function_exists('pcntl_signal'))
{
if (function_exists('pcntl_async_signals'))
{
pcntl_async_signals(true);
}
foreach ([SIGINT, SIGHUP, SIGTERM, SIGQUIT, SIGUSR1, SIGUSR2] as $signal)
{
pcntl_signal($signal, [Rhymix\Framework\Queue::class, 'signalHandler']);
}
}
// Create multiple processes if configured.
if (PHP_SAPI === 'cli' && $process_count > 1 && function_exists('pcntl_fork') && function_exists('pcntl_waitpid'))
{
@ -84,7 +97,7 @@ if (PHP_SAPI === 'cli' && $process_count > 1 && function_exists('pcntl_fork') &&
}
// The parent process waits for its children to finish.
while (count($pids))
while (count($pids) && !Rhymix\Framework\Queue::signalReceived())
{
$pid = pcntl_waitpid(-1, $status, \WNOHANG);
if ($pid)