Files
kiri-http-server/HotReload.php
T

370 lines
8.1 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;
2024-09-03 15:05:18 +08:00
use Kiri;
2023-04-18 22:20:20 +08:00
use Kiri\Di\Context;
2023-08-11 02:18:04 +08:00
use Kiri\Router\Router;
use Kiri\Server\Events\OnWorkerStart;
2024-09-03 15:05:18 +08:00
use Kiri\Server\Processes\AbstractProcess;
2023-04-18 22:20:20 +08:00
use Swoole\Event;
use Swoole\Process;
2024-09-03 15:05:18 +08:00
class HotReload extends AbstractProcess
2023-04-18 22:20:20 +08:00
{
2023-07-06 16:00:02 +08:00
/**
* @var array|mixed
*/
private array $watchFiles = [];
2023-07-06 16:53:53 +08:00
private array $md5Map = [];
2023-07-06 16:00:02 +08:00
/**
* @var array|string[]
*/
2023-07-06 16:53:53 +08:00
private array $dirs = [APP_PATH . 'app', APP_PATH . 'routes'];
2023-07-06 16:00:02 +08:00
2023-12-18 03:18:26 +08:00
private bool $forceFile = false;
2023-07-26 17:45:07 +08:00
protected mixed $inotify = null;
2023-07-10 10:24:01 +08:00
2023-08-11 02:18:04 +08:00
/**
* @param Router $router
*/
2024-08-29 17:01:07 +08:00
public function __construct(Router $router)
2023-08-11 02:18:04 +08:00
{
2024-08-29 17:01:07 +08:00
on(OnWorkerStart::class, [$router, 'scan_build_route']);
2023-12-18 03:19:11 +08:00
$this->forceFile = \config('reload.forceFile', false);
2023-08-11 02:18:04 +08:00
}
2023-07-06 16:00:02 +08:00
/**
* @return string
*/
public function getName(): string
{
2023-12-12 10:56:42 +08:00
// TODO: Change the autogenerated stub
2024-08-29 18:06:58 +08:00
return 'hot.load';
2023-07-06 16:00:02 +08:00
}
/**
2024-09-04 10:14:29 +08:00
* @return void
2023-07-06 16:00:02 +08:00
*/
2024-09-04 10:14:29 +08:00
public function onSigterm(): void
2023-07-06 16:00:02 +08:00
{
2023-07-10 10:36:45 +08:00
}
2023-07-10 10:34:41 +08:00
2023-07-10 10:36:45 +08:00
/**
* @param $data
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-07-10 10:36:45 +08:00
*/
public function onStop($data): void
{
2023-07-26 17:40:31 +08:00
$this->clearWatch();
2023-07-26 17:43:19 +08:00
if (is_resource($this->inotify)) {
Event::del($this->inotify);
}
2023-07-26 17:40:31 +08:00
2023-07-10 10:36:45 +08:00
$this->onShutdown($data);
2023-07-06 16:00:02 +08:00
}
/**
* @param Process|null $process
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
public function process(?Process $process): void
{
// TODO: Implement process() method.
2023-12-18 03:18:26 +08:00
if (!$this->forceFile && extension_loaded('inotify')) {
2023-07-06 16:00:02 +08:00
$this->onInotifyReload();
} else {
$this->onCrontabReload();
}
2023-07-26 17:40:31 +08:00
$process->exit(0);
2023-07-06 16:00:02 +08:00
}
/**
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
private function onCrontabReload(): void
{
$this->loadDirs();
$this->tick();
}
/**
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
private function onInotifyReload(): void
{
2023-07-10 10:24:01 +08:00
$this->inotify = inotify_init();
2023-07-06 16:00:02 +08:00
foreach ($this->dirs as $dir) {
if (!is_dir($dir)) {
continue;
}
2023-07-10 10:24:01 +08:00
$this->watch(rtrim($dir, '/'));
2023-07-06 16:00:02 +08:00
}
2024-08-29 17:01:07 +08:00
Event::add($this->inotify, fn () => $this->check());
Event::cycle(fn () => function () {
2023-07-26 17:40:31 +08:00
Event::dispatch();
2023-07-06 16:00:02 +08:00
}, true);
2023-07-10 02:22:38 +08:00
Event::wait();
2023-07-06 16:00:02 +08:00
}
/**
* @param bool $isReload
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
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);
}
}
/**
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
public function tick(): void
{
$isReloading = Context::get('isReloading', false);
if ($isReloading) {
return;
}
$pid = (int)file_get_contents(storage('.swoole.pid'));
if ($pid <= 0 || !Process::kill($pid, 0) || $this->isStop()) {
return;
}
$this->loadDirs(true);
sleep(2);
$this->tick();
}
/**
* @param $path
* @param bool $isReload
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
private function loadByDir($path, bool $isReload = false): void
{
2023-12-19 14:53:07 +08:00
if (!is_string($path) || $this->isStop()) {
2023-07-06 16:00:02 +08:00
return;
}
$path = rtrim($path, '/');
foreach (glob(realpath($path) . '/*') as $value) {
2023-12-19 14:53:07 +08:00
if ($this->isStop()) {
break;
}
2023-07-06 16:00:02 +08:00
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
{
2023-12-12 10:56:42 +08:00
$md5 = md5($value);
2023-07-06 16:00:02 +08:00
$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-07-10 10:24:01 +08:00
public function check(): void
2023-07-06 16:00:02 +08:00
{
2023-07-10 10:24:01 +08:00
if (!($events = inotify_read($this->inotify))) {
2023-07-06 16:00:02 +08:00
return;
}
2023-07-06 16:53:53 +08:00
if (Context::exists('isReloading')) {
2023-07-06 16:00:02 +08:00
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;
}
2023-07-06 16:53:53 +08:00
if (Context::exists('swoole_timer_after')) {
2023-07-06 16:00:02 +08:00
return;
}
2024-08-29 17:01:07 +08:00
$int = @swoole_timer_after(2000, fn () => $this->reload());
2023-07-06 16:00:02 +08:00
Context::set('swoole_timer_after', $int);
Context::set('isReloading', true);
}
}
/**
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
2023-07-10 10:24:01 +08:00
public function reload(): void
2023-07-06 16:00:02 +08:00
{
$this->trigger_reload();
2023-07-10 10:24:01 +08:00
$this->clearWatch();
2023-07-06 16:00:02 +08:00
foreach ($this->dirs as $root) {
2023-07-10 10:24:01 +08:00
$this->watch($root);
2023-07-06 16:00:02 +08:00
}
2023-07-06 16:53:53 +08:00
Context::remove('swoole_timer_after');
Context::remove('isReloading');
2023-07-06 16:00:02 +08:00
$this->md5Map = [];
}
/**
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
public function timerReload(): void
{
Context::set('isReloading', true);
$this->trigger_reload();
Context::set('swoole_timer_after', -1);
$this->loadDirs();
Context::set('isReloading', false);
$this->tick();
}
/**
* 重启
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
public function trigger_reload(): void
{
2024-09-03 15:05:18 +08:00
Kiri::getLogger()->failure('Wait trigger server Reload' . PHP_EOL);
2023-07-06 16:00:02 +08:00
di(ServerInterface::class)->reload(false);
}
/**
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
2023-07-10 10:24:01 +08:00
public function clearWatch(): void
2023-07-06 16:00:02 +08:00
{
foreach ($this->watchFiles as $wd) {
try {
2023-07-10 10:24:01 +08:00
inotify_rm_watch($this->inotify, $wd);
2023-07-06 16:00:02 +08:00
} catch (\Throwable $exception) {
2023-07-31 23:08:58 +08:00
trigger_print_error($exception, 'throwable');
2023-07-06 16:00:02 +08:00
}
}
$this->watchFiles = [];
}
/**
* @param $dir
* @return bool
2023-12-12 15:35:34 +08:00
* @throws
2023-07-06 16:00:02 +08:00
*/
2023-07-10 10:24:01 +08:00
public function watch($dir): bool
2023-07-06 16:00:02 +08:00
{
//目录不存在
if (!is_dir($dir)) {
2023-07-31 23:08:58 +08:00
return trigger_print_error("[$dir] is not a directory.");
2023-07-06 16:00:02 +08:00
}
//避免重复监听
if (isset($this->watchFiles[$dir])) {
return FALSE;
}
if (in_array($dir, [APP_PATH . 'commands', APP_PATH . '.git', APP_PATH . '.gitee'])) {
return FALSE;
}
2023-12-12 10:56:42 +08:00
$wd = @inotify_add_watch($this->inotify, $dir, IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE);
2023-07-06 16:00:02 +08:00
$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)) {
2023-07-10 10:24:01 +08:00
$this->watch($path);
2023-07-06 16:53:53 +08:00
continue;
2023-07-06 16:00:02 +08:00
}
//检测文件类型
if (strstr($f, '.') == '.php') {
2023-12-12 10:56:42 +08:00
$wd = @inotify_add_watch($this->inotify, $path, IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE);
2023-07-06 16:00:02 +08:00
$this->watchFiles[$path] = $wd;
}
}
return TRUE;
}
2023-04-18 22:20:20 +08:00
}