This commit is contained in:
xl
2024-11-18 14:00:51 +08:00
parent 7f93ab046b
commit f6a00d89c1
+43 -13
View File
@@ -2,7 +2,9 @@
namespace Kiri\Server\Abstracts; namespace Kiri\Server\Abstracts;
use Kiri\Di\Inject\Container;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Psr\Log\LoggerInterface;
use Swoole\Process; use Swoole\Process;
use Kiri\Server\Processes\AbstractProcess; use Kiri\Server\Processes\AbstractProcess;
@@ -16,6 +18,20 @@ class HotReload extends AbstractProcess
protected mixed $pipe; protected mixed $pipe;
/**
* @var LoggerInterface
*/
#[Container(LoggerInterface::class)]
public LoggerInterface $logger;
/**
* @var ServerInterface
*/
#[Container(ServerInterface::class)]
public ServerInterface $server;
/** /**
* @var bool * @var bool
*/ */
@@ -69,10 +85,7 @@ class HotReload extends AbstractProcess
*/ */
public function process(Process|null $process): void public function process(Process|null $process): void
{ {
$this->pipe = inotify_init(); $this->addListen();
foreach (config('reload.listen') as $value) {
$this->readDirectory($value);
}
while (!$this->isStop()) { while (!$this->isStop()) {
$read = inotify_read($this->pipe); $read = inotify_read($this->pipe);
if (!empty($read)) { if (!empty($read)) {
@@ -91,23 +104,40 @@ class HotReload extends AbstractProcess
return; return;
} }
$this->reloading = true; $this->reloading = true;
$this->logger->error('reloading server, please waite.');
foreach ($this->watches as $key => $watch) { $this->clear();
inotify_rm_watch($this->pipe, $key);
}
\Kiri::getLogger()->info('reloading server, please waite.'); $this->server->reload();
di(ServerInterface::class)->reload(); $this->addListen();
foreach (config('reload.listen') as $value) {
$this->readDirectory($value);
}
$this->reloading = false; $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 * @param string $directory
* @return void * @return void