This commit is contained in:
2021-12-11 05:33:00 +08:00
parent ebb7ac9673
commit 8870a7ca27
3 changed files with 286 additions and 281 deletions
+14 -11
View File
@@ -25,8 +25,8 @@ 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;
@@ -34,7 +34,7 @@ class HotReload extends Command
public int $int = -1; public int $int = -1;
private ?Process $process = null; private ?Process $process = NULL;
public Inotify|Scaner $driver; public Inotify|Scaner $driver;
@@ -44,11 +44,11 @@ class HotReload extends Command
public Logger $logger; public Logger $logger;
protected mixed $source = null; protected mixed $source = NULL;
protected mixed $pipes = []; protected mixed $pipes = [];
protected ?Coroutine\Channel $channel = null; protected ?Coroutine\Channel $channel = NULL;
/** /**
@@ -56,7 +56,7 @@ class HotReload extends Command
protected function configure() protected function configure()
{ {
$this->setName('sw:wather')->setDescription('server start') $this->setName('sw:wather')->setDescription('server start')
->addOption('daemon', null, InputOption::VALUE_OPTIONAL, "是否后台运行", "--front"); ->addOption('daemon', NULL, InputOption::VALUE_OPTIONAL, "是否后台运行", "--front");
} }
@@ -83,7 +83,7 @@ class HotReload extends Command
*/ */
public function setProcessName() public function setProcessName()
{ {
swoole_async_set(['enable_coroutine' => false]); swoole_async_set(['enable_coroutine' => FALSE]);
set_error_handler([$this, 'errorHandler']); set_error_handler([$this, 'errorHandler']);
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');
@@ -99,7 +99,7 @@ class HotReload extends Command
if (file_exists(storage('.manager.pid'))) { if (file_exists(storage('.manager.pid'))) {
$pid = (int)file_get_contents(storage('.manager.pid')); $pid = (int)file_get_contents(storage('.manager.pid'));
if ($pid > 0 && Process::kill($pid, 0)) { if ($pid > 0 && Process::kill($pid, 0)) {
Process::kill($pid, 15) && Process::wait(true); Process::kill($pid, 15) && Process::wait(TRUE);
} }
} }
file_put_contents(storage('.manager.pid'), getmypid()); file_put_contents(storage('.manager.pid'), getmypid());
@@ -137,7 +137,7 @@ class HotReload extends Command
$this->trigger_reload(); $this->trigger_reload();
if ($input->getOption('') == '--daemon') { if ($input->getOption('') == '--daemon') {
Process::daemon(true, true); Process::daemon(TRUE, TRUE);
} }
Timer::tick(1000, fn() => $this->healthCheck()); Timer::tick(1000, fn() => $this->healthCheck());
@@ -155,6 +155,9 @@ class HotReload extends Command
public function healthCheck() public function healthCheck()
{ {
$pid = (int)file_get_contents(storage('.swoole.pid')); $pid = (int)file_get_contents(storage('.swoole.pid'));
if ($this->driver->isReloading) {
return;
}
if (empty($pid)) { if (empty($pid)) {
$this->logger->warning('service is shutdown you need reload.'); $this->logger->warning('service is shutdown you need reload.');
$this->trigger_reload(); $this->trigger_reload();
@@ -178,7 +181,7 @@ class HotReload extends Command
$this->driver->clear(); $this->driver->clear();
$this->stopServer(); $this->stopServer();
$this->stopManager(); $this->stopManager();
while ($ret = Process::wait(true)) { while ($ret = Process::wait(TRUE)) {
echo "PID={$ret['pid']}\n"; echo "PID={$ret['pid']}\n";
sleep(1); sleep(1);
} }
@@ -203,7 +206,7 @@ class HotReload extends Command
protected function stopManager() protected function stopManager()
{ {
if ($this->process && Process::kill($this->process->pid, 0)) { if ($this->process && Process::kill($this->process->pid, 0)) {
Process::kill($this->process->pid) && Process::wait(true); Process::kill($this->process->pid) && Process::wait(TRUE);
} }
} }
+1 -1
View File
@@ -15,7 +15,7 @@ class Inotify
private array $watchFiles = []; private array $watchFiles = [];
protected bool $isReloading = FALSE; public bool $isReloading = FALSE;
protected int $cid; protected int $cid;
+17 -15
View File
@@ -3,12 +3,16 @@
namespace Kiri\FileListen; namespace Kiri\FileListen;
use Exception; use Exception;
use Swoole\Timer;
class Scaner class Scaner
{ {
private array $md5Map = []; private array $md5Map = [];
public bool $isReloading = FALSE;
/** /**
* @param array $dirs * @param array $dirs
* @param HotReload $process * @param HotReload $process
@@ -32,7 +36,7 @@ class Scaner
* @param bool $isReload * @param bool $isReload
* @throws Exception * @throws Exception
*/ */
private function loadDirs(bool $isReload = false) private function loadDirs(bool $isReload = FALSE)
{ {
foreach ($this->dirs as $value) { foreach ($this->dirs as $value) {
if (is_bool($path = realpath($value))) { if (is_bool($path = realpath($value))) {
@@ -52,7 +56,7 @@ class Scaner
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function loadByDir($path, bool $isReload = false): void private function loadByDir($path, bool $isReload = FALSE): void
{ {
if (!is_string($path)) { if (!is_string($path)) {
return; return;
@@ -64,7 +68,8 @@ class Scaner
} }
if (is_file($value)) { if (is_file($value)) {
if ($this->checkFile($value, $isReload)) { if ($this->checkFile($value, $isReload)) {
$this->timerReload(); Timer::after(2000, fn() => $this->timerReload());
$this->isReloading = TRUE;
break; break;
} }
} }
@@ -83,18 +88,18 @@ class Scaner
$mTime = filectime($value); $mTime = filectime($value);
if (!isset($this->md5Map[$md5])) { if (!isset($this->md5Map[$md5])) {
if ($isReload) { if ($isReload) {
return true; return TRUE;
} }
$this->md5Map[$md5] = $mTime; $this->md5Map[$md5] = $mTime;
} else { } else {
if ($this->md5Map[$md5] != $mTime) { if ($this->md5Map[$md5] != $mTime) {
if ($isReload) { if ($isReload) {
return true; return TRUE;
} }
$this->md5Map[$md5] = $mTime; $this->md5Map[$md5] = $mTime;
} }
} }
return false; return FALSE;
} }
@@ -103,28 +108,25 @@ class Scaner
*/ */
public function timerReload() public function timerReload()
{ {
if ($this->process->isReloading) { $this->isReloading = TRUE;
return;
}
$this->process->isReloading = true;
$this->process->trigger_reload(); $this->process->trigger_reload();
$this->process->int = -1; $this->process->int = -1;
$this->loadDirs(); $this->loadDirs();
$this->process->isReloading = FALSE; $this->isReloading = FALSE;
$this->process->isReloadingOut = FALSE; $this->process->isReloadingOut = FALSE;
$this->tick(); $this->tick();
} }
private bool $isStop = false; private bool $isStop = FALSE;
public function clear() public function clear()
{ {
$this->isStop = true; $this->isStop = TRUE;
} }
@@ -133,11 +135,11 @@ class Scaner
*/ */
public function tick() public function tick()
{ {
if ($this->process->isReloading || $this->isStop) { if ($this->isReloading || $this->isStop) {
return; return;
} }
$this->loadDirs(true); $this->loadDirs(TRUE);
sleep(2); sleep(2);