modify plugin name
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Reload;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Error\StdoutLoggerInterface;
|
||||
use Kiri\Server\Abstracts\BaseProcess;
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
use Swoole\Timer;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Inotify extends BaseProcess
|
||||
{
|
||||
|
||||
private mixed $inotify = null;
|
||||
private mixed $events;
|
||||
|
||||
private array $watchFiles = [];
|
||||
|
||||
|
||||
public bool $isReloading = FALSE;
|
||||
|
||||
|
||||
public string $name = 'inotify listen';
|
||||
|
||||
|
||||
public array $dirs = [];
|
||||
|
||||
|
||||
protected int $cid = -1;
|
||||
|
||||
const IG_DIR = [APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'];
|
||||
|
||||
|
||||
#[Inject(StdoutLoggerInterface::class)]
|
||||
public StdoutLoggerInterface $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(Process $process): void
|
||||
{
|
||||
// TODO: Implement process() method.
|
||||
set_error_handler([$this, 'error']);
|
||||
set_exception_handler([$this, 'error']);
|
||||
|
||||
if (!extension_loaded('inotify')) {
|
||||
while (true) {
|
||||
if ($this->isStop()) {
|
||||
break;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->dirs = Config::get('reload.inotify', []);
|
||||
$this->start();
|
||||
}
|
||||
|
||||
|
||||
public function onSigterm(): static
|
||||
{
|
||||
pcntl_signal(SIGTERM, function () {
|
||||
$this->isStop = true;
|
||||
});
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function error(): void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$this->inotify = inotify_init();
|
||||
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
|
||||
foreach ($this->dirs as $dir) {
|
||||
if (!is_dir($dir)) continue;
|
||||
$this->watch($dir);
|
||||
}
|
||||
Event::add($this->inotify, [$this, 'check']);
|
||||
Event::cycle(function () {
|
||||
if ($this->isStop()) {
|
||||
Event::del($this->inotify);
|
||||
Event::exit();
|
||||
}
|
||||
}, true);
|
||||
Event::wait();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 开始监听
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
if (!($events = inotify_read($this->inotify))) {
|
||||
return;
|
||||
}
|
||||
if ($this->isReloading) {
|
||||
return;
|
||||
}
|
||||
|
||||
$LISTEN_TYPE = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
|
||||
foreach ($events as $ev) {
|
||||
if (!in_array($ev['mask'], $LISTEN_TYPE)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//非重启类型
|
||||
if (str_ends_with($ev['name'], '.php')) {
|
||||
if ($this->isReloading) {
|
||||
break;
|
||||
}
|
||||
$this->isReloading = TRUE;
|
||||
Timer::after(3000, fn() => $this->reload());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function reload()
|
||||
{
|
||||
$swollen = \Kiri::getDi()->get(SwooleServerInterface::class);
|
||||
|
||||
$swollen->reload();
|
||||
|
||||
$this->clearWatch();
|
||||
foreach ($this->dirs as $root) {
|
||||
$this->watch($root);
|
||||
}
|
||||
$this->isReloading = FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clearWatch()
|
||||
{
|
||||
foreach ($this->watchFiles as $wd) {
|
||||
@inotify_rm_watch($this->inotify, $wd);
|
||||
}
|
||||
$this->watchFiles = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $dir
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function watch($dir): bool
|
||||
{
|
||||
//目录不存在
|
||||
if (!is_dir($dir)) {
|
||||
return $this->logger->addError("[$dir] is not a directory.");
|
||||
}
|
||||
//避免重复监听
|
||||
if (isset($this->watchFiles[$dir])) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (in_array($dir, self::IG_DIR)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
|
||||
$this->watchFiles[$dir] = $wd;
|
||||
|
||||
$files = scandir($dir);
|
||||
foreach ($files as $f) {
|
||||
if ($f == '.' || $f == '..') {
|
||||
continue;
|
||||
}
|
||||
$path = $dir . '/' . $f;
|
||||
//递归目录
|
||||
if (is_dir($path)) {
|
||||
$this->watch($path);
|
||||
} else if (!str_ends_with($f, '.php')) {
|
||||
continue;
|
||||
}
|
||||
//检测文件类型
|
||||
if (strstr($f, '.') == '.php') {
|
||||
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
|
||||
$this->watchFiles[$path] = $wd;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Reload;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Server\Abstracts\BaseProcess;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Process;
|
||||
|
||||
class Scaner extends BaseProcess
|
||||
{
|
||||
|
||||
private array $md5Map = [];
|
||||
|
||||
public bool $isReloading = FALSE;
|
||||
|
||||
|
||||
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();
|
||||
$this->tick();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $isReload
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadDirs(bool $isReload = FALSE)
|
||||
{
|
||||
foreach ($this->dirs as $value) {
|
||||
if (is_bool($path = realpath($value))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_dir($path)) continue;
|
||||
|
||||
$this->loadByDir($path, $isReload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param bool $isReload
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadByDir($path, bool $isReload = FALSE): void
|
||||
{
|
||||
if (!is_string($path)) {
|
||||
return;
|
||||
}
|
||||
$path = rtrim($path, '/');
|
||||
foreach (glob(realpath($path) . '/*') as $value) {
|
||||
if (is_dir($value)) {
|
||||
$this->loadByDir($value, $isReload);
|
||||
}
|
||||
if (is_file($value)) {
|
||||
if ($this->checkFile($value, $isReload)) {
|
||||
if ($this->isReloading) {
|
||||
break;
|
||||
}
|
||||
$this->isReloading = TRUE;
|
||||
|
||||
sleep(2);
|
||||
|
||||
$this->timerReload();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $isReload
|
||||
* @return bool
|
||||
*/
|
||||
private function checkFile($value, $isReload): bool
|
||||
{
|
||||
$md5 = md5($value);
|
||||
$mTime = filectime($value);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function timerReload()
|
||||
{
|
||||
$this->isReloading = TRUE;
|
||||
|
||||
$this->logger->warning('file change');
|
||||
|
||||
$swow = \Kiri::getDi()->get(SwooleServerInterface::class);
|
||||
|
||||
$swow->reload();
|
||||
|
||||
$this->loadDirs();
|
||||
|
||||
$this->isReloading = FALSE;
|
||||
|
||||
$this->tick();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function onSigterm(): static
|
||||
{
|
||||
pcntl_signal(SIGTERM, function () {
|
||||
$this->onProcessStop();
|
||||
});
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function tick()
|
||||
{
|
||||
if ($this->isStop) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->loadDirs(TRUE);
|
||||
|
||||
sleep(2);
|
||||
|
||||
$this->tick();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user