From 15e8d7a5a7444e27db760056317a5fd2bfda296a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 6 Jul 2021 16:25:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Shutdown.php | 64 ++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/HttpServer/Shutdown.php b/HttpServer/Shutdown.php index 012efb71..18f57811 100644 --- a/HttpServer/Shutdown.php +++ b/HttpServer/Shutdown.php @@ -6,7 +6,6 @@ namespace HttpServer; use Exception; use Snowflake\Abstracts\Component; -use Snowflake\Exception\ConfigException; /** @@ -51,7 +50,7 @@ class Shutdown extends Component $master_pid = Server()->setting['pid_file'] ?? PID_PATH; if (file_exists($master_pid)) { - $this->close($master_pid); + $this->close(file_get_contents($master_pid)); } $this->closeOther(); } @@ -109,33 +108,64 @@ class Shutdown extends Component */ public function directoryCheck(string $path): bool { - $dir = new \DirectoryIterator($path); - if ($dir->getSize() < 1) { - return true; - } - foreach ($dir as $value) { - if ($value->isDot()) continue; + $values = $this->getProcessPidS($path); + if (empty($values)) return false; - if (!$value->valid()) continue; - - $this->close($value->getRealPath()); + $diff = array_diff($values, $this->getPidS()); + foreach ($diff as $value) { + $this->pidIsExists($value); } return false; } + /** + * @param $path + * @return array|bool + */ + private function getProcessPidS($path): bool|array + { + $values = []; + $dir = new \DirectoryIterator($path); + if ($dir->getSize() < 1) { + return $values; + } + foreach ($dir as $value) { + if ($value->isDot()) continue; + + if (!$value->valid()) continue; + + $_value = file_get_contents($value->getRealPath()); + if (empty($_value)) { + continue; + } + $values[] = intval($_value); + } + return $values; + } + + + /** + * @return array + */ + private function getPidS(): array + { + exec('ps -eo pid', $output); + return array_filter($output, function ($value) { + return intval($value); + }); + } + + /** * @param string $value */ - public function close(string $value) + public function close(mixed $value) { - $content = file_get_contents($value); - - while ($this->pidIsExists($content)) { - exec('kill -15 ' . $content); + while ($this->pidIsExists($value)) { + exec('kill -15 ' . $value); usleep(100); } - clearstatcache($value); if (file_exists($value)) { @unlink($value);