This commit is contained in:
2026-07-20 11:15:46 +08:00
parent 276ea63b54
commit 01d0c01703
2 changed files with 62 additions and 2 deletions
+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;
}
}