Compare commits

...
5 Commits
Author SHA1 Message Date
as2252258 01d0c01703 eee 2026-07-20 11:15:46 +08:00
as2252258 276ea63b54 eee 2026-07-15 09:54:34 +08:00
as2252258 39bdb9f5c9 eee 2026-07-15 09:52:12 +08:00
as2252258 136f360fec eee 2026-07-15 09:42:08 +08:00
as2252258 6ad9d5d0da eee 2026-07-14 22:53:12 +08:00
4 changed files with 115 additions and 10 deletions
+8 -3
View File
@@ -63,7 +63,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
public function __construct()
{
$this->watchPaths = config('servers.reload.listen', []);
$this->excludePatterns = config('servers.reload.scan.skip_patterns', []);
$this->excludePatterns = array_values(array_unique(array_merge(
['/vendor/', '/tests/', '/cache/', '/node_modules/', '/storage/', '/.git/', '/.idea/'],
config('servers.reload.scan.skip_patterns', [])
)));
$this->extensions = config('servers.reload.scan.extensions', ['php']);
di(EventProvider::class)->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
@@ -237,8 +240,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
private function isExcluded(string $path): bool
{
$path = '/' . trim(str_replace('\\', '/', $path), '/') . '/';
foreach ($this->excludePatterns as $pattern) {
if (strpos($path, $pattern) !== false) {
$pattern = '/' . trim(str_replace('\\', '/', (string)$pattern), '/') . '/';
if ($pattern !== '//' && strpos($path, $pattern) !== false) {
return true;
}
}
@@ -307,7 +312,7 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
}
if (!$this->reloadWorkers()) {
di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed');
di(StdoutLogger::class)->println('reload server failed: Server->reload() failed, and master pid fallback not found or signal failed');
}
} finally {
$this->reloading = false;
+1 -1
View File
@@ -128,7 +128,7 @@ class HotReload extends AbstractProcess
$this->clear();
if (!$this->reloadWorkers()) {
di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed');
di(StdoutLogger::class)->println('reload server failed: Server->reload() failed, and master pid fallback not found or signal failed');
}
$this->addListen();
+51 -6
View File
@@ -2,6 +2,8 @@
namespace Kiri\Server\Abstracts;
use Kiri\Error\StdoutLogger;
use Kiri\Server\ServerInterface;
use Swoole\Process;
trait ReloadWorkers
@@ -14,6 +16,10 @@ trait ReloadWorkers
*/
private function reloadWorkers(): bool
{
if ($this->reloadByServerInstance()) {
return true;
}
$pid = $this->getMasterPid();
if ($pid === null) {
return false;
@@ -22,6 +28,18 @@ trait ReloadWorkers
return Process::kill($pid, SIGUSR1);
}
private function reloadByServerInstance(): bool
{
try {
di(StdoutLogger::class)->println('[file-build] Reload worker.');
$server = di(ServerInterface::class) ;
return $server->reload() !== false;
} catch (\Throwable) {
return false;
}
}
private function getMasterPid(): ?int
{
if (self::$cachedMasterPid !== null && $this->isCachedMasterPid(self::$cachedMasterPid)) {
@@ -29,14 +47,15 @@ trait ReloadWorkers
}
self::$cachedMasterPid = null;
$pidFilePid = $this->getPidFileMasterPid();
if ($pidFilePid !== null) {
self::$cachedMasterPid = $pidFilePid;
return $pidFilePid;
}
$processes = $this->listProcesses();
$candidates = [];
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
if (is_file($pidFile)) {
$candidates[] = (int)trim((string)file_get_contents($pidFile));
}
foreach ($processes as $process) {
if ($this->isMasterProcessCommand($process['command'])) {
$candidates[] = (int)$process['pid'];
@@ -62,10 +81,36 @@ trait ReloadWorkers
return false;
}
$pidFilePid = $this->readPidFile();
if ($pidFilePid === $pid && $this->isSameAppProcess($pid)) {
return true;
}
$command = $this->readProcessCommand($pid);
return $command !== '' && $this->isMasterProcessCommand($command) && $this->isSameAppProcess($pid);
}
private function getPidFileMasterPid(): ?int
{
$pid = $this->readPidFile();
if ($pid === null || !$this->isProcessAlive($pid) || !$this->isSameAppProcess($pid)) {
return null;
}
return $pid;
}
private function readPidFile(): ?int
{
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
if ($pidFile === '' || !is_file($pidFile)) {
return null;
}
$pid = (int)trim((string)file_get_contents($pidFile));
return $pid > 1 ? $pid : null;
}
private function readProcessCommand(int $pid): string
{
$cmdline = @file_get_contents('/proc/' . $pid . '/cmdline');
@@ -89,7 +134,7 @@ trait ReloadWorkers
private function listProcesses(): array
{
$lines = [];
exec('ps -eo pid=,ppid=,args=', $lines);
exec('ps -eo pid=,ppid=,args= 2>/dev/null', $lines);
$processes = [];
foreach ($lines as $line) {
+55
View File
@@ -134,6 +134,9 @@ class ServerCommand extends Command
{
$this->asyncServer->addProcess(config('process', []));
$hotReload = \config('servers.reload.hot', false) === true;
if ($hotReload && !$this->buildHotReloadArtifacts()) {
throw new Exception('Hot reload artifact build failed.');
}
if ($hotReload) {
$this->asyncServer->addProcess([FileWatcher::class]);
}
@@ -147,4 +150,56 @@ class ServerCommand extends Command
return 1;
}
/**
* 热更模式下不要在 Master 进程扫描业务类;先用独立 CLI 进程构建路由 artifact
* Worker 启动/重载时直接导入 artifact,避免多个 Worker 同时全量扫描导致 OOM。
* @return bool
*/
private function buildHotReloadArtifacts(): bool
{
$entry = $this->detectConsoleEntry();
if ($entry === null) {
\Kiri::getLogger()->println('hot reload build failed: entry script not found');
return false;
}
$php = PHP_BINARY ?: 'php';
$command = escapeshellarg($php) . ' ' . escapeshellarg($entry) . ' sw:file-build';
$output = [];
$returnCode = 0;
exec($command . ' 2>&1', $output, $returnCode);
foreach ($output as $line) {
if ($line !== '') {
\Kiri::getLogger()->println('[file-build] ' . $line);
}
}
return $returnCode === 0;
}
/**
* @return string|null
*/
private function detectConsoleEntry(): ?string
{
$candidates = [
$_SERVER['SCRIPT_FILENAME'] ?? '',
defined('APP_PATH') ? APP_PATH . 'kiri.php' : '',
defined('APP_PATH') ? APP_PATH . 'bin/kiri' : '',
defined('APP_PATH') ? APP_PATH . 'artisan' : '',
];
foreach ($candidates as $candidate) {
if (is_string($candidate) && $candidate !== '' && is_file($candidate)) {
return $candidate;
}
}
return null;
}
}