From def499c2f68b99390a6da15cebd372cb96edc953 Mon Sep 17 00:00:00 2001 From: xl Date: Tue, 3 May 2022 06:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-engine/Application.php | 19 +-- kiri-engine/FileListen/HotReload.php | 230 --------------------------- kiri-engine/FileListen/Inotify.php | 178 --------------------- kiri-engine/FileListen/Scaner.php | 159 ------------------ 4 files changed, 3 insertions(+), 583 deletions(-) delete mode 100644 kiri-engine/FileListen/HotReload.php delete mode 100644 kiri-engine/FileListen/Inotify.php delete mode 100644 kiri-engine/FileListen/Scaner.php diff --git a/kiri-engine/Application.php b/kiri-engine/Application.php index 1051fcd8..8ae147d0 100644 --- a/kiri-engine/Application.php +++ b/kiri-engine/Application.php @@ -17,7 +17,6 @@ use Kiri; use Kiri\Abstracts\{BaseApplication, Config, Kernel}; use Kiri\Crontab\CrontabProviders; use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute}; -use Kiri\FileListen\HotReload; use Kiri\Server\ServerProviders; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -110,18 +109,6 @@ class Application extends BaseApplication } - /** - * @throws - */ - public function withFileChangeListen() - { - $container = Kiri::getDi(); - - $console = $container->get(ConsoleApplication::class); - $console->add($container->get(HotReload::class)); - } - - /** * @param Closure|array $closure * @return $this @@ -239,9 +226,9 @@ class Application extends BaseApplication $this->container->setBindings(OutputInterface::class, $output); - if (!($class instanceof HotReload)) { - scan_directory(MODEL_PATH, 'app\Model'); - } + if (!($class instanceof Kiri\Server\ServerCommand)) { + scan_directory(MODEL_PATH,'app\Model'); + } $class->run($input, $output); fire(new OnAfterCommandExecute()); diff --git a/kiri-engine/FileListen/HotReload.php b/kiri-engine/FileListen/HotReload.php deleted file mode 100644 index 5f33eac7..00000000 --- a/kiri-engine/FileListen/HotReload.php +++ /dev/null @@ -1,230 +0,0 @@ -setName('sw:wather')->setDescription('server start'); - - $this->logger = Kiri::getDi()->get(StdoutLoggerInterface::class); - } - - - /** - * @throws ConfigException - * @throws Exception - */ - protected function initCore() - { - set_error_handler([$this, 'errorHandler']); - $this->dirs = Config::get('inotify', [APP_PATH . 'app']); - - $container = Kiri::getDi(); - if (!extension_loaded('inotify')) { - $this->driver = $container->make(Scaner::class, [$this->dirs, $this, $this->logger]); - } else { - $this->driver = $container->make(Inotify::class, [$this->dirs, $this, $this->logger]); - } - $this->clearOtherService(); - $this->setProcessName(); - } - - - /** - * @throws ConfigException - */ - public function setProcessName() - { - swoole_async_set(['enable_coroutine' => FALSE]); - if (Kiri::getPlatform()->isLinux()) { - swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather'); - } - } - - - /** - * @throws Exception - */ - public function clearOtherService() - { - if (file_exists(storage('.manager.pid'))) { - $pid = (int)file_get_contents(storage('.manager.pid')); - if ($pid > 0 && Process::kill($pid, 0)) { - Process::kill($pid, 15) && Process::wait(TRUE); - } - } - file_put_contents(storage('.manager.pid'), getmypid()); - } - - - /** - * @throws Exception - */ - public function errorHandler() - { - $error = func_get_args(); - $path = ['file' => $error[2], 'line' => $error[3]]; - - if ($error[0] === 0) { - $error[0] = 500; - } - $data = Json::to($error[0], $error[1], $path); - - $this->logger->error('error', [$data]); - } - - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - * @throws ConfigException - * @throws Exception - */ - public function execute(InputInterface $input, OutputInterface $output): int - { - $this->initCore(); - - $this->trigger_reload(); - Timer::tick(1000, fn() => $this->healthCheck()); - - pcntl_signal(SIGTERM, function () { - $this->onSignal(func_get_args()); - }); - $this->driver->start(); - return 0; - } - - - /** - * @throws Exception - */ - public function healthCheck() - { - $pid = (int)file_get_contents(storage('.swoole.pid')); - if ($this->int == 1) { - return; - } - if (empty($pid)) { - $this->logger->warning('service is shutdown you need reload.'); - $this->trigger_reload(); - } else if (!Process::kill($pid, 0)) { - $this->logger->warning('service is shutdown you need reload.'); - $this->trigger_reload(); - } - } - - - /** - * @param $data - * @throws Exception - */ - public function onSignal($data) - { - if (!$data) { - return; - } - Timer::clearAll(); - $this->driver->clear(); - $this->stopServer(); - while ($ret = Process::wait(TRUE)) { - echo "PID={$ret['pid']}\n"; - sleep(1); - } - } - - - /** - * @throws Exception - */ - protected function stopServer() - { - $pid = file_get_contents(storage('.swoole.pid')); - if (!empty($pid) && Process::kill($pid, 0)) { - Process::kill($pid, SIGTERM); - } - if ($this->process && Process::kill($this->process->pid, 0)) { - Process::kill($this->process->pid) && Process::wait(TRUE); - } - } - - - /** - * 重启 - * - * @throws Exception - */ - public function trigger_reload(string $path = '') - { - if ($this->int == 1) { - return; - } - $this->int = 1; - $this->logger->warning('restart'); - - $this->stopServer(); - $this->process = new Process(function (Process $process) { - $config = Config::get('scanner', []); - scan_directory(MODEL_PATH, 'app\Model'); - if (is_array($config)) foreach ($config as $key => $value) { - scan_directory($value, $key); - } - Kiri::app()->getServer()->start(); - }); - $this->process->start(); - - sleep(3); - - $this->int = -1; - } - - -} diff --git a/kiri-engine/FileListen/Inotify.php b/kiri-engine/FileListen/Inotify.php deleted file mode 100644 index 861ebb1b..00000000 --- a/kiri-engine/FileListen/Inotify.php +++ /dev/null @@ -1,178 +0,0 @@ -inotify = inotify_init(); - $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE; - foreach ($this->dirs as $dir) { - if (!is_dir($dir)) continue; - $this->watch($dir); - } - $this->process->int = -1; - Event::add($this->inotify, [$this, 'check']); - Event::wait(); - } - - - public function clear() - { - Event::del($this->inotify); - Event::exit(); - } - - - /** - * 开始监听 - * @throws Exception - */ - public function check() - { - if (!($events = inotify_read($this->inotify))) { - return; - } - if ($this->isReloading) { - return; - } - - $LISTEN_TYPE = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM]; - foreach ($events as $ev) { - if (!in_array($ev['mask'], $LISTEN_TYPE)) { - continue; - } - - $search = array_search($ev['wd'], $this->watchFiles); - - //非重启类型 - if (str_ends_with($ev['name'], '.php')) { - if ($this->isReloading) { - break; - } - Timer::after(3000, fn() => $this->reload($search)); - $this->isReloading = TRUE; - } - } - } - - /** - * @throws Exception - */ - public function reload($path) - { - $this->logger->warning('file change'); - - $this->process->trigger_reload($path); - $this->process->int = -1; - - $this->clearWatch(); - foreach ($this->dirs as $root) { - $this->watch($root); - } - $this->isReloading = FALSE; - } - - - /** - * @throws Exception - */ - public function clearWatch() - { - foreach ($this->watchFiles as $wd) { - @inotify_rm_watch($this->inotify, $wd); - } - $this->watchFiles = []; - } - - - /** - * @param $dir - * @return bool - * @throws Exception - */ - public function watch($dir): bool - { - //目录不存在 - if (!is_dir($dir)) { - return $this->logger->addError("[$dir] is not a directory."); - } - //避免重复监听 - if (isset($this->watchFiles[$dir])) { - return FALSE; - } - - if (in_array($dir, self::IG_DIR)) { - return FALSE; - } - - $wd = @inotify_add_watch($this->inotify, $dir, $this->events); - $this->watchFiles[$dir] = $wd; - - $files = scandir($dir); - foreach ($files as $f) { - if ($f == '.' || $f == '..') { - continue; - } - $path = $dir . '/' . $f; - //递归目录 - if (is_dir($path)) { - $this->watch($path); - } else if (!str_ends_with($f, '.php')) { - continue; - } - //检测文件类型 - if (strstr($f, '.') == '.php') { - $wd = @inotify_add_watch($this->inotify, $path, $this->events); - $this->watchFiles[$path] = $wd; - } - } - return TRUE; - } -} diff --git a/kiri-engine/FileListen/Scaner.php b/kiri-engine/FileListen/Scaner.php deleted file mode 100644 index 659194e2..00000000 --- a/kiri-engine/FileListen/Scaner.php +++ /dev/null @@ -1,159 +0,0 @@ -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($value); - 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($path) - { - $this->isReloading = TRUE; - - $this->logger->warning('file change'); - - $this->process->trigger_reload($path); - - $this->loadDirs(); - - $this->process->int = -1; - - $this->isReloading = FALSE; - $this->process->isReloadingOut = FALSE; - - $this->tick(); - } - - - private bool $isStop = FALSE; - - public function clear() - { - $this->isStop = TRUE; - } - - - /** - * @throws Exception - */ - public function tick() - { - if ($this->isReloading || $this->isStop) { - return; - } - - $this->loadDirs(TRUE); - - sleep(2); - - $this->tick(); - } - -}