From c19e141f688952bd67e4af6b9fbd508c0733fe8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 6 Dec 2021 17:58:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Abstracts/BaseProcess.php | 30 +++++++++++++++++++++++++++++- ServerManager.php | 18 ++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Abstracts/BaseProcess.php b/Abstracts/BaseProcess.php index 6733fce..601b9e5 100644 --- a/Abstracts/BaseProcess.php +++ b/Abstracts/BaseProcess.php @@ -3,7 +3,7 @@ namespace Server\Abstracts; -use JetBrains\PhpStorm\Pure; +use Kiri\Context; use Server\Contract\OnProcessInterface; use Swoole\Coroutine; use Swoole\Process; @@ -80,4 +80,32 @@ abstract class BaseProcess implements OnProcessInterface } + /** + * + */ + public function onSigterm(): static + { + if (!Context::inCoroutine()) { + Process::signal(SIGTERM, fn($data) => $this->onShutdown($data)); + } else { + Coroutine::create(function () { + $data = Coroutine::waitSignal(SIGTERM, -1); + if ($data) { + $this->onShutdown($data); + } + }); + } + return $this; + } + + + /** + * @param $data + */ + protected function onShutdown($data): void + { + $this->isStop = true; + } + + } diff --git a/ServerManager.php b/ServerManager.php index 42868dd..f81b368 100644 --- a/ServerManager.php +++ b/ServerManager.php @@ -67,6 +67,9 @@ class ServerManager extends Component private Server|null $server = null; + protected array $initProcesses = []; + + const DEFAULT_EVENT = [ Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'], Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'], @@ -160,14 +163,23 @@ class ServerManager extends Component if (Kiri::getPlatform()->isLinux()) { $process->name($system . '(' . $customProcess->getName() . ')'); } - $customProcess->process($process); + $customProcess->onSigterm()->process($process); }, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine()); $this->logger->debug($system . ' ' . $customProcess->getName() . ' start.'); - $this->container->setBindings($customProcess->getName(), $process); + $this->initProcesses[$customProcess->getName()] = $process; $this->server->addProcess($process); } + /** + * @return array + */ + public function getProcesses(): array + { + return $this->initProcesses; + } + + /** * @return array */ @@ -346,7 +358,6 @@ class ServerManager extends Component * @param array $events * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface - * @throws ReflectionException */ private function addTaskListener(array $events = []): void { @@ -365,7 +376,6 @@ class ServerManager extends Component * @param array|null $settings * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface - * @throws ReflectionException */ public function bindCallback(?array $settings = []) {