modify plugin name
This commit is contained in:
+117
-123
@@ -2,167 +2,161 @@
|
|||||||
|
|
||||||
namespace Kiri\Reload;
|
namespace Kiri\Reload;
|
||||||
|
|
||||||
|
use DirectoryIterator;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use Kiri\Server\Abstracts\BaseProcess;
|
use Kiri\Server\Abstracts\BaseProcess;
|
||||||
|
use Kiri\Server\ServerInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
|
||||||
class Scaner extends BaseProcess
|
class Scaner extends BaseProcess
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $md5Map = [];
|
private array $md5Map = [];
|
||||||
|
|
||||||
public bool $isReloading = FALSE;
|
public bool $isReloading = FALSE;
|
||||||
|
|
||||||
|
|
||||||
private array $dirs = [];
|
private array $dirs = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var LoggerInterface
|
* @var LoggerInterface
|
||||||
*/
|
*/
|
||||||
#[Inject(LoggerInterface::class)]
|
#[Inject(LoggerInterface::class)]
|
||||||
public LoggerInterface $logger;
|
public LoggerInterface $logger;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function process(Process $process): void
|
public function process(Process $process): void
|
||||||
{
|
{
|
||||||
$this->dirs = Config::get('reload.inotify', []);
|
$this->dirs = Config::get('reload.inotify', []);
|
||||||
|
|
||||||
$this->loadDirs();
|
$this->loadDirs();
|
||||||
$this->tick();
|
$this->tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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))) {
|
$value = new DirectoryIterator($value);
|
||||||
continue;
|
if ($value->isDot() || str_starts_with($value->getFilename(), '.')) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
if (!is_dir($path)) continue;
|
if ($value->isDir()) {
|
||||||
|
$this->loadByDir($value, $isReload);
|
||||||
$this->loadByDir($path, $isReload);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param DirectoryIterator $path
|
||||||
* @param bool $isReload
|
* @param bool $isReload
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function loadByDir($path, bool $isReload = FALSE): void
|
private function loadByDir(DirectoryIterator $path, bool $isReload = FALSE): void
|
||||||
{
|
{
|
||||||
if (!is_string($path)) {
|
if ($path->isDir()) {
|
||||||
return;
|
$this->loadByDir(new DirectoryIterator($path->getRealPath()), $isReload);
|
||||||
}
|
}
|
||||||
$path = rtrim($path, '/');
|
if (!str_ends_with($path->getFilename(), '.php')) {
|
||||||
foreach (glob(realpath($path) . '/*') as $value) {
|
return;
|
||||||
if (is_dir($value)) {
|
}
|
||||||
$this->loadByDir($value, $isReload);
|
if ($this->checkFile($path, $isReload)) {
|
||||||
}
|
if ($this->isReloading) {
|
||||||
if (is_file($value)) {
|
return;
|
||||||
if ($this->checkFile($value, $isReload)) {
|
}
|
||||||
if ($this->isReloading) {
|
$this->isReloading = TRUE;
|
||||||
break;
|
sleep(2);
|
||||||
}
|
$this->timerReload();
|
||||||
$this->isReloading = TRUE;
|
}
|
||||||
|
}
|
||||||
sleep(2);
|
|
||||||
|
|
||||||
$this->timerReload();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param DirectoryIterator $value
|
||||||
* @param $isReload
|
* @param $isReload
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function checkFile($value, $isReload): bool
|
private function checkFile(DirectoryIterator $value, $isReload): bool
|
||||||
{
|
{
|
||||||
$md5 = md5($value);
|
$md5 = md5_file($value->getRealPath());
|
||||||
$mTime = filectime($value);
|
$mTime = $value->getCTime();
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function timerReload()
|
public function timerReload()
|
||||||
{
|
{
|
||||||
$this->isReloading = TRUE;
|
$this->isReloading = TRUE;
|
||||||
|
|
||||||
$this->logger->warning('file change');
|
$this->logger->warning('file change');
|
||||||
|
|
||||||
$swow = \Kiri::getDi()->get(SwooleServerInterface::class);
|
$swow = \Kiri::getDi()->get(ServerInterface::class);
|
||||||
|
|
||||||
$swow->reload();
|
$swow->reload();
|
||||||
|
|
||||||
$this->loadDirs();
|
$this->loadDirs();
|
||||||
|
|
||||||
$this->isReloading = FALSE;
|
$this->isReloading = FALSE;
|
||||||
|
|
||||||
$this->tick();
|
$this->tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function onSigterm(): static
|
public function onSigterm(): static
|
||||||
{
|
{
|
||||||
pcntl_signal(SIGTERM, function () {
|
pcntl_signal(SIGTERM, function () {
|
||||||
$this->onProcessStop();
|
$this->onProcessStop();
|
||||||
});
|
});
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function tick()
|
public function tick()
|
||||||
{
|
{
|
||||||
if ($this->isStop) {
|
if ($this->isStop) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->loadDirs(TRUE);
|
$this->loadDirs(TRUE);
|
||||||
|
|
||||||
sleep(2);
|
sleep(2);
|
||||||
|
|
||||||
$this->tick();
|
$this->tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user