From 633d1fce44bdb333a2050ce0faebdc3d1c0d30bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 19 Aug 2021 17:41:10 +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 --- Server/Abstracts/CustomProcess.php | 20 +++++++++++------ Server/ServerManager.php | 35 ++++++++++++++++++----------- System/Application.php | 36 ++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/Server/Abstracts/CustomProcess.php b/Server/Abstracts/CustomProcess.php index bcb510d7..108f2823 100644 --- a/Server/Abstracts/CustomProcess.php +++ b/Server/Abstracts/CustomProcess.php @@ -4,6 +4,7 @@ namespace Server\Abstracts; use JetBrains\PhpStorm\Pure; +use Kiri\Kiri; use Swoole\Coroutine; use Swoole\Process; @@ -43,18 +44,23 @@ abstract class CustomProcess implements \Server\SInterface\CustomProcess */ public function signListen(Process $process): void { - if (!$this->enableSwooleCoroutine) { - Process::signal(SIGTERM | SIGKILL, function ($signo) - use ($process) { - $this->onProcessStop(); - $this->waiteExit($process); + if (Coroutine::getCid() === -1) { + Process::signal(SIGTERM | SIGKILL, function ($signo) use ($process) { + if ($signo) { + $lists = Kiri::app()->getProcess(); + foreach ($lists as $process) { + $process->exit(0); + } + } }); } else { go(function () use ($process) { $data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1); if ($data) { - $this->onProcessStop(); - $this->waiteExit($process); + $lists = Kiri::app()->getProcess(); + foreach ($lists as $process) { + $process->exit(0); + } } }); } diff --git a/Server/ServerManager.php b/Server/ServerManager.php index beb836e6..753ea550 100644 --- a/Server/ServerManager.php +++ b/Server/ServerManager.php @@ -94,6 +94,7 @@ class ServerManager /** * @return bool * @throws ConfigException + * @throws Exception */ public function isRunner(): bool { @@ -115,6 +116,26 @@ class ServerManager * @throws Exception */ public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true) + { + $customProcess = $this->resolveProcess($customProcess); + Kiri::app()->addProcess($customProcess, $process = new Process(function (Process $soloProcess) use ($customProcess) { + $system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid); + if (Kiri::getPlatform()->isLinux()) { + $soloProcess->name($system . '.' . $customProcess->getProcessName($soloProcess) . ' start.'); + } + $customProcess->signListen($soloProcess); + echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Process %s start.", $customProcess->getProcessName($soloProcess)) . PHP_EOL; + $customProcess->onHandler($soloProcess); + }, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine)); + $this->server->addProcess($process); + } + + + /** + * @param $customProcess + * @return mixed + */ + private function resolveProcess($customProcess): CustomProcess { if (is_string($customProcess)) { $implements = class_implements($customProcess); @@ -123,19 +144,7 @@ class ServerManager } $customProcess = new $customProcess($this->server); } - /** @var Process $process */ - $this->server->addProcess(new Process(function (Process $soloProcess) use ($customProcess) { - $system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid); - if (Kiri::getPlatform()->isLinux()) { - $soloProcess->name($system . '.' . $customProcess->getProcessName($soloProcess) . ' start.'); - } - - $customProcess->signListen($soloProcess); - - echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Process %s start.", $customProcess->getProcessName($soloProcess)) . PHP_EOL; - $customProcess->onHandler($soloProcess); - }, - $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine)); + return $customProcess; } diff --git a/System/Application.php b/System/Application.php index 884a282e..93518405 100644 --- a/System/Application.php +++ b/System/Application.php @@ -11,7 +11,6 @@ namespace Kiri; use Closure; -use Console\CommandInterface; use Console\Console; use Console\ConsoleProviders; use Database\DatabasesProviders; @@ -28,8 +27,8 @@ use Kiri\Exception\NotFindClassException; use Kiri\FileListen\FileChangeCustomProcess; use ReflectionException; use Server\ResponseInterface; -use Server\ServerManager; use stdClass; +use Swoole\Process; use Swoole\Timer; /** @@ -51,6 +50,10 @@ class Application extends BaseApplication public string $state = ''; + /** @var array */ + private array $_process = []; + + /** * @throws NotFindClassException */ @@ -79,6 +82,35 @@ class Application extends BaseApplication } + /** + * @param string $class + * @param Process $process + */ + public function addProcess(string $class, Process $process) + { + $this->_process[$class] = $process; + } + + + /** + * @return Process[] + */ + public function getProcess(): array + { + return $this->_process; + } + + + /** + * @param string $class + * @return Process|null + */ + public function getProcessName(string $class): ?Process + { + return $this->_process[$class] ?? null; + } + + /** * @throws NotFindClassException * @throws ReflectionException