This commit is contained in:
2026-07-08 09:42:56 +08:00
parent 0adfe14a1f
commit 7f36bb0d53
5 changed files with 78 additions and 12 deletions
+9 -4
View File
@@ -9,7 +9,6 @@ use Kiri\Exception\NotFindClassException;
use Kiri\Server\Config as SConfig; use Kiri\Server\Config as SConfig;
use Kiri\Server\Constant; use Kiri\Server\Constant;
use Kiri\Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnServerBeforeStart;
use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Handler\OnServer; use Kiri\Server\Handler\OnServer;
use Kiri\Server\Processes\TraitProcess; use Kiri\Server\Processes\TraitProcess;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
@@ -32,6 +31,8 @@ class AsyncServer extends Component implements ServerInterface
*/ */
private ?Server $server = null; private ?Server $server = null;
private bool $shuttingDown = false;
/** /**
* @param array $service * @param array $service
@@ -61,14 +62,18 @@ class AsyncServer extends Component implements ServerInterface
*/ */
public function shutdown(): bool 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; return true;
} }
/** /**
* @param array $service * @param array $service
* @param int $daemon * @param int $daemon
+26 -3
View File
@@ -245,15 +245,38 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
di(HotReloadState::class)->store($changedFiles); di(HotReloadState::class)->store($changedFiles);
di(StdoutLogger::class)->println('detected file changes, reloading server: ' . $preview); di(StdoutLogger::class)->println('detected file changes, reloading server: ' . $preview);
$server = di(ServerInterface::class); if (!$this->reloadWorkers()) {
if (method_exists($server, 'reload')) { di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed');
$server->reload();
} }
} finally { } finally {
$this->reloading = false; $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 private function startInotify(): void
{ {
$this->inotifyFd = inotify_init(); $this->inotifyFd = inotify_init();
+27 -2
View File
@@ -7,7 +7,6 @@ use Kiri\Error\StdoutLogger;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Router\Router; use Kiri\Router\Router;
use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\ServerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Swoole\Event; use Swoole\Event;
use Swoole\Process; use Swoole\Process;
@@ -116,7 +115,9 @@ class HotReload extends AbstractProcess
di(StdoutLogger::class)->println('reloading server[' . \config('site.id', 'system-service') . '], please waite.'); di(StdoutLogger::class)->println('reloading server[' . \config('site.id', 'system-service') . '], please waite.');
$this->clear(); $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->addListen();
$this->reloading = false; $this->reloading = false;
@@ -126,6 +127,30 @@ class HotReload extends AbstractProcess
/** /**
* @return void * @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 protected function addListen(): void
{ {
foreach (config('servers.reload.listen') as $value) { foreach (config('servers.reload.listen') as $value) {
+12 -1
View File
@@ -42,7 +42,19 @@ trait TraitServer
*/ */
public function onSigint($no, array $signInfo): void public function onSigint($no, array $signInfo): void
{ {
static $handling = false;
if ($handling) {
return;
}
$handling = true;
try { 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); Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
$this->shutdown(); $this->shutdown();
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
@@ -50,7 +62,6 @@ trait TraitServer
} }
} }
/** /**
* @param $signal * @param $signal
* @param $callback * @param $callback
+3 -1
View File
@@ -6,6 +6,7 @@ namespace Kiri\Server\Processes;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
use const SIGHUP; use const SIGHUP;
use const SIGINT;
use const SIGTERM; use const SIGTERM;
/** /**
@@ -133,7 +134,8 @@ abstract class AbstractProcess implements OnProcessInterface
Coroutine::set($array); Coroutine::set($array);
Coroutine::create(fn() => $this->coroutineWaitSignal()); Coroutine::create(fn() => $this->coroutineWaitSignal());
} else { } else {
$process::signal(SIGTERM | SIGINT | SIGUSR1, [$this, 'pointWaitSignal']); $process::signal(SIGTERM, [$this, 'pointWaitSignal']);
$process::signal(SIGINT, [$this, 'pointWaitSignal']);
} }
return $this; return $this;
} }