Files
kiri-http-server/Abstracts/HotReload.php
T

153 lines
2.9 KiB
PHP
Raw Normal View History

2024-11-18 11:24:08 +08:00
<?php
namespace Kiri\Server\Abstracts;
use Kiri\Server\ServerInterface;
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;
/**
* @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;
/**
* @return string
*/
public function getName(): string
{
2024-11-18 11:38:27 +08:00
return 'hotReload.process';
2024-11-18 11:24:08 +08:00
}
/**
* @return void
*/
public function onSigterm(): void
{
// TODO: Implement onSigterm() method.
2024-11-18 12:06:53 +08:00
fclose($this->pipe);
2024-11-18 11:43:08 +08:00
2024-11-18 11:46:39 +08:00
var_dump('ddddddddddddddd');
2024-11-18 11:43:08 +08:00
$this->stop();
2024-11-18 11:46:39 +08:00
var_dump('ddddddddddddddd1111111111111111111111');
2024-11-18 11:24:08 +08:00
}
/**
* @param ?Process $process
*/
public function process(Process|null $process): void
{
$this->pipe = inotify_init();
2024-11-18 12:22:49 +08:00
2024-11-18 12:19:26 +08:00
foreach (config('reload.listen') as $value) {
2024-11-18 12:22:49 +08:00
$this->readDirectory($value);
2024-11-18 11:27:05 +08:00
}
2024-11-18 11:24:08 +08:00
while (!$this->isStop()) {
$read = inotify_read($this->pipe);
2024-11-18 12:22:49 +08:00
foreach ($read as $item) {
2024-11-18 13:40:23 +08:00
$this->reWatch($this->watches[$item['wd']]);
2024-11-18 12:21:33 +08:00
}
2024-11-18 12:22:49 +08:00
$this->reload();
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 11:32:40 +08:00
\Kiri::getLogger()->info('reloading server, please waite.');
2024-11-18 11:24:08 +08:00
di(ServerInterface::class)->reload();
$this->reloading = false;
}
/**
* @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);
$this->watches[$wd] = $directory;
2024-11-18 11:24:08 +08:00
}
}
/**
2024-11-18 13:40:23 +08:00
* @param int $directory
2024-11-18 11:24:08 +08:00
* @return void
*/
2024-11-18 12:23:58 +08:00
public function reWatch(int $directory): void
2024-11-18 11:24:08 +08:00
{
2024-11-18 12:23:58 +08:00
if (isset($this->watches[$directory])) {
2024-11-18 11:24:08 +08:00
inotify_rm_watch($this->pipe, $directory);
2024-11-18 12:23:58 +08:00
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
2024-11-18 13:40:23 +08:00
var_dump($wd);
2024-11-18 12:23:58 +08:00
$this->watches[$wd] = $directory;
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);
}
}
}
}