This commit is contained in:
2020-09-07 11:46:18 +08:00
parent c446c6dfbe
commit 6a1d3a022f
+25 -14
View File
@@ -29,7 +29,6 @@ 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;
@@ -65,14 +64,13 @@ class ServerInotify extends Process
public function tick() public function tick()
{ {
$this->loadByDir(APP_PATH . 'app', true); $this->loadByDir(APP_PATH . 'app', true);
$this->loadByDir(APP_PATH . 'routes', true);
} }
/** /**
* @param $path * @param $path
* @param bool $isReload * @param bool $isReload
* @return array|void * @return void
* @throws Exception * @throws Exception
*/ */
private function loadByDir($path, $isReload = false) private function loadByDir($path, $isReload = false)
@@ -90,15 +88,13 @@ class ServerInotify extends Process
$mTime = filectime($value); $mTime = filectime($value);
if (!isset($this->md5Map[$md5])) { if (!isset($this->md5Map[$md5])) {
if ($isReload) { if ($isReload) {
$this->isReloading = true; return Timer::after(2000, [$this, 'reload']);
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) {
$this->isReloading = true; return Timer::after(2000, [$this, 'reload']);
return [$this, 'reload'];
} }
$this->md5Map[$md5] = $mTime; $this->md5Map[$md5] = $mTime;
} }
@@ -127,18 +123,22 @@ class ServerInotify extends Process
if (empty($ev['name'])) { if (empty($ev['name'])) {
continue; continue;
} }
if ($ev['mask'] == IN_IGNORED || !in_array($ev['mask'], $eventList)) { if ($ev['mask'] == IN_IGNORED) {
continue; continue;
} }
if (!in_array($ev['mask'], $eventList)) {
continue;
}
$fileType = strstr($ev['name'], '.');
//非重启类型 //非重启类型
if (strstr($ev['name'], '.') !== '.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;
} }
} }
@@ -197,21 +197,29 @@ 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 == '.' || $f == '..' || $f == 'runtime' || !preg_match('/.*\.php/', $f)) { if ($f == '.' or $f == '..' or $f == 'runtime' or preg_match('/\.txt/', $f) or preg_match('/\.sql/', $f) or preg_match('/\.log/', $f)) {
continue; continue;
} }
$path = $dir . '/' . $f; $path = $dir . '/' . $f;
@@ -220,9 +228,12 @@ class ServerInotify extends Process
$this->watch($path, FALSE); $this->watch($path, FALSE);
} }
//检测文件类型
if (strstr($f, '.') == '.php') {
$wd = @inotify_add_watch($this->inotify, $path, $this->events); $wd = @inotify_add_watch($this->inotify, $path, $this->events);
$this->watchFiles[$path] = $wd; $this->watchFiles[$path] = $wd;
} }
}
return TRUE; return TRUE;
} }
} }