diff --git a/Abstracts/HotReload.php b/Abstracts/HotReload.php index 46586e3..694aa42 100644 --- a/Abstracts/HotReload.php +++ b/Abstracts/HotReload.php @@ -2,7 +2,9 @@ namespace Kiri\Server\Abstracts; +use Kiri\Di\Inject\Container; use Kiri\Server\ServerInterface; +use Psr\Log\LoggerInterface; use Swoole\Process; use Kiri\Server\Processes\AbstractProcess; @@ -16,6 +18,20 @@ class HotReload extends AbstractProcess protected mixed $pipe; + /** + * @var LoggerInterface + */ + #[Container(LoggerInterface::class)] + public LoggerInterface $logger; + + + /** + * @var ServerInterface + */ + #[Container(ServerInterface::class)] + public ServerInterface $server; + + /** * @var bool */ @@ -69,10 +85,7 @@ class HotReload extends AbstractProcess */ public function process(Process|null $process): void { - $this->pipe = inotify_init(); - foreach (config('reload.listen') as $value) { - $this->readDirectory($value); - } + $this->addListen(); while (!$this->isStop()) { $read = inotify_read($this->pipe); if (!empty($read)) { @@ -91,23 +104,40 @@ class HotReload extends AbstractProcess return; } $this->reloading = true; + $this->logger->error('reloading server, please waite.'); - foreach ($this->watches as $key => $watch) { - inotify_rm_watch($this->pipe, $key); - } + $this->clear(); - \Kiri::getLogger()->info('reloading server, please waite.'); + $this->server->reload(); - di(ServerInterface::class)->reload(); - - foreach (config('reload.listen') as $value) { - $this->readDirectory($value); - } + $this->addListen(); $this->reloading = false; } + /** + * @return void + */ + protected function addListen(): void + { + $this->pipe = inotify_init(); + foreach (config('reload.listen') as $value) { + $this->readDirectory($value); + } + } + + + /** + * @return void + */ + protected function clear(): void + { + foreach ($this->watches as $key => $watch) { + inotify_rm_watch($this->pipe, $key); + } + } + /** * @param string $directory * @return void