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
+7 -2
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;
}
}
+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;
}
}