2022-06-16 17:38:23 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Reload;
|
|
|
|
|
|
2022-06-22 18:28:46 +08:00
|
|
|
use DirectoryIterator;
|
2022-06-16 17:38:23 +08:00
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Abstracts\Config;
|
|
|
|
|
use Kiri\Annotation\Inject;
|
|
|
|
|
use Kiri\Server\Abstracts\BaseProcess;
|
2022-06-22 18:28:46 +08:00
|
|
|
use Kiri\Server\ServerInterface;
|
2022-06-16 17:38:23 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Swoole\Process;
|
2022-09-05 18:28:44 +08:00
|
|
|
use Swoole\Timer;
|
2022-06-16 17:38:23 +08:00
|
|
|
|
2022-06-23 13:44:32 +08:00
|
|
|
class Scanner extends BaseProcess
|
2022-06-16 17:38:23 +08:00
|
|
|
{
|
|
|
|
|
|
2022-06-22 18:28:46 +08:00
|
|
|
private array $md5Map = [];
|
|
|
|
|
|
|
|
|
|
public bool $isReloading = FALSE;
|
2022-06-23 13:44:32 +08:00
|
|
|
|
2022-06-23 00:32:41 +08:00
|
|
|
public string $name = 'hot reload';
|
2022-06-22 18:28:46 +08:00
|
|
|
|
2022-09-05 18:42:53 +08:00
|
|
|
protected bool $enable_coroutine = true;
|
2022-06-22 18:28:46 +08:00
|
|
|
|
|
|
|
|
private array $dirs = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var LoggerInterface
|
|
|
|
|
*/
|
|
|
|
|
#[Inject(LoggerInterface::class)]
|
|
|
|
|
public LoggerInterface $logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function process(Process $process): void
|
|
|
|
|
{
|
|
|
|
|
$this->dirs = Config::get('reload.inotify', []);
|
|
|
|
|
|
|
|
|
|
$this->loadDirs();
|
2022-09-05 18:41:37 +08:00
|
|
|
Timer::tick(3000, fn() => $this->loadDirs(true));
|
2022-06-22 18:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $isReload
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function loadDirs(bool $isReload = FALSE)
|
|
|
|
|
{
|
2022-09-05 18:56:20 +08:00
|
|
|
echo 'file tick ' . date('Y-m-d H:i:s') . PHP_EOL;
|
2022-06-23 13:44:32 +08:00
|
|
|
try {
|
2022-09-05 18:28:44 +08:00
|
|
|
if ($this->isReloading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-23 13:44:32 +08:00
|
|
|
foreach ($this->dirs as $value) {
|
2022-09-05 18:09:01 +08:00
|
|
|
if ($this->isReloading) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-23 13:44:32 +08:00
|
|
|
$value = new DirectoryIterator($value);
|
|
|
|
|
if ($value->isDir()) {
|
|
|
|
|
$this->loadByDir($value, $isReload);
|
|
|
|
|
}
|
2022-06-22 18:28:46 +08:00
|
|
|
}
|
2022-06-23 13:44:32 +08:00
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$this->logger->error($throwable->getMessage(), [$throwable]);
|
2022-09-05 18:14:07 +08:00
|
|
|
}
|
2022-06-22 18:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-23 13:44:32 +08:00
|
|
|
* @param DirectoryIterator $iterator
|
2022-06-22 18:28:46 +08:00
|
|
|
* @param bool $isReload
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-06-23 13:44:32 +08:00
|
|
|
private function loadByDir(DirectoryIterator $iterator, bool $isReload = FALSE): void
|
2022-06-22 18:28:46 +08:00
|
|
|
{
|
2022-06-23 13:44:32 +08:00
|
|
|
foreach ($iterator as $path) {
|
2022-06-22 18:28:46 +08:00
|
|
|
if ($this->isReloading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-05 18:28:44 +08:00
|
|
|
if (!$this->isNeedCheck($path)) {
|
2022-06-23 13:44:32 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($path->isDir()) {
|
|
|
|
|
$this->loadByDir(new DirectoryIterator($path->getRealPath()), $isReload);
|
|
|
|
|
}
|
|
|
|
|
if ($this->checkFile($path, $isReload)) {
|
|
|
|
|
$this->isReloading = TRUE;
|
2022-09-05 18:56:20 +08:00
|
|
|
$this->timerReload();
|
2022-06-23 13:44:32 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-22 18:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-05 18:28:44 +08:00
|
|
|
private function isNeedCheck(DirectoryIterator $path): bool
|
|
|
|
|
{
|
|
|
|
|
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($path->getExtension() !== 'php') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-22 18:28:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param DirectoryIterator $value
|
|
|
|
|
* @param $isReload
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function checkFile(DirectoryIterator $value, $isReload): bool
|
|
|
|
|
{
|
2022-06-23 13:44:32 +08:00
|
|
|
$md5 = md5($value->getRealPath());
|
|
|
|
|
$mTime = filectime($value->getRealPath());
|
2022-06-22 18:28:46 +08:00
|
|
|
if (!isset($this->md5Map[$md5])) {
|
|
|
|
|
if ($isReload) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
$this->md5Map[$md5] = $mTime;
|
|
|
|
|
} else {
|
|
|
|
|
if ($this->md5Map[$md5] != $mTime) {
|
|
|
|
|
if ($isReload) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
$this->md5Map[$md5] = $mTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-07-11 16:33:46 +08:00
|
|
|
/**
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function onSigterm(): static
|
|
|
|
|
{
|
|
|
|
|
pcntl_signal(SIGTERM, function () {
|
2022-09-05 18:47:20 +08:00
|
|
|
Timer::clearAll();
|
2022-07-11 16:33:46 +08:00
|
|
|
$this->onProcessStop();
|
|
|
|
|
});
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-22 18:28:46 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function timerReload()
|
|
|
|
|
{
|
|
|
|
|
$this->logger->warning('file change');
|
|
|
|
|
$swow = \Kiri::getDi()->get(ServerInterface::class);
|
|
|
|
|
$swow->reload();
|
|
|
|
|
|
2022-06-23 13:44:32 +08:00
|
|
|
$this->loadDirs();
|
2022-09-05 18:32:27 +08:00
|
|
|
$this->isReloading = FALSE;
|
2022-06-22 18:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:38:23 +08:00
|
|
|
}
|