From f7a2d6f30e54d77fcf9082c1078b377fb2ecbfdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 6 Dec 2021 14:32:04 +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 --- kiri-engine/FileListen/HotReload.php | 47 +++++++++++++++++++++------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/kiri-engine/FileListen/HotReload.php b/kiri-engine/FileListen/HotReload.php index fcce897e..16d90fb9 100644 --- a/kiri-engine/FileListen/HotReload.php +++ b/kiri-engine/FileListen/HotReload.php @@ -86,6 +86,7 @@ class HotReload extends Command public function execute(InputInterface $input, OutputInterface $output): int { $this->initCore(); + $this->trigger_reload(); Timer::tick(1000, fn() => $this->healthCheck()); @@ -124,17 +125,35 @@ class HotReload extends Command return; } $this->driver->clear(); + $this->stopServer(); + $this->stopManager(); + while ($ret = Process::wait(true)) { + echo "PID={$ret['pid']}\n"; + sleep(1); + } + } + + + /** + * @throws Exception + */ + protected function stopServer() + { $pid = file_get_contents(storage('.swoole.pid')); if (!empty($pid) && Process::kill($pid, 0)) { Process::kill($pid, SIGTERM); } + } + + + /** + * + */ + protected function stopManager() + { if ($this->process && Process::kill($this->process->pid, 0)) { Process::kill($this->process->pid) && Process::wait(true); } - while ($ret = Process::wait(true)) { - echo "PID={$ret['pid']}\n"; - sleep(1); - } } @@ -145,17 +164,21 @@ class HotReload extends Command */ public function trigger_reload() { - $this->logger->warning('change reload'); - $pid = $this->process?->pid; - if ($pid && Process::kill($pid, 0)) { - Process::kill($pid) && Process::wait(true); + if ($this->int == 1) { + return; } - $this->process = null; - $process = new Process(function (Process $process) { + $this->int = 1; + $this->logger->warning('change reload'); + + $this->stopServer(); + $this->stopManager(); + + $this->process = new Process(function (Process $process) { $process->exec(PHP_BINARY, [APP_PATH . "kiri.php", "sw:server", "restart"]); }); - $process->start(); - $this->process = $process; + + $this->process->start(); + $this->int = -1; }