改名
This commit is contained in:
+251
-246
@@ -12,7 +12,10 @@ namespace Snowflake\Process;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Snowflake\Abstracts\Config;
|
use Snowflake\Abstracts\Config;
|
||||||
|
use Snowflake\Exception\ComponentException;
|
||||||
|
use Snowflake\Exception\ConfigException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
use Swoole\Coroutine;
|
||||||
use Swoole\Event;
|
use Swoole\Event;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
|
|
||||||
@@ -22,290 +25,292 @@ use Swoole\Timer;
|
|||||||
*/
|
*/
|
||||||
class ServerInotify extends Process
|
class ServerInotify extends Process
|
||||||
{
|
{
|
||||||
private mixed $inotify;
|
private mixed $inotify;
|
||||||
private bool $isReloading = false;
|
private bool $isReloading = false;
|
||||||
private bool $isReloadingOut = false;
|
private bool $isReloadingOut = false;
|
||||||
private array $watchFiles = [];
|
private array $watchFiles = [];
|
||||||
private ?array $dirs = [];
|
private ?array $dirs = [];
|
||||||
private int $events;
|
private int $events;
|
||||||
|
|
||||||
private int $int = -1;
|
private int $int = -1;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param \Swoole\Process $process
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(\Swoole\Process $process): void
|
public function onHandler(\Swoole\Process $process): void
|
||||||
{
|
{
|
||||||
set_error_handler([$this, 'onErrorHandler']);
|
set_error_handler([$this, 'onErrorHandler']);
|
||||||
$this->dirs = Config::get('inotify', [APP_PATH]);
|
$this->dirs = Config::get('inotify', [APP_PATH]);
|
||||||
|
|
||||||
if (extension_loaded('inotify')) {
|
var_dump(extension_loaded('inotify'));
|
||||||
$this->inotify = inotify_init();
|
if (extension_loaded('inotify')) {
|
||||||
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
|
$this->inotify = inotify_init();
|
||||||
|
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
|
||||||
|
|
||||||
$this->watch(APP_PATH);
|
foreach ($this->dirs as $dir) {
|
||||||
Event::add($this->inotify, [$this, 'check']);
|
if (!is_dir($dir)) continue;
|
||||||
Event::wait();
|
$this->watch($dir);
|
||||||
} else {
|
}
|
||||||
$this->loadDirs();
|
Event::add($this->inotify, [$this, 'check']);
|
||||||
$this->tick();
|
Event::wait();
|
||||||
}
|
} else {
|
||||||
}
|
$this->loadDirs();
|
||||||
|
$this->tick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $isReload
|
* @param bool $isReload
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function loadDirs($isReload = false)
|
private function loadDirs($isReload = false)
|
||||||
{
|
{
|
||||||
foreach ($this->dirs as $value) {
|
foreach ($this->dirs as $value) {
|
||||||
if (is_bool($path = realpath($value))) {
|
if (is_bool($path = realpath($value))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_dir($path)) continue;
|
if (!is_dir($path)) continue;
|
||||||
|
|
||||||
$this->loadByDir($path, $isReload);
|
$this->loadByDir($path, $isReload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private array $md5Map = [];
|
private array $md5Map = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function tick()
|
public function tick()
|
||||||
{
|
{
|
||||||
if ($this->isReloading) {
|
if ($this->isReloading) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->loadDirs(true);
|
$this->loadDirs(true);
|
||||||
|
|
||||||
Timer::after(2000, [$this, 'tick']);
|
Timer::after(2000, [$this, 'tick']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param bool $isReload
|
* @param bool $isReload
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function loadByDir($path, $isReload = false): void
|
private function loadByDir($path, $isReload = false): void
|
||||||
{
|
{
|
||||||
if (!is_string($path)) {
|
if (!is_string($path)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$path = rtrim($path, '/');
|
$path = rtrim($path, '/');
|
||||||
foreach (glob(realpath($path) . '/*') as $value) {
|
foreach (glob(realpath($path) . '/*') as $value) {
|
||||||
if (is_dir($value)) {
|
if (is_dir($value)) {
|
||||||
$this->loadByDir($value, $isReload);
|
$this->loadByDir($value, $isReload);
|
||||||
}
|
}
|
||||||
if (is_file($value)) {
|
if (is_file($value)) {
|
||||||
if ($this->checkFile($value, $isReload)) {
|
if ($this->checkFile($value, $isReload)) {
|
||||||
$this->timerReload();
|
$this->timerReload();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @param $isReload
|
* @param $isReload
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function checkFile($value, $isReload): bool
|
private function checkFile($value, $isReload): bool
|
||||||
{
|
{
|
||||||
$md5 = md5($value);
|
$md5 = md5($value);
|
||||||
$mTime = filectime($value);
|
$mTime = filectime($value);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始监听
|
* 开始监听
|
||||||
*/
|
*/
|
||||||
public function check()
|
public function check()
|
||||||
{
|
{
|
||||||
if (!($events = inotify_read($this->inotify))) {
|
if (!($events = inotify_read($this->inotify))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->isReloading) {
|
if ($this->isReloading) {
|
||||||
if (!$this->isReloadingOut) {
|
if (!$this->isReloadingOut) {
|
||||||
$this->isReloadingOut = true;
|
$this->isReloadingOut = true;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$eventList = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
|
$eventList = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
|
||||||
foreach ($events as $ev) {
|
foreach ($events as $ev) {
|
||||||
if (empty($ev['name'])) {
|
if (empty($ev['name'])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($ev['mask'] == IN_IGNORED) {
|
if ($ev['mask'] == IN_IGNORED) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!in_array($ev['mask'], $eventList)) {
|
if (!in_array($ev['mask'], $eventList)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$fileType = strstr($ev['name'], '.');
|
$fileType = strstr($ev['name'], '.');
|
||||||
//非重启类型
|
//非重启类型
|
||||||
if ($fileType !== '.php') {
|
if ($fileType !== '.php') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($this->int !== -1) {
|
if ($this->int !== -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->int = @swoole_timer_after(2000, [$this, 'reload']);
|
$this->int = @swoole_timer_after(2000, [$this, 'reload']);
|
||||||
|
|
||||||
$this->isReloading = true;
|
$this->isReloading = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function reload()
|
public function reload()
|
||||||
{
|
{
|
||||||
$this->isReloading = true;
|
$this->isReloading = true;
|
||||||
$this->trigger_reload();
|
$this->trigger_reload();
|
||||||
|
|
||||||
$this->clearWatch();
|
$this->clearWatch();
|
||||||
$this->watch(APP_PATH);
|
foreach ($this->dirs as $root) {
|
||||||
|
$this->watch($root);
|
||||||
|
}
|
||||||
|
$this->int = -1;
|
||||||
|
$this->isReloading = FALSE;
|
||||||
|
$this->isReloadingOut = FALSE;
|
||||||
|
$this->md5Map = [];
|
||||||
|
}
|
||||||
|
|
||||||
$this->int = -1;
|
/**
|
||||||
$this->isReloading = FALSE;
|
* @throws Exception
|
||||||
$this->isReloadingOut = FALSE;
|
*/
|
||||||
$this->md5Map = [];
|
public function timerReload()
|
||||||
}
|
{
|
||||||
|
$this->isReloading = true;
|
||||||
|
$this->trigger_reload();
|
||||||
|
|
||||||
/**
|
$this->int = -1;
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function timerReload()
|
|
||||||
{
|
|
||||||
$this->isReloading = true;
|
|
||||||
$this->trigger_reload();
|
|
||||||
|
|
||||||
$this->int = -1;
|
$this->loadDirs();
|
||||||
|
|
||||||
$this->loadDirs();
|
$this->isReloading = FALSE;
|
||||||
|
$this->isReloadingOut = FALSE;
|
||||||
|
|
||||||
$this->isReloading = FALSE;
|
$this->tick();
|
||||||
$this->isReloadingOut = FALSE;
|
}
|
||||||
|
|
||||||
$this->tick();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重启
|
* 重启
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function trigger_reload()
|
public function trigger_reload()
|
||||||
{
|
{
|
||||||
logger()->debug('server reload.');
|
exec(PHP_BINARY . ' snowflake runtime:builder');
|
||||||
|
|
||||||
|
Snowflake::reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function clearWatch()
|
||||||
|
{
|
||||||
|
foreach ($this->watchFiles as $wd) {
|
||||||
|
try {
|
||||||
|
inotify_rm_watch($this->inotify, $wd);
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
logger()->addError($exception, 'throwable');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->watchFiles = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
exec(PHP_BINARY . ' snowflake runtime:builder');
|
/**
|
||||||
|
* @param $code
|
||||||
Snowflake::reload();
|
* @param $message
|
||||||
}
|
* @param $file
|
||||||
|
* @param $line
|
||||||
/**
|
* @throws Exception
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
protected function onErrorHandler($code, $message, $file, $line)
|
||||||
public function clearWatch()
|
{
|
||||||
{
|
if (str_contains($message, 'The file descriptor is not an inotify instance')) {
|
||||||
foreach ($this->watchFiles as $wd) {
|
return;
|
||||||
try {
|
}
|
||||||
inotify_rm_watch($this->inotify, $wd);
|
logger()->debug('Error:' . $message);
|
||||||
} catch (\Throwable $exception) {
|
logger()->debug($file . ':' . $line);
|
||||||
logger()->addError($exception, 'throwable');
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->watchFiles = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $code
|
* @param $dir
|
||||||
* @param $message
|
* @return bool
|
||||||
* @param $file
|
* @throws Exception
|
||||||
* @param $line
|
*/
|
||||||
* @throws Exception
|
public function watch($dir): bool
|
||||||
*/
|
{
|
||||||
protected function onErrorHandler($code, $message, $file, $line)
|
//目录不存在
|
||||||
{
|
if (!is_dir($dir)) {
|
||||||
if (str_contains($message, 'The file descriptor is not an inotify instance')) {
|
return logger()->addError("[$dir] is not a directory.");
|
||||||
return;
|
}
|
||||||
}
|
//避免重复监听
|
||||||
logger()->debug('Error:' . $message);
|
if (isset($this->watchFiles[$dir])) {
|
||||||
logger()->debug($file . ':' . $line);
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (in_array($dir, [APP_PATH . 'config', APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'])) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
|
||||||
* @param $dir
|
$this->watchFiles[$dir] = $wd;
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function watch($dir): bool
|
|
||||||
{
|
|
||||||
//目录不存在
|
|
||||||
if (!is_dir($dir)) {
|
|
||||||
return logger()->addError("[$dir] is not a directory.");
|
|
||||||
}
|
|
||||||
//避免重复监听
|
|
||||||
if (isset($this->watchFiles[$dir])) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (in_array($dir, [APP_PATH . 'config', APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'])) {
|
$files = scandir($dir);
|
||||||
return FALSE;
|
foreach ($files as $f) {
|
||||||
}
|
if ($f == '.' or $f == '..' or $f == 'runtime' or preg_match('/\.txt/', $f) or preg_match('/\.sql/', $f) or preg_match('/\.log/', $f)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$path = $dir . '/' . $f;
|
||||||
|
//递归目录
|
||||||
|
if (is_dir($path)) {
|
||||||
|
$this->watch($path);
|
||||||
|
}
|
||||||
|
|
||||||
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
|
//检测文件类型
|
||||||
$this->watchFiles[$dir] = $wd;
|
if (strstr($f, '.') == '.php') {
|
||||||
|
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
|
||||||
$files = scandir($dir);
|
$this->watchFiles[$path] = $wd;
|
||||||
foreach ($files as $f) {
|
}
|
||||||
if ($f == '.' or $f == '..' or $f == 'runtime' or preg_match('/\.txt/', $f) or preg_match('/\.sql/', $f) or preg_match('/\.log/', $f)) {
|
}
|
||||||
continue;
|
return TRUE;
|
||||||
}
|
}
|
||||||
$path = $dir . '/' . $f;
|
|
||||||
//递归目录
|
|
||||||
if (is_dir($path)) {
|
|
||||||
$this->watch($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
//检测文件类型
|
|
||||||
if (strstr($f, '.') == '.php') {
|
|
||||||
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
|
|
||||||
$this->watchFiles[$path] = $wd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user