Files
kiri-core/System/Process/ServerInotify.php
T

299 lines
5.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-22
* Time: 19:09
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Process;
use Exception;
2020-09-14 10:31:10 +08:00
use Snowflake\Abstracts\Config;
2021-03-01 16:48:30 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2020-11-17 17:41:10 +08:00
use Swoole\Coroutine;
2020-08-31 01:27:08 +08:00
use Swoole\Event;
use Swoole\Timer;
/**
* Class ServerInotify
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Server
2020-08-31 01:27:08 +08:00
*/
class ServerInotify extends Process
{
2020-12-17 14:09:14 +08:00
private mixed $inotify;
2020-10-29 18:17:25 +08:00
private bool $isReloading = false;
private bool $isReloadingOut = false;
private array $watchFiles = [];
private ?array $dirs = [];
private int $events;
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private int $int = -1;
2020-08-31 01:27:08 +08:00
/**
* @param \Swoole\Process $process
* @throws Exception
*/
2021-01-05 15:23:04 +08:00
public function onHandler(\Swoole\Process $process): void
2020-08-31 01:27:08 +08:00
{
2020-09-07 12:07:07 +08:00
set_error_handler([$this, 'onErrorHandler']);
2021-03-05 16:57:12 +08:00
if (env('debug') != 'true') {
2021-03-05 16:39:35 +08:00
Timer::tick(100000, function () {
});
return;
}
2020-09-14 10:38:41 +08:00
$this->dirs = Config::get('inotify', false, [APP_PATH]);
2020-08-31 01:27:08 +08:00
if (extension_loaded('inotify')) {
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
2021-02-28 17:51:19 +08:00
2020-09-14 10:31:10 +08:00
foreach ($this->dirs as $dir) {
$this->watch($dir);
}
2020-08-31 01:27:08 +08:00
Event::add($this->inotify, [$this, 'check']);
Event::wait();
} else {
2021-03-01 16:48:30 +08:00
$this->loadByDir(APP_PATH . 'app');
2020-11-17 17:06:31 +08:00
$this->loadByDir(APP_PATH . 'routes');
$this->loadByDir(__DIR__ . '/../../');
2020-11-17 17:41:10 +08:00
$this->tick();
2020-08-31 01:27:08 +08:00
}
}
2020-10-29 18:17:25 +08:00
private array $md5Map = [];
2020-09-03 01:28:55 +08:00
2020-09-03 01:21:09 +08:00
/**
* @throws Exception
*/
2020-08-31 01:27:08 +08:00
public function tick()
{
2020-11-17 17:06:31 +08:00
if ($this->isReloading) {
return;
2020-09-14 10:38:41 +08:00
}
2020-11-17 17:10:27 +08:00
$this->loadByDir(APP_PATH . 'app', true);
$this->loadByDir(APP_PATH . 'routes', true);
$this->loadByDir(__DIR__ . '/../../', true);
2020-11-17 17:41:10 +08:00
2020-11-17 17:47:24 +08:00
Timer::after(2000, [$this, 'tick']);
2020-09-03 01:28:55 +08:00
}
/**
* @param $path
2020-09-03 01:34:21 +08:00
* @param bool $isReload
2020-12-17 14:10:56 +08:00
* @return void
2020-09-03 01:28:55 +08:00
* @throws Exception
*/
2020-12-17 14:10:56 +08:00
private function loadByDir($path, $isReload = false): void
2020-09-03 01:28:55 +08:00
{
$path = rtrim($path, '/');
2020-11-17 17:41:10 +08:00
foreach (glob(realpath($path) . '/*') as $value) {
2020-09-03 01:28:55 +08:00
if (is_dir($value)) {
2020-09-03 01:35:39 +08:00
$this->loadByDir($value, $isReload);
2020-09-03 01:28:55 +08:00
}
2020-11-17 16:56:25 +08:00
if (is_file($value)) {
2020-11-17 17:41:10 +08:00
if ($this->checkFile($value, $isReload)) {
$this->timerReload();
break;
2020-11-17 16:56:25 +08:00
}
}
}
}
/**
* @param $value
* @param $isReload
* @return bool
*/
2020-12-17 14:09:14 +08:00
private function checkFile($value, $isReload): bool
2020-11-17 16:56:25 +08:00
{
$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) {
2020-09-03 01:34:21 +08:00
if ($isReload) {
2020-11-17 16:56:25 +08:00
return true;
2020-09-03 01:34:21 +08:00
}
2020-09-03 01:35:26 +08:00
$this->md5Map[$md5] = $mTime;
2020-09-03 01:28:55 +08:00
}
}
2020-11-17 16:56:25 +08:00
return false;
2020-08-31 01:27:08 +08:00
}
2020-09-03 01:28:55 +08:00
2020-08-31 01:27:08 +08:00
/**
* 开始监听
*/
public function check()
{
if (!($events = inotify_read($this->inotify))) {
return;
}
if ($this->isReloading) {
if (!$this->isReloadingOut) {
$this->isReloadingOut = true;
}
return;
}
$eventList = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
foreach ($events as $ev) {
if (empty($ev['name'])) {
continue;
}
2020-09-07 11:46:18 +08:00
if ($ev['mask'] == IN_IGNORED) {
2020-08-31 01:27:08 +08:00
continue;
}
2020-09-07 11:46:18 +08:00
if (!in_array($ev['mask'], $eventList)) {
continue;
}
$fileType = strstr($ev['name'], '.');
2020-09-07 11:34:57 +08:00
//非重启类型
2020-09-07 11:46:18 +08:00
if ($fileType !== '.php') {
2020-09-07 11:34:57 +08:00
continue;
2020-08-31 01:27:08 +08:00
}
2020-09-07 11:34:57 +08:00
if ($this->int !== -1) {
return;
}
$this->int = @swoole_timer_after(2000, [$this, 'reload']);
2020-09-07 11:46:18 +08:00
2020-08-31 01:27:08 +08:00
$this->isReloading = true;
}
}
/**
* @throws Exception
*/
public function reload()
{
2020-09-03 01:37:02 +08:00
$this->isReloading = true;
2020-08-31 01:27:08 +08:00
$this->trigger_reload();
2020-11-17 16:59:22 +08:00
$this->clearWatch();
foreach ($this->dirs as $root) {
$this->watch($root);
2020-08-31 01:27:08 +08:00
}
$this->int = -1;
$this->isReloading = FALSE;
$this->isReloadingOut = FALSE;
2020-09-18 21:52:09 +08:00
$this->md5Map = [];
2020-11-11 15:20:30 +08:00
}
/**
* @throws Exception
*/
public function timerReload()
{
$this->isReloading = true;
$this->trigger_reload();
$this->int = -1;
2020-11-17 17:06:31 +08:00
$this->loadByDir(APP_PATH . 'app');
$this->loadByDir(APP_PATH . 'routes');
$this->loadByDir(__DIR__ . '/../../');
2020-11-17 16:49:34 +08:00
$this->isReloading = FALSE;
$this->isReloadingOut = FALSE;
2020-11-17 17:41:10 +08:00
$this->tick();
2020-08-31 01:27:08 +08:00
}
2021-03-01 00:37:30 +08:00
2020-08-31 01:27:08 +08:00
/**
* 重启
2020-10-14 11:21:56 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
public function trigger_reload()
{
2020-09-14 10:31:10 +08:00
Snowflake::reload();
2020-08-31 01:27:08 +08:00
}
/**
* 清理所有inotify监听
*/
public function clearWatch()
{
2020-09-07 11:54:57 +08:00
foreach ($this->watchFiles as $wd) {
try {
2020-09-07 11:59:54 +08:00
inotify_rm_watch($this->inotify, $wd);
} catch (\Throwable $exception) {
2020-09-07 11:51:14 +08:00
}
2020-08-31 01:27:08 +08:00
}
2021-03-01 16:48:30 +08:00
$this->watchFiles = [];
}
2020-08-31 01:27:08 +08:00
2021-03-01 16:48:53 +08:00
2020-09-07 12:04:43 +08:00
/**
2021-01-12 17:43:48 +08:00
* @param $code
* @param $message
* @param $file
* @param $line
2021-03-01 16:48:30 +08:00
* @throws ComponentException
2020-09-07 12:04:43 +08:00
*/
2021-01-12 17:43:48 +08:00
protected function onErrorHandler($code, $message, $file, $line)
2020-09-07 12:04:43 +08:00
{
2021-03-01 16:48:30 +08:00
if (str_contains($message, 'The file descriptor is not an inotify instance')) {
return;
}
2020-09-07 17:36:52 +08:00
$this->application->debug('Error:' . $message);
$this->application->debug($file . ':' . $line);
2020-09-07 12:04:43 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $dir
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function watch($dir): bool
2020-08-31 01:27:08 +08:00
{
2020-09-07 11:46:18 +08:00
//目录不存在
2020-08-31 01:27:08 +08:00
if (!is_dir($dir)) {
2020-09-07 11:46:18 +08:00
throw new Exception("[$dir] is not a directory.");
2020-08-31 01:27:08 +08:00
}
2020-09-07 11:46:18 +08:00
//避免重复监听
if (isset($this->watchFiles[$dir])) {
2020-08-31 01:27:08 +08:00
return FALSE;
}
2020-09-07 11:46:18 +08:00
if (in_array($dir, [APP_PATH . '/config', APP_PATH . '/commands', APP_PATH . '/.git', APP_PATH . '/.gitee'])) {
return FALSE;
}
2020-08-31 01:27:08 +08:00
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
$this->watchFiles[$dir] = $wd;
$files = scandir($dir);
foreach ($files as $f) {
2020-09-07 11:46:18 +08:00
if ($f == '.' or $f == '..' or $f == 'runtime' or preg_match('/\.txt/', $f) or preg_match('/\.sql/', $f) or preg_match('/\.log/', $f)) {
2020-08-31 01:27:08 +08:00
continue;
}
$path = $dir . '/' . $f;
//递归目录
if (is_dir($path)) {
2020-09-14 10:31:10 +08:00
$this->watch($path);
2020-08-31 01:27:08 +08:00
}
2020-09-07 11:46:18 +08:00
//检测文件类型
if (strstr($f, '.') == '.php') {
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd;
}
2020-08-31 01:27:08 +08:00
}
return TRUE;
}
}