This commit is contained in:
2022-09-05 18:28:44 +08:00
parent 85d19dcccf
commit b764ced6fa
+23 -29
View File
@@ -10,6 +10,7 @@ use Kiri\Server\Abstracts\BaseProcess;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Swoole\Process; use Swoole\Process;
use Swoole\Timer;
class Scanner extends BaseProcess class Scanner extends BaseProcess
{ {
@@ -40,7 +41,7 @@ class Scanner extends BaseProcess
$this->dirs = Config::get('reload.inotify', []); $this->dirs = Config::get('reload.inotify', []);
$this->loadDirs(); $this->loadDirs();
$this->tick(); Timer::tick(3000, fn() => $this->loadByDir());
} }
@@ -51,6 +52,9 @@ class Scanner extends BaseProcess
private function loadDirs(bool $isReload = FALSE) private function loadDirs(bool $isReload = FALSE)
{ {
try { try {
if ($this->isReloading) {
return;
}
foreach ($this->dirs as $value) { foreach ($this->dirs as $value) {
if ($this->isReloading) { if ($this->isReloading) {
break; break;
@@ -62,9 +66,8 @@ class Scanner extends BaseProcess
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$this->logger->error($throwable->getMessage(), [$throwable]); $this->logger->error($throwable->getMessage(), [$throwable]);
} } finally {
if ($this->isReloading) { $this->loadDirs($isReload);
$this->timerReload();
} }
} }
@@ -81,28 +84,35 @@ class Scanner extends BaseProcess
if ($this->isReloading) { if ($this->isReloading) {
return; return;
} }
/** @var DirectoryIterator $path */ if (!$this->isNeedCheck($path)) {
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue; continue;
} }
if ($path->getExtension() !== 'php') {
continue;
}
if ($path->isDir()) { if ($path->isDir()) {
$this->loadByDir(new DirectoryIterator($path->getRealPath()), $isReload); $this->loadByDir(new DirectoryIterator($path->getRealPath()), $isReload);
} }
if ($this->checkFile($path, $isReload)) { if ($this->checkFile($path, $isReload)) {
$this->isReloading = TRUE; $this->isReloading = TRUE;
Timer::after(3000, fn() => $this->timerReload());
break; break;
} }
} }
} }
private function isNeedCheck(DirectoryIterator $path): bool
{
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
return false;
}
if ($path->getExtension() !== 'php') {
return false;
}
return true;
}
/** /**
* @param DirectoryIterator $value * @param DirectoryIterator $value
* @param $isReload * @param $isReload
@@ -148,8 +158,6 @@ class Scanner extends BaseProcess
{ {
$this->logger->warning('file change'); $this->logger->warning('file change');
var_dump('file change');
$swow = \Kiri::getDi()->get(ServerInterface::class); $swow = \Kiri::getDi()->get(ServerInterface::class);
$swow->reload(); $swow->reload();
@@ -159,18 +167,4 @@ class Scanner extends BaseProcess
$this->loadDirs(); $this->loadDirs();
} }
/**
* @throws Exception
*/
public function tick()
{
if ($this->isStop) {
return;
}
$this->loadDirs(TRUE);
sleep(2);
$this->tick();
}
} }