From 776aba0dbd665c91a9bd1d74b081e8b36a3d79d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 7 Sep 2020 11:34:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Process/ServerInotify.php | 69 ++++++++++++-------------------- 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/System/Process/ServerInotify.php b/System/Process/ServerInotify.php index 98588967..fedf8a69 100644 --- a/System/Process/ServerInotify.php +++ b/System/Process/ServerInotify.php @@ -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; }