Files
kiri-http-server/HotReload.php
T

380 lines
6.9 KiB
PHP
Raw Normal View History

2023-04-18 22:20:20 +08:00
<?php
2023-04-19 14:29:33 +08:00
declare(strict_types=1);
2023-04-18 22:20:20 +08:00
namespace Kiri\Server;
use Exception;
use Kiri\Di\Context;
use Swoole\Event;
use Swoole\Process;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HotReload extends Command
{
public string $name = 'hot:reload';
private array $md5Map = [];
/**
* @var Process
*/
2023-04-19 13:04:17 +08:00
private mixed $process;
2023-04-18 22:20:20 +08:00
/**
* @var array|mixed
*/
private array $watchFiles = [];
2023-04-19 14:29:22 +08:00
private bool $isExit = false;
2023-04-18 22:20:20 +08:00
/**
* @return void
*/
protected function configure()
{
$this->setName('hot:load');
parent::configure(); // TODO: Change the autogenerated stub
}
2023-04-18 23:48:42 +08:00
private array $dirs = [APP_PATH . 'app/', APP_PATH . 'config/', APP_PATH . 'routes/'];
2023-04-18 22:20:20 +08:00
/**
* @param InputInterface $input
* @param OutputInterface $output
2023-04-19 14:29:22 +08:00
* @return int
2023-04-18 22:20:20 +08:00
* @throws Exception
*/
2023-04-19 14:29:22 +08:00
protected function execute(InputInterface $input, OutputInterface $output): int
2023-04-18 22:20:20 +08:00
{
2023-04-19 13:28:55 +08:00
$this->startProcess();
2023-04-19 14:37:25 +08:00
Process::signal(SIGINT, fn() => $this->exit());
Process::signal(SIGTERM, fn() => $this->exit());
2023-04-19 14:29:22 +08:00
sleep(3);
2023-04-19 15:12:34 +08:00
// if (extension_loaded('inotify')) {
// $this->onInotifyReload();
// } else {
$this->onCrontabReload();
// }
2023-04-19 14:29:22 +08:00
return 1;
2023-04-18 22:20:20 +08:00
}
2023-04-19 14:32:42 +08:00
/**
* @return void
* @throws Exception
*/
public function exit(): void
{
$this->stopProcess();
2023-04-19 14:37:25 +08:00
$this->isExit = true;
2023-04-19 14:32:42 +08:00
}
2023-04-18 22:20:20 +08:00
/**
* @return void
*/
private function startProcess(): void
{
2023-04-19 14:41:32 +08:00
$this->process = proc_open([PHP_BINARY, APP_PATH . 'kiri.php', 'sw:server', 'restart'], [], $pipes);
2023-04-18 22:20:20 +08:00
}
2023-04-19 13:15:21 +08:00
/**
* @return void
* @throws Exception
*/
private function stopProcess(): void
{
if (!is_resource($this->process)) {
return;
}
$pid = (int)file_get_contents(storage('.swoole.pid'));
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGTERM);
}
proc_close($this->process);
$this->process = null;
}
2023-04-18 22:20:20 +08:00
/**
* @return void
* @throws Exception
*/
private function onCrontabReload(): void
{
$this->loadDirs();
$this->tick();
}
/**
* @return void
* @throws Exception
*/
private function onInotifyReload(): void
{
$init = inotify_init();
2023-04-19 15:12:57 +08:00
foreach ($this->dirs as $dir) {
2023-04-18 22:20:20 +08:00
if (!is_dir($dir)) {
continue;
}
$this->watch($init, $dir);
}
Event::add($init, fn() => $this->check($init));
2023-04-19 15:05:28 +08:00
Event::cycle(function () use ($init) {
$pid = (int)file_get_contents(storage('.swoole.pid'));
if ($pid <= 0 || !Process::kill($pid, 0)) {
Event::del($init);
}
}, true);
2023-04-18 22:20:20 +08:00
Event::wait();
}
/**
* @param bool $isReload
* @throws Exception
*/
private function loadDirs(bool $isReload = false): void
{
foreach ($this->dirs as $value) {
if (is_bool($path = realpath($value))) {
continue;
}
if (!is_dir($path)) continue;
$this->loadByDir($path, $isReload);
}
}
/**
* @throws Exception
*/
public function tick(): void
{
$isReloading = Context::get('isReloading', false);
if ($isReloading) {
return;
}
2023-04-19 14:29:22 +08:00
$pid = (int)file_get_contents(storage('.swoole.pid'));
if ($pid <= 0 || !Process::kill($pid, 0)) {
return;
}
2023-04-18 22:20:20 +08:00
$this->loadDirs(true);
sleep(2);
$this->tick();
}
/**
* @param $path
* @param bool $isReload
* @return void
* @throws Exception
*/
2023-04-19 14:29:22 +08:00
private function loadByDir($path, bool $isReload = false): void
2023-04-18 22:20:20 +08:00
{
if (!is_string($path)) {
return;
}
$path = rtrim($path, '/');
foreach (glob(realpath($path) . '/*') as $value) {
if (is_dir($value)) {
$this->loadByDir($value, $isReload);
}
if (is_file($value)) {
if ($this->checkFile($value, $isReload)) {
$this->timerReload();
break;
}
}
}
}
/**
* @param $value
* @param $isReload
* @return bool
*/
private function checkFile($value, $isReload): bool
{
$md5 = md5($value);
$mTime = filectime($value);
if (!isset($this->md5Map[$md5])) {
if ($isReload) {
return true;
}
$this->md5Map[$md5] = $mTime;
} else {
if ($this->md5Map[$md5] != $mTime) {
if ($isReload) {
return true;
}
$this->md5Map[$md5] = $mTime;
}
}
return false;
}
/**
* 开始监听
*/
2023-04-19 14:29:22 +08:00
public function check($inotify): void
2023-04-18 22:20:20 +08:00
{
if (!($events = inotify_read($inotify))) {
return;
}
$isReloading = Context::get('isReloading', false);
if ($isReloading) {
return;
}
$eventList = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM];
foreach ($events as $ev) {
if (empty($ev['name'])) {
continue;
}
if ($ev['mask'] == IN_IGNORED) {
continue;
}
if (!in_array($ev['mask'], $eventList)) {
continue;
}
$fileType = strstr($ev['name'], '.');
//非重启类型
if ($fileType !== '.php') {
continue;
}
if (Context::get('swoole_timer_after') !== -1) {
return;
}
$int = @swoole_timer_after(2000, fn() => $this->reload($inotify));
Context::set('swoole_timer_after', $int);
Context::set('isReloading', true);
}
}
/**
* @throws Exception
*/
public function reload($inotify): void
{
Context::set('isReloading', true);
$this->trigger_reload();
$this->clearWatch($inotify);
foreach ($this->dirs as $root) {
$this->watch($inotify, $root);
}
Context::set('swoole_timer_after', -1);
Context::set('isReloading', false);
$this->md5Map = [];
}
/**
* @throws Exception
*/
2023-04-19 14:41:32 +08:00
public function timerReload(): void
2023-04-18 22:20:20 +08:00
{
Context::set('isReloading', true);
$this->trigger_reload();
Context::set('swoole_timer_after', -1);
$this->loadDirs();
Context::set('isReloading', false);
$this->tick();
}
/**
* 重启
* @throws Exception
*/
public function trigger_reload(): void
{
2023-04-18 23:48:42 +08:00
echo 'tigger server Reload' . PHP_EOL;
2023-04-19 13:15:21 +08:00
$this->stopProcess();
2023-04-18 22:20:20 +08:00
$this->startProcess();
}
/**
* @throws Exception
*/
2023-04-19 13:04:17 +08:00
public function clearWatch($inotify): void
2023-04-18 22:20:20 +08:00
{
foreach ($this->watchFiles as $wd) {
try {
inotify_rm_watch($inotify, $wd);
} catch (\Throwable $exception) {
2023-04-18 23:48:42 +08:00
logger()->addError($exception, 'throwable');
2023-04-18 22:20:20 +08:00
}
}
$this->watchFiles = [];
}
/**
* @param $inotify
* @param $dir
* @return bool
* @throws Exception
*/
public function watch($inotify, $dir): bool
{
//目录不存在
if (!is_dir($dir)) {
return logger()->addError("[$dir] is not a directory.");
}
//避免重复监听
if (isset($this->watchFiles[$dir])) {
return FALSE;
}
if (in_array($dir, [APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'])) {
return FALSE;
}
$wd = @inotify_add_watch($inotify, $dir, IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE);
$this->watchFiles[$dir] = $wd;
$files = scandir($dir);
foreach ($files as $f) {
if ($f == '.' or $f == '..' or $f == 'runtime' or preg_match('/\.txt/', $f) or preg_match('/\.sql/', $f) or preg_match('/\.log/', $f)) {
continue;
}
$path = $dir . '/' . $f;
//递归目录
if (is_dir($path)) {
$this->watch($inotify, $path);
}
//检测文件类型
if (strstr($f, '.') == '.php') {
$wd = @inotify_add_watch($inotify, $path, IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE);
$this->watchFiles[$path] = $wd;
}
}
return TRUE;
}
}