From ef9a10d7ffe1481518ec45f0212dbfb481a12e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 29 Mar 2021 10:44:36 +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 | 204 ++++++++++++++++++++-------------------- 1 file changed, 104 insertions(+), 100 deletions(-) diff --git a/HttpServer/Shutdown.php b/HttpServer/Shutdown.php index d7e16672..9f951da8 100644 --- a/HttpServer/Shutdown.php +++ b/HttpServer/Shutdown.php @@ -16,127 +16,131 @@ class Shutdown extends Component { - private string $taskDirectory; - private string $workerDirectory; - private string $managerDirectory; - private string $processDirectory; + private string $taskDirectory; + private string $workerDirectory; + private string $managerDirectory; + private string $processDirectory; - public function init() - { - $this->taskDirectory = storage(null, 'pid/task'); - $this->workerDirectory = storage(null, 'pid/worker'); - $this->managerDirectory = storage(null, 'pid/manager'); - $this->processDirectory = storage(null, 'pid/process'); - } + /** + * @throws Exception + */ + public function init() + { + $this->taskDirectory = storage(null, 'pid/task'); + $this->workerDirectory = storage(null, 'pid/worker'); + $this->managerDirectory = storage(null, 'pid/manager'); + $this->processDirectory = storage(null, 'pid/process'); + } - /** - * @throws Exception - */ - public function shutdown(): void - { - clearstatcache(storage()); - exec('ls -alh /.dockerenv', $output, $cod); - if ($cod === 0 && !empty($output)) { - return; - } + /** + * @throws Exception + */ + public function shutdown(): void + { + clearstatcache(storage()); + exec('ls -alh /.dockerenv', $output, $cod); + if ($cod === 0 && !empty($output)) { + return; + } - $master_pid = Server()->setting['pid_file'] ?? PID_PATH; - if (file_exists($master_pid)) { - $this->close($master_pid); - } - $this->closeOther(); - } + $master_pid = Server()->setting['pid_file'] ?? PID_PATH; + if (file_exists($master_pid)) { + $this->close($master_pid); + } + $this->closeOther(); + } - /** - * 关闭其他进程 - */ - private function closeOther(): void - { - $this->directoryCheck($this->managerDirectory); - $this->directoryCheck($this->taskDirectory); - $this->directoryCheck($this->workerDirectory); - $this->directoryCheck($this->processDirectory); - } + /** + * 关闭其他进程 + */ + private function closeOther(): void + { + $this->directoryCheck($this->managerDirectory); + $this->directoryCheck($this->taskDirectory); + $this->directoryCheck($this->workerDirectory); + $this->directoryCheck($this->processDirectory); + } - /** - * @return bool - * @throws Exception - * check server is running. - */ - public function isRunning() - { - $master_pid = Server()->setting['pid_file'] ?? PID_PATH; + /** + * @return bool + * @throws Exception + * check server is running. + */ + public function isRunning() + { + $master_pid = Server()->setting['pid_file'] ?? PID_PATH; - if (!file_exists($master_pid)) { - return false; - } + if (!file_exists($master_pid)) { + return false; + } - return $this->pidIsExists($master_pid); - } + return $this->pidIsExists($master_pid); + } - /** - * @param $content - * @return bool - */ - public function pidIsExists($content): bool - { - if (intval($content) < 1) { - return false; - } - $shell = 'ps -eo pid,cmd,state | grep %d | grep -v grep'; - exec(sprintf($shell, $content), $output, $code); - if (empty($output)) { - return false; - } - return true; - } + /** + * @param $content + * @return bool + */ + public function pidIsExists($content): bool + { + if (intval($content) < 1) { + return false; + } + $shell = 'ps -eo pid,cmd,state | grep %d | grep -v grep'; + exec(sprintf($shell, $content), $output, $code); + var_dump($output, $code); + if (empty($output)) { + return false; + } + return true; + } - /** - * @param string $path - */ - public function directoryCheck(string $path) - { - $dir = new \DirectoryIterator($path); - if ($dir->getSize() < 1) { - return true; - } - foreach ($dir as $value) { - /** @var \DirectoryIterator $value */ - if ($value->isDot()) continue; + /** + * @param string $path + */ + public function directoryCheck(string $path) + { + $dir = new \DirectoryIterator($path); + if ($dir->getSize() < 1) { + return true; + } + foreach ($dir as $value) { + /** @var \DirectoryIterator $value */ + if ($value->isDot()) continue; - if (!$value->valid()) continue; + if (!$value->valid()) continue; - $this->close($value->getRealPath()); - } - return false; - } + $this->close($value->getRealPath()); + } + return false; + } - /** - * @param string $value - */ - public function close(string $value) - { - $resource = fopen($value, 'r'); - $content = fgets($resource); - fclose($resource); + /** + * @param string $value + */ + public function close(string $value) + { + $resource = fopen($value, 'r'); + $content = fgets($resource); + fclose($resource); - while ($this->pidIsExists($content)) { - exec('kill -15 ' . $content); - sleep(1); - } + while ($this->pidIsExists($content)) { + exec('kill -15 ' . $content); + sleep(1); + } - clearstatcache($value); - if (file_exists($value)) { - @unlink($value); - } - } + clearstatcache($value); + if (file_exists($value)) { + @unlink($value); + } + } }