This commit is contained in:
2021-11-19 02:54:05 +08:00
parent 841a6b4a19
commit 0a6fdda720
+120 -115
View File
@@ -21,142 +21,147 @@ class HotReload extends Command
{ {
public bool $isReloading = false; public bool $isReloading = FALSE;
public bool $isReloadingOut = false; public bool $isReloadingOut = FALSE;
public ?array $dirs = []; public ?array $dirs = [];
public int $events; public int $events;
public int $int = -1; public int $int = -1;
private ?Process $process = null; private ?Process $process = NULL;
protected mixed $source = null; protected mixed $source = NULL;
protected mixed $pipes = []; protected mixed $pipes = [];
protected ?Coroutine\Channel $channel = null; protected ?Coroutine\Channel $channel = NULL;
/** /**
* *
*/ */
protected function configure() protected function configure()
{ {
$this->setName('sw:wather') $this->setName('sw:wather')
->setDescription('server start'); ->setDescription('server start');
} }
/** /**
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return int * @return int
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function execute(InputInterface $input, OutputInterface $output): int public function execute(InputInterface $input, OutputInterface $output): int
{ {
// TODO: Implement onHandler() method. // TODO: Implement onHandler() method.
set_error_handler([$this, 'onErrorHandler']); set_error_handler([$this, 'onErrorHandler']);
$this->dirs = Config::get('inotify', [APP_PATH . 'app']); $this->dirs = Config::get('inotify', [APP_PATH . 'app']);
if (!extension_loaded('inotify')) { if (!extension_loaded('inotify')) {
$driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]); $driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]);
} else { } else {
$driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]); $driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]);
} }
if (Kiri::getPlatform()->isLinux()) { if (Kiri::getPlatform()->isLinux()) {
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();
pcntl_signal(SIGTERM | SIGKILL, function ($data) { Process::signal(SIGKILL, [$this, 'onSignal']);
var_dump($data); Process::signal(SIGTERM, [$this, 'onSignal']);
$pid = file_get_contents(storage('.swoole.pid')); $driver->start();
if (!empty($pid) && Process::kill($pid, 0)) { return 0;
Process::kill($pid, SIGTERM); }
}
});
$driver->start();
return 0;
}
/** public function onSignal($data)
* @throws Exception {
*/ var_dump($data);
public function onExit() $pid = file_get_contents(storage('.swoole.pid'));
{ if (!empty($pid) && Process::kill($pid, 0)) {
$data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1); Process::kill($pid, SIGTERM);
if ($data) { }
$pid = file_get_contents(storage('.swoole.pid')); }
if (!empty($pid) && Process::kill($pid, 0)) {
Process::kill($pid, SIGTERM);
}
$this->stop();
$this->source = null;
}
}
/** /**
* @throws Exception * @throws Exception
*/ */
private function stop(): void public function onExit()
{ {
if (is_resource($this->source)) { $data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
proc_terminate($this->source); if ($data) {
while (proc_get_status($this->source)['running']) { $pid = file_get_contents(storage('.swoole.pid'));
Coroutine::sleep(1); if (!empty($pid) && Process::kill($pid, 0)) {
var_dump(proc_get_status($this->source)['running']); Process::kill($pid, SIGTERM);
} }
var_dump(proc_get_status($this->source)['running']); $this->stop();
proc_close($this->source); $this->source = NULL;
var_dump('isClose.'); }
$this->source = null; }
}
}
/** /**
* @param $code * @throws Exception
* @param $message */
* @param $file private function stop(): void
* @param $line {
* @throws Exception if (is_resource($this->source)) {
*/ proc_terminate($this->source);
public function onErrorHandler($code, $message, $file, $line) while (proc_get_status($this->source)['running']) {
{ Coroutine::sleep(1);
if (str_contains($message, 'The file descriptor is not an inotify instance')) { var_dump(proc_get_status($this->source)['running']);
return; }
} var_dump(proc_get_status($this->source)['running']);
debug('Error:' . $message . ' at ' . $file . ':' . $line); proc_close($this->source);
} var_dump('isClose.');
$this->source = NULL;
}
}
/** /**
* 重启 * @param $code
* * @param $message
* @throws Exception * @param $file
*/ * @param $line
public function trigger_reload() * @throws Exception
{ */
Kiri::getDi()->get(Logger::class)->warning('change reload'); public function onErrorHandler($code, $message, $file, $line)
if ($this->process instanceof Process && Process::kill($this->process->pid, 0)) { {
$pid = file_get_contents(storage('.swoole.pid')); if (str_contains($message, 'The file descriptor is not an inotify instance')) {
if (!empty($pid) && Process::kill($pid, 0)) { return;
Process::kill($pid, SIGTERM); }
} debug('Error:' . $message . ' at ' . $file . ':' . $line);
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(); * @throws Exception
} */
public function trigger_reload()
{
Kiri::getDi()->get(Logger::class)->warning('change reload');
if ($this->process instanceof Process && Process::kill($this->process->pid, 0)) {
$pid = file_get_contents(storage('.swoole.pid'));
if (!empty($pid) && Process::kill($pid, 0)) {
Process::kill($pid, SIGTERM);
}
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();
}
} }