This commit is contained in:
2020-09-07 11:34:57 +08:00
parent 36b3d573b5
commit 776aba0dbd
+25 -44
View File
@@ -10,6 +10,7 @@ namespace Snowflake\Process;
use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
use Swoole\Event;
use Swoole\Server;
@@ -28,6 +29,7 @@ class ServerInotify extends Process
private $watchFiles = [];
private $dirs = [];
private $events;
private $outListener = [APP_PATH . '/config', APP_PATH . '/commands', APP_PATH . '/.git', APP_PATH . '/.gitee'];
private $int = -1;
@@ -70,7 +72,7 @@ class ServerInotify extends Process
/**
* @param $path
* @param bool $isReload
* @return void
* @return array|void
* @throws Exception
*/
private function loadByDir($path, $isReload = false)
@@ -88,13 +90,15 @@ class ServerInotify extends Process
$mTime = filectime($value);
if (!isset($this->md5Map[$md5])) {
if ($isReload) {
return $this->reload();
$this->isReloading = true;
return [$this, 'reload'];
}
$this->md5Map[$md5] = $mTime;
} else {
if ($this->md5Map[$md5] != $mTime) {
if ($isReload) {
return $this->reload();
$this->isReloading = true;
return [$this, 'reload'];
}
$this->md5Map[$md5] = $mTime;
}
@@ -123,25 +127,19 @@ class ServerInotify extends Process
if (empty($ev['name'])) {
continue;
}
if ($ev['mask'] == IN_IGNORED) {
continue;
} else if (in_array($ev['mask'], $eventList)) {
$fileType = strstr($ev['name'], '.');
//非重启类型
if ($fileType !== '.php') {
continue;
}
} else {
if ($ev['mask'] == IN_IGNORED || !in_array($ev['mask'], $eventList)) {
continue;
}
try {
if ($this->int !== -1) {
return;
}
$this->int = @swoole_timer_after(2000, [$this, 'reload']);
} catch (Exception $exception) {
$fileType = strstr($ev['name'], '.');
//非重启类型
if ($fileType !== '.php') {
continue;
}
if ($this->int !== -1) {
return;
}
$this->int = @swoole_timer_after(2000, [$this, 'reload']);
$this->isReloading = true;
}
}
@@ -171,6 +169,7 @@ class ServerInotify extends Process
/**
* 重启
* @throws ComponentException
*/
public function trigger_reload()
{
@@ -184,11 +183,8 @@ class ServerInotify extends Process
*/
public function clearWatch()
{
try {
foreach ($this->watchFiles as $wd) {
@inotify_rm_watch($this->inotify, $wd);
}
} catch (Exception $exception) {
foreach ($this->watchFiles as $wd) {
@inotify_rm_watch($this->inotify, $wd);
}
$this->watchFiles = [];
}
@@ -202,29 +198,21 @@ class ServerInotify extends Process
*/
public function watch($dir, $root = TRUE)
{
//目录不存在
if (!is_dir($dir)) {
throw new Exception("[$dir] is not a directory.");
}
//避免重复监听
if (isset($this->watchFiles[$dir])) {
return FALSE;
}
//根目录
if (isset($this->watchFiles[$dir]) || in_array($dir, $this->outListener)) {
return FALSE;
}
if ($root) {
$this->dirs[] = $dir;
}
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);
$this->watchFiles[$dir] = $wd;
$files = scandir($dir);
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)) {
if ($f == '.' || $f == '..' || $f == 'runtime' || !preg_match('/\.php/', $f)) {
continue;
}
$path = $dir . '/' . $f;
@@ -233,15 +221,8 @@ class ServerInotify extends Process
$this->watch($path, FALSE);
}
//检测文件类型
$fileType = strstr($f, '.');
if ($fileType == '.php') {
try {
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd;
} catch (Exception $exception) {
}
}
$wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd;
}
return TRUE;
}