This commit is contained in:
2026-07-14 22:53:12 +08:00
parent d3ca24c4c5
commit 6ad9d5d0da
+33 -6
View File
@@ -29,14 +29,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 +63,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 +116,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) {