diff --git a/Processes/AbstractProcess.php b/Processes/AbstractProcess.php index fdee087..c9f1dd4 100644 --- a/Processes/AbstractProcess.php +++ b/Processes/AbstractProcess.php @@ -5,6 +5,7 @@ namespace Kiri\Server\Processes; use Swoole\Coroutine; use Swoole\Process; +use const SIGHUP; /** * @@ -125,30 +126,49 @@ abstract class AbstractProcess implements OnProcessInterface { $this->process = $process; if ($this->enable_coroutine) { - Coroutine::set([ - 'enable_deadlock_check' => false, - 'deadlock_check_disable_trace' => false, - 'exit_condition' => function () { - return Coroutine::stats()['coroutine_num'] === 0; - } - ]); - Coroutine::create(fn () => $this->coroutineWaitSignal()); + $array['enable_deadlock_check'] = false; + $array['deadlock_check_disable_trace'] = false; + $array['exit_condition'] = [$this, 'exit_condition']; + Coroutine::set($array); + Coroutine::create(fn() => $this->coroutineWaitSignal()); } else { - pcntl_signal(SIGTERM, [$this, 'pointWaitSignal']); + pcntl_signal(\SIGTERM, [$this, 'pointWaitSignal']); + pcntl_signal(\SIGINT, [$this, 'pointWaitSignal']); + pcntl_signal(\SIGUSR1, [$this, 'pointWaitSignal']); + pcntl_signal(SIGHUP, [$this, 'pointWaitSignal']); } return $this; } + public function exit_condition(): bool + { + return Coroutine::stats()['coroutine_num'] === 0; + } + + /** - * @param $data + * @param $signal * @return void */ - public function pointWaitSignal($data): void + public function pointWaitSignal($signal): void { $this->stop = true; - $this->onSigterm(); + switch ($signal) { + case \SIGTERM: + case \SIGUSR1: + case \SIGINT: + // some stuff before stop consumer e.g. delete lock etc + pcntl_signal($signal, SIG_DFL); // restore handler + posix_kill(posix_getpid(), $signal); + break; // kill self with signal, see https://www.cons.org/cracauer/sigint.html + case SIGHUP: + // some stuff to restart consumer + break; + default: + // do nothing + } }