diff --git a/Abstracts/HotReload.php b/Abstracts/HotReload.php index 5b213a7..98620a7 100644 --- a/Abstracts/HotReload.php +++ b/Abstracts/HotReload.php @@ -4,6 +4,9 @@ namespace Kiri\Server\Abstracts; use Kiri\Di\Inject\Container; use Kiri\Error\StdoutLogger; +use Kiri\Events\EventProvider; +use Kiri\Router\Router; +use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\ServerInterface; use Psr\Log\LoggerInterface; use Swoole\Event; @@ -51,6 +54,15 @@ class HotReload extends AbstractProcess protected bool $reloading = false; + /** + * @throws \Exception + */ + public function __construct() + { + di(EventProvider::class)->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']); + } + + /** * @return string */ @@ -78,10 +90,9 @@ class HotReload extends AbstractProcess $this->addListen(); Event::add($this->pipe, function () use ($process) { $read = inotify_read($this->pipe); - - var_dump($read); - - $this->reload(); + if (count($read) > 0) { + $this->reload(); + } }); Event::cycle(function (): void { if ($this->isStop()) { @@ -103,14 +114,10 @@ class HotReload extends AbstractProcess $this->reloading = true; $this->clear(); - di(ServerInterface::class)->reload(); - $this->addListen(); $this->reloading = false; - - var_dump(inotify_queue_len($this->pipe)); } @@ -147,25 +154,6 @@ class HotReload extends AbstractProcess } - /** - * @param int $directory - * @return void - */ - public function reWatch(int $directory): void - { - if (isset($this->watches[$directory])) { - inotify_rm_watch($this->pipe, $directory); - - $path = $this->watches[$directory]; - unset($this->watches[$directory]); - - $data = inotify_add_watch($this->pipe, $path, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE); - - $this->watches[$data] = $path; - } - } - - /** * @param string $directory * @return void diff --git a/ServerCommand.php b/ServerCommand.php index 6c4a361..3aa7840 100644 --- a/ServerCommand.php +++ b/ServerCommand.php @@ -142,9 +142,10 @@ class ServerCommand extends Command protected function start(InputInterface $input): int { $this->asyncServer->addProcess(config('processes', [])); - $this->eventProvider->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']); if (\config('reload.hot', false) === true) { $this->asyncServer->addProcess([HotReload::class]); + } else { + di(Router::class)->scan_build_route(); } $this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon')); $this->asyncServer->start();