diff --git a/Abstracts/ReloadWorkers.php b/Abstracts/ReloadWorkers.php index b56b58a..4daba24 100644 --- a/Abstracts/ReloadWorkers.php +++ b/Abstracts/ReloadWorkers.php @@ -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) {