/**
* Method to handle POSIX signals.
*
* @param integer $signal The received POSIX signal.
*
* @return void
*
* @since 1.7.0
* @see pcntl_signal()
* @throws \RuntimeException
*/
public static function signal($signal)
{
// Log all signals sent to the daemon.
Log::add('Received signal: ' . $signal, Log::DEBUG);
// Let's make sure we have an application instance.
if (!is_subclass_of(static::$instance, CliApplication::class)) {
Log::add('Cannot find the application instance.', Log::EMERGENCY);
throw new \RuntimeException('Cannot find the application instance.');
}
// Fire the onReceiveSignal event.
static::$instance->triggerEvent('onReceiveSignal', array($signal));
switch ($signal) {
case SIGINT:
case SIGTERM:
// Handle shutdown tasks
if (static::$instance->running && static::$instance->isActive()) {
static::$instance->shutdown();
} else {
static::$instance->close();
}
break;
case SIGHUP:
// Handle restart tasks
if (static::$instance->running && static::$instance->isActive()) {
static::$instance->shutdown(true);
} else {
static::$instance->close();
}
break;
case SIGCHLD:
// A child process has died
while (static::$instance->pcntlWait($signal, WNOHANG || WUNTRACED) > 0) {
usleep(1000);
}
break;
case SIGCLD:
while (static::$instance->pcntlWait($signal, WNOHANG) > 0) {
$signal = static::$instance->pcntlChildExitStatus($signal);
}
break;
default:
break;
}
}