From 2a5b7d1cac14bf06bf9563e77e1e053cdf210dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 20 Jun 2022 18:14:25 +0800 Subject: [PATCH] modify plugin name --- Abstracts/AsyncServer.php | 15 +++++++++++++++ Abstracts/CoroutineServer.php | 27 ++++++++++++++++++++++++++- Abstracts/ProcessManager.php | 14 ++++++++++++++ Server.php | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index a8c9545..1f9179a 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -7,6 +7,7 @@ use Kiri; use Kiri\Abstracts\Config; use Kiri\Di\ContainerInterface; use Kiri\Exception\ConfigException; +use Kiri\Server\Events\OnShutdown; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; @@ -81,6 +82,20 @@ class AsyncServer implements ServerInterface } + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException + */ + public function shutdown(): void + { + $this->server->shutdown(); + + $this->dispatch->dispatch(new OnShutdown()); + } + + /** * @param \Kiri\Server\Config $config * @param int $daemon diff --git a/Abstracts/CoroutineServer.php b/Abstracts/CoroutineServer.php index 61b793c..5499424 100644 --- a/Abstracts/CoroutineServer.php +++ b/Abstracts/CoroutineServer.php @@ -7,6 +7,7 @@ use Kiri\Di\ContainerInterface; use Kiri\Events\EventDispatch; use Kiri\Exception\ConfigException; use Kiri\Server\Constant; +use Kiri\Server\Events\OnShutdown; use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\Events\OnWorkerStop; use Kiri\Server\ServerInterface; @@ -33,6 +34,9 @@ class CoroutineServer implements ServerInterface use TraitServer; + private bool $isShutdown = false; + + /** * @param Config $config * @param ContainerInterface $container @@ -164,6 +168,23 @@ class CoroutineServer implements ServerInterface } + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws \ReflectionException + */ + public function shutdown(): void + { + $this->isShutdown = true; + $this->processManager->shutdown(); + foreach ($this->servers as $server) { + $server->shutdown(); + } + $this->dispatch->dispatch(new OnShutdown()); + } + + /** * @return void * @throws ConfigException @@ -195,7 +216,11 @@ class CoroutineServer implements ServerInterface $server->start(); - $this->dispatch->dispatch(new OnWorkerStop($server,0)); + $this->dispatch->dispatch(new OnWorkerStop($server, 0)); + + if ($this->isShutdown) { + return; + } $this->runServer($server); } diff --git a/Abstracts/ProcessManager.php b/Abstracts/ProcessManager.php index 944da94..6bd6fa6 100644 --- a/Abstracts/ProcessManager.php +++ b/Abstracts/ProcessManager.php @@ -61,6 +61,20 @@ class ProcessManager } + /** + * @return void + */ + public function shutdown(): void + { + foreach ($this->_process as $process) { + if (!$process instanceof Process) { + continue; + } + $process->exit(0); + } + } + + /** * @param $customProcess * @param $system diff --git a/Server.php b/Server.php index 5da11ac..57c73ea 100644 --- a/Server.php +++ b/Server.php @@ -204,7 +204,7 @@ class Server extends HttpService } catch (\Throwable $exception) { $this->logger->error($exception->getMessage()); } finally { - $this->manager->getServer()->shutdown(); + $this->manager->shutdown(); } }