From 01d0c0170394af4fa060ee148660953d5eb7763f Mon Sep 17 00:00:00 2001 From: whwyy Date: Mon, 20 Jul 2026 11:15:46 +0800 Subject: [PATCH] eee --- Abstracts/FileWatcher.php | 9 +++++-- ServerCommand.php | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Abstracts/FileWatcher.php b/Abstracts/FileWatcher.php index 5f623d4..c5f84b4 100644 --- a/Abstracts/FileWatcher.php +++ b/Abstracts/FileWatcher.php @@ -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; } } diff --git a/ServerCommand.php b/ServerCommand.php index 09936e9..980d40c 100644 --- a/ServerCommand.php +++ b/ServerCommand.php @@ -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; + } + }