From be9f4281e6a7be118b3bcdf7ceecd56abe51aee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 18 Nov 2021 19:01: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 --- kiri-engine/FileListen/HotReload.php | 38 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/kiri-engine/FileListen/HotReload.php b/kiri-engine/FileListen/HotReload.php index 3ad7e445..88d99f95 100644 --- a/kiri-engine/FileListen/HotReload.php +++ b/kiri-engine/FileListen/HotReload.php @@ -7,7 +7,6 @@ use Kiri\Abstracts\Config; use Kiri\Abstracts\Logger; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Server\Constant; use Swoole\Coroutine; use Swoole\Process; use Symfony\Component\Console\Command\Command; @@ -31,6 +30,9 @@ class HotReload extends Command public int $int = -1; + private ?Process $process = null; + + protected mixed $source = null; protected mixed $pipes = []; @@ -70,10 +72,18 @@ class HotReload extends Command swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather'); } $this->trigger_reload(); - Coroutine::create(fn() => $this->onExit()); - Coroutine::create(function () use ($driver) { - $driver->start(); + Process::signal(SIGTERM | SIGKILL, function ($data) { + var_dump($data); + $pid = file_get_contents(storage('.swoole.pid')); + if (!empty($pid) && Process::kill($pid, 0)) { + Process::kill($pid, SIGTERM); + } + while ($ret = Process::wait(true)) { + echo "PID={$ret['pid']}\n"; + sleep(1); + } }); + $driver->start(); return 0; } @@ -104,12 +114,16 @@ class HotReload extends Command proc_terminate($this->source); while (proc_get_status($this->source)['running']) { Coroutine::sleep(1); + var_dump(proc_get_status($this->source)['running']); } + var_dump(proc_get_status($this->source)['running']); proc_close($this->source); + var_dump('isClose.'); $this->source = null; } } + /** * @param $code * @param $message @@ -134,20 +148,18 @@ class HotReload extends Command public function trigger_reload() { Kiri::getDi()->get(Logger::class)->warning('change reload'); - Coroutine::create(function () { - $reusePort = Config::get('server.settings')[Constant::OPTION_ENABLE_REUSE_PORT] ?? false; - if (!$reusePort) { - $this->source = proc_open("php " . APP_PATH . "kiri.php sw:server", [], $pipes); - return; - } + if ($this->process instanceof Process && Process::kill($this->process->pid, 0)) { $pid = file_get_contents(storage('.swoole.pid')); - $source = proc_open("php " . APP_PATH . "kiri.php sw:server start", [], $pipes); if (!empty($pid) && Process::kill($pid, 0)) { Process::kill($pid, SIGTERM); } - $this->stop(); - $this->source = $source; + Process::kill($this->process->pid, SIGTERM); + Process::wait(true); + } + $this->process = new Process(function (Process $process) { + $process->exec(PHP_BINARY, [APP_PATH . "kiri.php", "sw:server", "restart"]); }); + $this->process->start(); }