diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 4e3274f..97698f6 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -142,8 +142,6 @@ class AsyncServer implements ServerInterface /** * @param SConfig $config * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface * @throws Exception */ public function addListener(SConfig $config): void @@ -217,12 +215,14 @@ class AsyncServer implements ServerInterface /** * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException + * @throws */ public function start(): void { + $pid = (int)file_get_contents(storage('.swoole.pid')); + if (posix_kill($pid, 0)) { + posix_kill($pid, SIGTERM); + } $processManager = Kiri::getDi()->get(EventDispatch::class); $processManager->dispatch(new OnServerBeforeStart()); $this->server->start(); diff --git a/HotReload.php b/HotReload.php index 72dfb36..79d4184 100644 --- a/HotReload.php +++ b/HotReload.php @@ -49,10 +49,10 @@ class HotReload extends Command */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->startProcess(); pcntl_signal(SIGINT | SIGQUIT | SIGTERM, function () { - Process::wait(); + $this->stopProcess(); }); + $this->startProcess(); if (extension_loaded('inotify')) { $this->onInotifyReload(); } else { @@ -70,6 +70,23 @@ class HotReload extends Command $this->process = proc_open([PHP_BINARY, APP_PATH . 'kiri.php', 'sw:server', 'start'], [], $pipes); } + /** + * @return void + * @throws Exception + */ + private function stopProcess(): void + { + if (!is_resource($this->process)) { + return; + } + $pid = (int)file_get_contents(storage('.swoole.pid')); + if (posix_kill($pid, 0)) { + posix_kill($pid, SIGTERM); + } + proc_close($this->process); + $this->process = null; + } + /** * @return void @@ -268,14 +285,7 @@ class HotReload extends Command public function trigger_reload(): void { echo 'tigger server Reload' . PHP_EOL; - if (is_resource($this->process)) { - $pid = (int)file_get_contents(storage('.swoole.pid')); - if (posix_kill($pid, 0)) { - posix_kill($pid, SIGTERM); - } - proc_close($this->process); - } - $this->process = null; + $this->stopProcess(); $this->startProcess(); } diff --git a/Server.php b/Server.php index fa5977b..44b47d4 100644 --- a/Server.php +++ b/Server.php @@ -135,7 +135,6 @@ class Server /** * @param OnTaskerStart $onWorkerStart - * @throws ConfigException */ public function setTaskerName(OnTaskerStart $onWorkerStart): void { @@ -151,7 +150,6 @@ class Server /** * @return void - * @throws ConfigException * @throws Exception */ public function onHotReload(): void diff --git a/State.php b/State.php index 9494004..331bdf2 100644 --- a/State.php +++ b/State.php @@ -5,6 +5,7 @@ namespace Kiri\Server; use Exception; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; +use Kiri\Abstracts\Logger; use Kiri\Server\Abstracts\TraitServer; use Swoole\Process; @@ -22,6 +23,7 @@ class State extends Component */ public function init(): void { + Logger::emergency(); $this->servers = Config::get('server.ports'); } @@ -46,7 +48,7 @@ class State extends Component * @param $port * @throws Exception */ - public function exit($port) + public function exit($port): void { if (!($pid = checkPortIsAlready($port))) { return; diff --git a/composer.json b/composer.json index 151ae4d..636d0fb 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "psr/http-message": "^1.0", "psr/event-dispatcher": "^1.0", "ext-inotify": "*", - "ext-posix": "*" + "ext-posix": "*", + "ext-pcntl": "*" }, "autoload": { "psr-4": {