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 $isReloadingOut = false;
public bool $isReloading = FALSE;
public bool $isReloadingOut = FALSE;
public ?array $dirs = [];
public int $events;
@@ -34,7 +34,7 @@ class HotReload extends Command
public int $int = -1;
private ?Process $process = null;
private ?Process $process = NULL;
public Inotify|Scaner $driver;
@@ -44,11 +44,11 @@ class HotReload extends Command
public Logger $logger;
protected mixed $source = null;
protected mixed $source = NULL;
protected mixed $pipes = [];
protected ?Coroutine\Channel $channel = null;
protected ?Coroutine\Channel $channel = NULL;
/**
@@ -56,7 +56,7 @@ class HotReload extends Command
protected function configure()
{
$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()
{
swoole_async_set(['enable_coroutine' => false]);
swoole_async_set(['enable_coroutine' => FALSE]);
set_error_handler([$this, 'errorHandler']);
if (Kiri::getPlatform()->isLinux()) {
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'))) {
$pid = (int)file_get_contents(storage('.manager.pid'));
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());
@@ -137,7 +137,7 @@ class HotReload extends Command
$this->trigger_reload();
if ($input->getOption('') == '--daemon') {
Process::daemon(true, true);
Process::daemon(TRUE, TRUE);
}
Timer::tick(1000, fn() => $this->healthCheck());
@@ -155,6 +155,9 @@ class HotReload extends Command
public function healthCheck()
{
$pid = (int)file_get_contents(storage('.swoole.pid'));
if ($this->driver->isReloading) {
return;
}
if (empty($pid)) {
$this->logger->warning('service is shutdown you need reload.');
$this->trigger_reload();
@@ -178,7 +181,7 @@ class HotReload extends Command
$this->driver->clear();
$this->stopServer();
$this->stopManager();
while ($ret = Process::wait(true)) {
while ($ret = Process::wait(TRUE)) {
echo "PID={$ret['pid']}\n";
sleep(1);
}
@@ -203,7 +206,7 @@ class HotReload extends Command
protected function stopManager()
{
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 = [];
protected bool $isReloading = FALSE;
public bool $isReloading = FALSE;
protected int $cid;
+17 -15
View File
@@ -3,12 +3,16 @@
namespace Kiri\FileListen;
use Exception;
use Swoole\Timer;
class Scaner
{
private array $md5Map = [];
public bool $isReloading = FALSE;
/**
* @param array $dirs
* @param HotReload $process
@@ -32,7 +36,7 @@ class Scaner
* @param bool $isReload
* @throws Exception
*/
private function loadDirs(bool $isReload = false)
private function loadDirs(bool $isReload = FALSE)
{
foreach ($this->dirs as $value) {
if (is_bool($path = realpath($value))) {
@@ -52,7 +56,7 @@ class Scaner
* @return void
* @throws Exception
*/
private function loadByDir($path, bool $isReload = false): void
private function loadByDir($path, bool $isReload = FALSE): void
{
if (!is_string($path)) {
return;
@@ -64,7 +68,8 @@ class Scaner
}
if (is_file($value)) {
if ($this->checkFile($value, $isReload)) {
$this->timerReload();
Timer::after(2000, fn() => $this->timerReload());
$this->isReloading = TRUE;
break;
}
}
@@ -83,18 +88,18 @@ class Scaner
$mTime = filectime($value);
if (!isset($this->md5Map[$md5])) {
if ($isReload) {
return true;
return TRUE;
}
$this->md5Map[$md5] = $mTime;
} else {
if ($this->md5Map[$md5] != $mTime) {
if ($isReload) {
return true;
return TRUE;
}
$this->md5Map[$md5] = $mTime;
}
}
return false;
return FALSE;
}
@@ -103,28 +108,25 @@ class Scaner
*/
public function timerReload()
{
if ($this->process->isReloading) {
return;
}
$this->process->isReloading = true;
$this->isReloading = TRUE;
$this->process->trigger_reload();
$this->process->int = -1;
$this->loadDirs();
$this->process->isReloading = FALSE;
$this->isReloading = FALSE;
$this->process->isReloadingOut = FALSE;
$this->tick();
}
private bool $isStop = false;
private bool $isStop = FALSE;
public function clear()
{
$this->isStop = true;
$this->isStop = TRUE;
}
@@ -133,11 +135,11 @@ class Scaner
*/
public function tick()
{
if ($this->process->isReloading || $this->isStop) {
if ($this->isReloading || $this->isStop) {
return;
}
$this->loadDirs(true);
$this->loadDirs(TRUE);
sleep(2);