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