diff --git a/Scaner.php b/Scaner.php new file mode 100644 index 0000000..7161466 --- /dev/null +++ b/Scaner.php @@ -0,0 +1,170 @@ +dirs = Config::get('reload.inotify', []); + + $this->loadDirs(); + $this->tick(); + } + + + /** + * @param bool $isReload + * @throws Exception + */ + private function loadDirs(bool $isReload = FALSE) + { + foreach ($this->dirs as $value) { + if (is_bool($path = realpath($value))) { + continue; + } + + if (!is_dir($path)) continue; + + $this->loadByDir($path, $isReload); + } + } + + + /** + * @param $path + * @param bool $isReload + * @return void + * @throws Exception + */ + private function loadByDir($path, bool $isReload = FALSE): void + { + if (!is_string($path)) { + return; + } + $path = rtrim($path, '/'); + foreach (glob(realpath($path) . '/*') as $value) { + if (is_dir($value)) { + $this->loadByDir($value, $isReload); + } + if (is_file($value)) { + if ($this->checkFile($value, $isReload)) { + if ($this->isReloading) { + break; + } + $this->isReloading = TRUE; + + sleep(2); + + $this->timerReload(); + break; + } + } + } + } + + + /** + * @param $value + * @param $isReload + * @return bool + */ + private function checkFile($value, $isReload): bool + { + $md5 = md5($value); + $mTime = filectime($value); + if (!isset($this->md5Map[$md5])) { + if ($isReload) { + return TRUE; + } + $this->md5Map[$md5] = $mTime; + } else { + if ($this->md5Map[$md5] != $mTime) { + if ($isReload) { + return TRUE; + } + $this->md5Map[$md5] = $mTime; + } + } + return FALSE; + } + + + /** + * @throws Exception + */ + public function timerReload() + { + $this->isReloading = TRUE; + + $this->logger->warning('file change'); + + $swow = \Kiri::getDi()->get(SwooleServerInterface::class); + + $swow->reload(); + + $this->loadDirs(); + + $this->isReloading = FALSE; + + $this->tick(); + } + + + /** + * @return $this + */ + public function onSigterm(): static + { + pcntl_signal(SIGTERM, function () { + $this->onProcessStop(); + }); + return $this; + } + + + /** + * @throws Exception + */ + public function tick() + { + if ($this->isStop) { + return; + } + + $this->loadDirs(TRUE); + + sleep(2); + + $this->tick(); + } + +} diff --git a/Server.php b/Server.php index 483c103..db1e6d1 100644 --- a/Server.php +++ b/Server.php @@ -117,11 +117,11 @@ class Server extends HttpService $reload = Config::get('reload.hot', false); if ($reload !== false) { - $this->eventProvider->on(OnWorkerStart::class, [$this->router, 'scan_build_route']); + $this->eventProvider->on(OnWorkerStart::class, [$this, 'onWorkerStart']); - $this->process[] = Inotify::class; + $this->process[] = Scaner::class; } else { - $this->router->scan_build_route(); + $this->onWorkerStart(); } $processes = array_merge($this->process, Config::get('processes', [])); @@ -134,6 +134,19 @@ class Server extends HttpService } + /** + * @return void + * @throws ReflectionException + * @throws Exception + */ + public function onWorkerStart(): void + { + scan_directory(MODEL_PATH, 'app\Model'); + + $this->router->scan_build_route(); + } + + /** * @return void * @throws ConfigException