Compare commits
5
Commits
d3ca24c4c5
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01d0c01703 | ||
|
|
276ea63b54 | ||
|
|
39bdb9f5c9 | ||
|
|
136f360fec | ||
|
|
6ad9d5d0da |
@@ -63,7 +63,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->watchPaths = config('servers.reload.listen', []);
|
$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']);
|
$this->extensions = config('servers.reload.scan.extensions', ['php']);
|
||||||
|
|
||||||
di(EventProvider::class)->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
|
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
|
private function isExcluded(string $path): bool
|
||||||
{
|
{
|
||||||
|
$path = '/' . trim(str_replace('\\', '/', $path), '/') . '/';
|
||||||
foreach ($this->excludePatterns as $pattern) {
|
foreach ($this->excludePatterns as $pattern) {
|
||||||
if (strpos($path, $pattern) !== false) {
|
$pattern = '/' . trim(str_replace('\\', '/', (string)$pattern), '/') . '/';
|
||||||
|
if ($pattern !== '//' && strpos($path, $pattern) !== false) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -307,7 +312,7 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->reloadWorkers()) {
|
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 {
|
} finally {
|
||||||
$this->reloading = false;
|
$this->reloading = false;
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ class HotReload extends AbstractProcess
|
|||||||
|
|
||||||
$this->clear();
|
$this->clear();
|
||||||
if (!$this->reloadWorkers()) {
|
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();
|
$this->addListen();
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Kiri\Server\Abstracts;
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
|
use Kiri\Error\StdoutLogger;
|
||||||
|
use Kiri\Server\ServerInterface;
|
||||||
use Swoole\Process;
|
use Swoole\Process;
|
||||||
|
|
||||||
trait ReloadWorkers
|
trait ReloadWorkers
|
||||||
@@ -14,6 +16,10 @@ trait ReloadWorkers
|
|||||||
*/
|
*/
|
||||||
private function reloadWorkers(): bool
|
private function reloadWorkers(): bool
|
||||||
{
|
{
|
||||||
|
if ($this->reloadByServerInstance()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$pid = $this->getMasterPid();
|
$pid = $this->getMasterPid();
|
||||||
if ($pid === null) {
|
if ($pid === null) {
|
||||||
return false;
|
return false;
|
||||||
@@ -22,6 +28,18 @@ trait ReloadWorkers
|
|||||||
return Process::kill($pid, SIGUSR1);
|
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
|
private function getMasterPid(): ?int
|
||||||
{
|
{
|
||||||
if (self::$cachedMasterPid !== null && $this->isCachedMasterPid(self::$cachedMasterPid)) {
|
if (self::$cachedMasterPid !== null && $this->isCachedMasterPid(self::$cachedMasterPid)) {
|
||||||
@@ -29,14 +47,15 @@ trait ReloadWorkers
|
|||||||
}
|
}
|
||||||
|
|
||||||
self::$cachedMasterPid = null;
|
self::$cachedMasterPid = null;
|
||||||
|
|
||||||
|
$pidFilePid = $this->getPidFileMasterPid();
|
||||||
|
if ($pidFilePid !== null) {
|
||||||
|
self::$cachedMasterPid = $pidFilePid;
|
||||||
|
return $pidFilePid;
|
||||||
|
}
|
||||||
$processes = $this->listProcesses();
|
$processes = $this->listProcesses();
|
||||||
$candidates = [];
|
$candidates = [];
|
||||||
|
|
||||||
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
|
|
||||||
if (is_file($pidFile)) {
|
|
||||||
$candidates[] = (int)trim((string)file_get_contents($pidFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($processes as $process) {
|
foreach ($processes as $process) {
|
||||||
if ($this->isMasterProcessCommand($process['command'])) {
|
if ($this->isMasterProcessCommand($process['command'])) {
|
||||||
$candidates[] = (int)$process['pid'];
|
$candidates[] = (int)$process['pid'];
|
||||||
@@ -62,10 +81,36 @@ trait ReloadWorkers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pidFilePid = $this->readPidFile();
|
||||||
|
if ($pidFilePid === $pid && $this->isSameAppProcess($pid)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$command = $this->readProcessCommand($pid);
|
$command = $this->readProcessCommand($pid);
|
||||||
return $command !== '' && $this->isMasterProcessCommand($command) && $this->isSameAppProcess($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
|
private function readProcessCommand(int $pid): string
|
||||||
{
|
{
|
||||||
$cmdline = @file_get_contents('/proc/' . $pid . '/cmdline');
|
$cmdline = @file_get_contents('/proc/' . $pid . '/cmdline');
|
||||||
@@ -89,7 +134,7 @@ trait ReloadWorkers
|
|||||||
private function listProcesses(): array
|
private function listProcesses(): array
|
||||||
{
|
{
|
||||||
$lines = [];
|
$lines = [];
|
||||||
exec('ps -eo pid=,ppid=,args=', $lines);
|
exec('ps -eo pid=,ppid=,args= 2>/dev/null', $lines);
|
||||||
|
|
||||||
$processes = [];
|
$processes = [];
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
|
|||||||
@@ -134,6 +134,9 @@ class ServerCommand extends Command
|
|||||||
{
|
{
|
||||||
$this->asyncServer->addProcess(config('process', []));
|
$this->asyncServer->addProcess(config('process', []));
|
||||||
$hotReload = \config('servers.reload.hot', false) === true;
|
$hotReload = \config('servers.reload.hot', false) === true;
|
||||||
|
if ($hotReload && !$this->buildHotReloadArtifacts()) {
|
||||||
|
throw new Exception('Hot reload artifact build failed.');
|
||||||
|
}
|
||||||
if ($hotReload) {
|
if ($hotReload) {
|
||||||
$this->asyncServer->addProcess([FileWatcher::class]);
|
$this->asyncServer->addProcess([FileWatcher::class]);
|
||||||
}
|
}
|
||||||
@@ -147,4 +150,56 @@ class ServerCommand extends Command
|
|||||||
return 1;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user