diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index ef8ec7b..abd59d0 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -9,7 +9,6 @@ use Kiri\Exception\NotFindClassException; use Kiri\Server\Config as SConfig; use Kiri\Server\Constant; use Kiri\Server\Events\OnServerBeforeStart; -use Kiri\Server\Events\OnShutdown; use Kiri\Server\Handler\OnServer; use Kiri\Server\Processes\TraitProcess; use Kiri\Server\ServerInterface; @@ -32,6 +31,8 @@ class AsyncServer extends Component implements ServerInterface */ private ?Server $server = null; + private bool $shuttingDown = false; + /** * @param array $service @@ -61,14 +62,18 @@ class AsyncServer extends Component implements ServerInterface */ public function shutdown(): bool { - $this->server->shutdown(); + if ($this->shuttingDown) { + return true; + } + $this->shuttingDown = true; - $this->dispatch->dispatch(new OnShutdown); + if ($this->server !== null) { + $this->server->shutdown(); + } return true; } - /** * @param array $service * @param int $daemon diff --git a/Abstracts/FileWatcher.php b/Abstracts/FileWatcher.php index d5356e4..4e1d1c6 100644 --- a/Abstracts/FileWatcher.php +++ b/Abstracts/FileWatcher.php @@ -245,15 +245,38 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface di(HotReloadState::class)->store($changedFiles); di(StdoutLogger::class)->println('detected file changes, reloading server: ' . $preview); - $server = di(ServerInterface::class); - if (method_exists($server, 'reload')) { - $server->reload(); + if (!$this->reloadWorkers()) { + di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed'); } } finally { $this->reloading = false; } } + private function reloadWorkers(): bool + { + $pid = $this->getMasterPid(); + if ($pid === null) { + return false; + } + + return Process::kill($pid, SIGUSR1); + } + + private function getMasterPid(): ?int + { + $pidFile = function_exists('storage') ? storage('.swoole.pid') : ''; + if (!is_file($pidFile)) { + return null; + } + + $pid = (int)trim((string)file_get_contents($pidFile)); + if ($pid <= 1 || !Process::kill($pid, 0)) { + return null; + } + + return $pid; + } private function startInotify(): void { $this->inotifyFd = inotify_init(); diff --git a/Abstracts/HotReload.php b/Abstracts/HotReload.php index 5111699..8590ab2 100644 --- a/Abstracts/HotReload.php +++ b/Abstracts/HotReload.php @@ -7,7 +7,6 @@ use Kiri\Error\StdoutLogger; use Kiri\Events\EventProvider; use Kiri\Router\Router; use Kiri\Server\Events\OnWorkerStart; -use Kiri\Server\ServerInterface; use Psr\Log\LoggerInterface; use Swoole\Event; use Swoole\Process; @@ -116,7 +115,9 @@ class HotReload extends AbstractProcess di(StdoutLogger::class)->println('reloading server[' . \config('site.id', 'system-service') . '], please waite.'); $this->clear(); - di(ServerInterface::class)->reload(); + if (!$this->reloadWorkers()) { + di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed'); + } $this->addListen(); $this->reloading = false; @@ -126,6 +127,30 @@ class HotReload extends AbstractProcess /** * @return void */ + private function reloadWorkers(): bool + { + $pid = $this->getMasterPid(); + if ($pid === null) { + return false; + } + + return Process::kill($pid, SIGUSR1); + } + + private function getMasterPid(): ?int + { + $pidFile = function_exists('storage') ? storage('.swoole.pid') : ''; + if (!is_file($pidFile)) { + return null; + } + + $pid = (int)trim((string)file_get_contents($pidFile)); + if ($pid <= 1 || !Process::kill($pid, 0)) { + return null; + } + + return $pid; + } protected function addListen(): void { foreach (config('servers.reload.listen') as $value) { diff --git a/Abstracts/TraitServer.php b/Abstracts/TraitServer.php index 7cda410..b39fce3 100644 --- a/Abstracts/TraitServer.php +++ b/Abstracts/TraitServer.php @@ -42,15 +42,26 @@ trait TraitServer */ public function onSigint($no, array $signInfo): void { + static $handling = false; + if ($handling) { + return; + } + $handling = true; + try { + $pidFile = function_exists('storage') ? storage('.swoole.pid') : ''; + $masterPid = is_file($pidFile) ? (int)trim((string)file_get_contents($pidFile)) : 0; + if ($masterPid > 1 && $masterPid !== getmypid()) { + return; + } + Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no); $this->shutdown(); } catch (\Throwable $exception) { - \Kiri::getLogger()->json_log($exception); + \Kiri::getLogger()->json_log($exception); } } - /** * @param $signal * @param $callback diff --git a/Processes/AbstractProcess.php b/Processes/AbstractProcess.php index 9efd223..76844a3 100644 --- a/Processes/AbstractProcess.php +++ b/Processes/AbstractProcess.php @@ -6,6 +6,7 @@ namespace Kiri\Server\Processes; use Swoole\Coroutine; use Swoole\Process; use const SIGHUP; +use const SIGINT; use const SIGTERM; /** @@ -133,7 +134,8 @@ abstract class AbstractProcess implements OnProcessInterface Coroutine::set($array); Coroutine::create(fn() => $this->coroutineWaitSignal()); } else { - $process::signal(SIGTERM | SIGINT | SIGUSR1, [$this, 'pointWaitSignal']); + $process::signal(SIGTERM, [$this, 'pointWaitSignal']); + $process::signal(SIGINT, [$this, 'pointWaitSignal']); } return $this; }