This commit is contained in:
2021-03-29 10:44:36 +08:00
parent 4d69d4bfaf
commit ef9a10d7ff
+104 -100
View File
@@ -16,127 +16,131 @@ class Shutdown extends Component
{ {
private string $taskDirectory; private string $taskDirectory;
private string $workerDirectory; private string $workerDirectory;
private string $managerDirectory; private string $managerDirectory;
private string $processDirectory; private string $processDirectory;
public function init() /**
{ * @throws Exception
$this->taskDirectory = storage(null, 'pid/task'); */
$this->workerDirectory = storage(null, 'pid/worker'); public function init()
$this->managerDirectory = storage(null, 'pid/manager'); {
$this->processDirectory = storage(null, 'pid/process'); $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 * @throws Exception
*/ */
public function shutdown(): void public function shutdown(): void
{ {
clearstatcache(storage()); clearstatcache(storage());
exec('ls -alh /.dockerenv', $output, $cod); exec('ls -alh /.dockerenv', $output, $cod);
if ($cod === 0 && !empty($output)) { if ($cod === 0 && !empty($output)) {
return; return;
} }
$master_pid = Server()->setting['pid_file'] ?? PID_PATH; $master_pid = Server()->setting['pid_file'] ?? PID_PATH;
if (file_exists($master_pid)) { if (file_exists($master_pid)) {
$this->close($master_pid); $this->close($master_pid);
} }
$this->closeOther(); $this->closeOther();
} }
/** /**
* 关闭其他进程 * 关闭其他进程
*/ */
private function closeOther(): void private function closeOther(): void
{ {
$this->directoryCheck($this->managerDirectory); $this->directoryCheck($this->managerDirectory);
$this->directoryCheck($this->taskDirectory); $this->directoryCheck($this->taskDirectory);
$this->directoryCheck($this->workerDirectory); $this->directoryCheck($this->workerDirectory);
$this->directoryCheck($this->processDirectory); $this->directoryCheck($this->processDirectory);
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
* check server is running. * check server is running.
*/ */
public function isRunning() public function isRunning()
{ {
$master_pid = Server()->setting['pid_file'] ?? PID_PATH; $master_pid = Server()->setting['pid_file'] ?? PID_PATH;
if (!file_exists($master_pid)) { if (!file_exists($master_pid)) {
return false; return false;
} }
return $this->pidIsExists($master_pid); return $this->pidIsExists($master_pid);
} }
/** /**
* @param $content * @param $content
* @return bool * @return bool
*/ */
public function pidIsExists($content): bool public function pidIsExists($content): bool
{ {
if (intval($content) < 1) { if (intval($content) < 1) {
return false; return false;
} }
$shell = 'ps -eo pid,cmd,state | grep %d | grep -v grep'; $shell = 'ps -eo pid,cmd,state | grep %d | grep -v grep';
exec(sprintf($shell, $content), $output, $code); exec(sprintf($shell, $content), $output, $code);
if (empty($output)) { var_dump($output, $code);
return false; if (empty($output)) {
} return false;
return true; }
} return true;
}
/** /**
* @param string $path * @param string $path
*/ */
public function directoryCheck(string $path) public function directoryCheck(string $path)
{ {
$dir = new \DirectoryIterator($path); $dir = new \DirectoryIterator($path);
if ($dir->getSize() < 1) { if ($dir->getSize() < 1) {
return true; return true;
} }
foreach ($dir as $value) { foreach ($dir as $value) {
/** @var \DirectoryIterator $value */ /** @var \DirectoryIterator $value */
if ($value->isDot()) continue; if ($value->isDot()) continue;
if (!$value->valid()) continue; if (!$value->valid()) continue;
$this->close($value->getRealPath()); $this->close($value->getRealPath());
} }
return false; return false;
} }
/** /**
* @param string $value * @param string $value
*/ */
public function close(string $value) public function close(string $value)
{ {
$resource = fopen($value, 'r'); $resource = fopen($value, 'r');
$content = fgets($resource); $content = fgets($resource);
fclose($resource); fclose($resource);
while ($this->pidIsExists($content)) { while ($this->pidIsExists($content)) {
exec('kill -15 ' . $content); exec('kill -15 ' . $content);
sleep(1); sleep(1);
} }
clearstatcache($value); clearstatcache($value);
if (file_exists($value)) { if (file_exists($value)) {
@unlink($value); @unlink($value);
} }
} }
} }