2024-11-18 11:24:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Abstracts;
|
|
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2024-11-18 14:15:17 +08:00
|
|
|
use Kiri\Error\StdoutLogger;
|
2024-11-18 17:05:21 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
|
|
|
|
use Kiri\Router\Router;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerStart;
|
2024-11-18 11:24:08 +08:00
|
|
|
use Kiri\Server\ServerInterface;
|
2024-11-18 14:00:51 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
2024-11-18 14:13:47 +08:00
|
|
|
use Swoole\Event;
|
2024-11-18 11:24:08 +08:00
|
|
|
use Swoole\Process;
|
|
|
|
|
use Kiri\Server\Processes\AbstractProcess;
|
|
|
|
|
|
|
|
|
|
class HotReload extends AbstractProcess
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 11:32:40 +08:00
|
|
|
/**
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
2024-11-18 11:24:08 +08:00
|
|
|
protected mixed $pipe;
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
/**
|
2024-11-18 14:15:17 +08:00
|
|
|
* @var LoggerInterface|StdoutLogger
|
2024-11-18 14:00:51 +08:00
|
|
|
*/
|
|
|
|
|
#[Container(LoggerInterface::class)]
|
2024-11-18 14:15:17 +08:00
|
|
|
public StdoutLogger|LoggerInterface $logger;
|
2024-11-18 14:00:51 +08:00
|
|
|
|
|
|
|
|
|
2024-11-18 11:24:08 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $enable_coroutine = false;
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 12:19:26 +08:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected array $watches = [];
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 11:24:08 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $enable_queue = false;
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 11:32:40 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2024-11-18 11:24:08 +08:00
|
|
|
protected bool $reloading = false;
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 17:05:21 +08:00
|
|
|
/**
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
di(EventProvider::class)->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 11:24:08 +08:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName(): string
|
|
|
|
|
{
|
2024-11-18 17:15:22 +08:00
|
|
|
return 'hotReload';
|
2024-11-18 11:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function onSigterm(): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement onSigterm() method.
|
2024-11-18 11:43:08 +08:00
|
|
|
$this->stop();
|
2024-11-18 11:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ?Process $process
|
|
|
|
|
*/
|
|
|
|
|
public function process(Process|null $process): void
|
|
|
|
|
{
|
2024-11-18 16:42:28 +08:00
|
|
|
$this->pipe = inotify_init();
|
2024-11-18 14:00:51 +08:00
|
|
|
$this->addListen();
|
2024-11-18 14:19:31 +08:00
|
|
|
Event::add($this->pipe, function () use ($process) {
|
2024-11-18 14:34:42 +08:00
|
|
|
$read = inotify_read($this->pipe);
|
2024-11-18 17:05:21 +08:00
|
|
|
if (count($read) > 0) {
|
|
|
|
|
$this->reload();
|
|
|
|
|
}
|
2024-11-18 14:19:31 +08:00
|
|
|
});
|
2024-11-18 14:13:47 +08:00
|
|
|
Event::cycle(function (): void {
|
|
|
|
|
if ($this->isStop()) {
|
2024-11-18 14:14:15 +08:00
|
|
|
Event::exit();
|
2024-11-18 12:21:33 +08:00
|
|
|
}
|
2024-11-18 14:13:47 +08:00
|
|
|
});
|
|
|
|
|
Event::wait();
|
2024-11-18 11:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function reload(): void
|
|
|
|
|
{
|
2024-11-18 11:50:13 +08:00
|
|
|
if ($this->reloading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-18 11:24:08 +08:00
|
|
|
$this->reloading = true;
|
|
|
|
|
|
2024-11-18 17:32:49 +08:00
|
|
|
di(StdoutLogger::class)->println('reloading server[' . \config('id', 'system-service') . '], please waite.');
|
2024-11-18 17:07:57 +08:00
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
$this->clear();
|
2024-11-18 14:16:03 +08:00
|
|
|
di(ServerInterface::class)->reload();
|
2024-11-18 14:00:51 +08:00
|
|
|
$this->addListen();
|
2024-11-18 11:32:40 +08:00
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
$this->reloading = false;
|
|
|
|
|
}
|
2024-11-18 11:24:08 +08:00
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function addListen(): void
|
|
|
|
|
{
|
2024-11-18 13:53:50 +08:00
|
|
|
foreach (config('reload.listen') as $value) {
|
|
|
|
|
$this->readDirectory($value);
|
|
|
|
|
}
|
2024-11-18 11:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-11-18 14:00:51 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function clear(): void
|
|
|
|
|
{
|
2024-11-18 16:05:10 +08:00
|
|
|
$this->watches = [];
|
2024-11-18 14:00:51 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-18 11:24:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $directory
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function readFile(string $directory): void
|
|
|
|
|
{
|
|
|
|
|
if (str_ends_with($directory, '.php') === true) {
|
2024-11-18 12:19:26 +08:00
|
|
|
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
|
|
|
|
|
2024-11-18 16:50:22 +08:00
|
|
|
$this->watches[] = $wd;
|
2024-11-18 11:24:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $directory
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function readDirectory(string $directory): void
|
|
|
|
|
{
|
|
|
|
|
foreach (glob($directory . '/*') as $data) {
|
|
|
|
|
if (is_dir($data)) {
|
2024-11-18 12:07:24 +08:00
|
|
|
$this->readDirectory($data);
|
2024-11-18 11:24:08 +08:00
|
|
|
} else {
|
|
|
|
|
$this->readFile($data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|