This commit is contained in:
2021-11-18 19:01:11 +08:00
parent 57be085166
commit be9f4281e6
+25 -13
View File
@@ -7,7 +7,6 @@ use Kiri\Abstracts\Config;
use Kiri\Abstracts\Logger; use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Server\Constant;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@@ -31,6 +30,9 @@ class HotReload extends Command
public int $int = -1; public int $int = -1;
private ?Process $process = null;
protected mixed $source = null; protected mixed $source = null;
protected mixed $pipes = []; protected mixed $pipes = [];
@@ -70,10 +72,18 @@ class HotReload extends Command
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather'); swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
} }
$this->trigger_reload(); $this->trigger_reload();
Coroutine::create(fn() => $this->onExit()); Process::signal(SIGTERM | SIGKILL, function ($data) {
Coroutine::create(function () use ($driver) { var_dump($data);
$driver->start(); $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; return 0;
} }
@@ -104,12 +114,16 @@ class HotReload extends Command
proc_terminate($this->source); proc_terminate($this->source);
while (proc_get_status($this->source)['running']) { while (proc_get_status($this->source)['running']) {
Coroutine::sleep(1); Coroutine::sleep(1);
var_dump(proc_get_status($this->source)['running']);
} }
var_dump(proc_get_status($this->source)['running']);
proc_close($this->source); proc_close($this->source);
var_dump('isClose.');
$this->source = null; $this->source = null;
} }
} }
/** /**
* @param $code * @param $code
* @param $message * @param $message
@@ -134,20 +148,18 @@ class HotReload extends Command
public function trigger_reload() public function trigger_reload()
{ {
Kiri::getDi()->get(Logger::class)->warning('change reload'); Kiri::getDi()->get(Logger::class)->warning('change reload');
Coroutine::create(function () { if ($this->process instanceof Process && Process::kill($this->process->pid, 0)) {
$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;
}
$pid = file_get_contents(storage('.swoole.pid')); $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)) { if (!empty($pid) && Process::kill($pid, 0)) {
Process::kill($pid, SIGTERM); Process::kill($pid, SIGTERM);
} }
$this->stop(); Process::kill($this->process->pid, SIGTERM);
$this->source = $source; Process::wait(true);
}
$this->process = new Process(function (Process $process) {
$process->exec(PHP_BINARY, [APP_PATH . "kiri.php", "sw:server", "restart"]);
}); });
$this->process->start();
} }