153 lines
2.9 KiB
PHP
153 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Kiri\Server\Abstracts;
|
|
|
|
use Kiri\Server\ServerInterface;
|
|
use Swoole\Process;
|
|
use Kiri\Server\Processes\AbstractProcess;
|
|
|
|
class HotReload extends AbstractProcess
|
|
{
|
|
|
|
|
|
/**
|
|
* @var mixed
|
|
*/
|
|
protected mixed $pipe;
|
|
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected bool $enable_coroutine = false;
|
|
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected array $watches = [];
|
|
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected bool $enable_queue = false;
|
|
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected bool $reloading = false;
|
|
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getName(): string
|
|
{
|
|
return 'hotReload.process';
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function onSigterm(): void
|
|
{
|
|
// TODO: Implement onSigterm() method.
|
|
fclose($this->pipe);
|
|
|
|
var_dump('ddddddddddddddd');
|
|
|
|
$this->stop();
|
|
|
|
var_dump('ddddddddddddddd1111111111111111111111');
|
|
}
|
|
|
|
/**
|
|
* @param ?Process $process
|
|
*/
|
|
public function process(Process|null $process): void
|
|
{
|
|
$this->pipe = inotify_init();
|
|
|
|
foreach (config('reload.listen') as $value) {
|
|
$this->readDirectory($value);
|
|
}
|
|
while (!$this->isStop()) {
|
|
$read = inotify_read($this->pipe);
|
|
|
|
foreach ($read as $item) {
|
|
$this->reWatch($this->watches[$item['wd']]);
|
|
}
|
|
|
|
$this->reload();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function reload(): void
|
|
{
|
|
if ($this->reloading) {
|
|
return;
|
|
}
|
|
$this->reloading = true;
|
|
|
|
\Kiri::getLogger()->info('reloading server, please waite.');
|
|
|
|
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) {
|
|
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
|
|
|
$this->watches[$wd] = $directory;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param int $directory
|
|
* @return void
|
|
*/
|
|
public function reWatch(int $directory): void
|
|
{
|
|
if (isset($this->watches[$directory])) {
|
|
inotify_rm_watch($this->pipe, $directory);
|
|
|
|
$wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
|
|
|
var_dump($wd);
|
|
|
|
$this->watches[$wd] = $directory;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $directory
|
|
* @return void
|
|
*/
|
|
public function readDirectory(string $directory): void
|
|
{
|
|
foreach (glob($directory . '/*') as $data) {
|
|
if (is_dir($data)) {
|
|
$this->readDirectory($data);
|
|
} else {
|
|
$this->readFile($data);
|
|
}
|
|
}
|
|
}
|
|
} |