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

229 lines
4.4 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
*/
namespace Snowflake\Process;
use Exception;
2020-09-07 11:34:57 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
use Swoole\Event;
2020-09-03 01:22:41 +08:00
use Swoole\Server;
2020-08-31 01:27:08 +08:00
use Swoole\Timer;
use swoole_process;
/**
* 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
{
private $inotify;
private $isReloading = false;
private $isReloadingOut = false;
private $watchFiles = [];
private $dirs = [];
private $events;
2020-09-07 11:34:57 +08:00
private $outListener = [APP_PATH . '/config', APP_PATH . '/commands', APP_PATH . '/.git', APP_PATH . '/.gitee'];
2020-08-31 01:27:08 +08:00
private $int = -1;
/**
* @param \Swoole\Process $process
* @throws Exception
*/
public function onHandler(\Swoole\Process $process)
{
Snowflake::setProcessId($process->pid);
if (extension_loaded('inotify')) {
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
$process->name('event: file change.');
$this->watch(APP_PATH);
Event::add($this->inotify, [$this, 'check']);
Event::wait();
} else {
2020-09-03 01:32:05 +08:00
$this->loadByDir(APP_PATH . 'app');
2020-09-07 11:29:17 +08:00
$this->loadByDir(APP_PATH . 'routes');
2020-09-03 01:57:10 +08:00
Timer::tick(2000, [$this, 'tick']);
2020-08-31 01:27:08 +08:00
}
}
2020-09-03 01:28:55 +08:00
private $md5Map = [];
2020-09-03 01:21:09 +08:00
/**
* @throws Exception
*/
2020-08-31 01:27:08 +08:00
public function tick()
{
2020-09-03 01:34:21 +08:00
$this->loadByDir(APP_PATH . 'app', true);
2020-09-07 11:29:17 +08:00
$this->loadByDir(APP_PATH . 'routes', true);
2020-09-03 01:28:55 +08:00
}
/**
* @param $path
2020-09-03 01:34:21 +08:00
* @param bool $isReload
2020-09-07 11:34:57 +08:00
* @return array|void
2020-09-03 01:28:55 +08:00
* @throws Exception
*/
2020-09-03 01:34:21 +08:00
private function loadByDir($path, $isReload = false)
2020-09-03 01:28:55 +08:00
{
$path = rtrim($path, '/');
2020-09-03 01:37:02 +08:00
if ($this->isReloading) {
return;
}
2020-09-03 01:28:55 +08:00
foreach (glob($path . '/*') as $value) {
if (is_dir($value)) {
2020-09-03 01:35:39 +08:00
$this->loadByDir($value, $isReload);
2020-09-03 01:32:44 +08:00
continue;
2020-09-03 01:28:55 +08:00
}
2020-09-03 01:32:44 +08:00
$md5 = md5($value);
2020-09-03 01:35:26 +08:00
$mTime = filectime($value);
2020-09-03 01:28:55 +08:00
if (!isset($this->md5Map[$md5])) {
2020-09-03 01:34:21 +08:00
if ($isReload) {
2020-09-07 11:34:57 +08:00
$this->isReloading = true;
return [$this, 'reload'];
2020-09-03 01:34:21 +08:00
}
2020-09-03 01:35:26 +08:00
$this->md5Map[$md5] = $mTime;
2020-09-03 01:34:21 +08:00
} else {
if ($this->md5Map[$md5] != $mTime) {
if ($isReload) {
2020-09-07 11:34:57 +08:00
$this->isReloading = true;
return [$this, 'reload'];
2020-09-03 01:34:21 +08:00
}
2020-09-03 01:35:26 +08:00
$this->md5Map[$md5] = $mTime;
2020-09-03 01:34:21 +08:00
}
2020-09-03 01:28:55 +08:00
}
}
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:34:57 +08:00
if ($ev['mask'] == IN_IGNORED || !in_array($ev['mask'], $eventList)) {
2020-08-31 01:27:08 +08:00
continue;
}
2020-09-07 11:34:57 +08:00
//非重启类型
2020-09-07 11:43:02 +08:00
if (strstr($ev['name'], '.') !== '.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-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();
$this->clearWatch();
//重新监听
foreach ($this->dirs as $root) {
$this->watch($root);
}
$this->int = -1;
$this->isReloading = FALSE;
$this->isReloadingOut = FALSE;
2020-09-03 01:37:02 +08:00
$this->loadByDir(APP_PATH . 'app');
2020-08-31 01:27:08 +08:00
}
/**
* 重启
2020-09-07 11:34:57 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
public function trigger_reload()
{
2020-09-03 01:22:41 +08:00
/** @var Server $server */
2020-09-03 11:39:20 +08:00
$server = Snowflake::app()->get('server')->getServer();
2020-09-03 01:22:41 +08:00
$server->reload();
2020-08-31 01:27:08 +08:00
}
/**
* 清理所有inotify监听
*/
public function clearWatch()
{
2020-09-07 11:34:57 +08:00
foreach ($this->watchFiles as $wd) {
@inotify_rm_watch($this->inotify, $wd);
2020-08-31 01:27:08 +08:00
}
$this->watchFiles = [];
}
/**
* @param $dir
* @param bool $root
* @return bool
* @throws Exception
*/
public function watch($dir, $root = TRUE)
{
if (!is_dir($dir)) {
2020-09-07 11:34:57 +08:00
return FALSE;
2020-08-31 01:27:08 +08:00
}
2020-09-07 11:34:57 +08:00
if (isset($this->watchFiles[$dir]) || in_array($dir, $this->outListener)) {
2020-08-31 01:27:08 +08:00
return FALSE;
}
if ($root) {
$this->dirs[] = $dir;
}
$wd = @inotify_add_watch($this->inotify, $dir, $this->events);
$this->watchFiles[$dir] = $wd;
$files = scandir($dir);
foreach ($files as $f) {
2020-09-07 11:43:02 +08:00
if ($f == '.' || $f == '..' || $f == 'runtime' || !preg_match('/.*\.php/', $f)) {
2020-08-31 01:27:08 +08:00
continue;
}
$path = $dir . '/' . $f;
//递归目录
if (is_dir($path)) {
$this->watch($path, FALSE);
}
2020-09-07 11:34:57 +08:00
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd;
2020-08-31 01:27:08 +08:00
}
return TRUE;
}
}