From fedef53f9306afdac8f6f93954e4a5eeb121589d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 19 Aug 2021 18:10:15 +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/ServerManager.php | 44 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/Server/ServerManager.php b/Server/ServerManager.php index fce49434..fce9a909 100644 --- a/Server/ServerManager.php +++ b/Server/ServerManager.php @@ -117,16 +117,7 @@ class ServerManager */ public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true) { - $customProcess = $this->resolveProcess($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); + $process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine); $this->server->addProcess($process); Kiri::app()->addProcess($customProcess::class, $process); } @@ -134,18 +125,31 @@ class ServerManager /** * @param $customProcess - * @return mixed + * @param $redirect_stdin_and_stdout + * @param $pipe_type + * @param $enable_coroutine + * @return Process */ - private function resolveProcess($customProcess): CustomProcess + private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process { - if (is_string($customProcess)) { - $implements = class_implements($customProcess); - if (!in_array(CustomProcess::class, $implements)) { - trigger_error('custom process must implement ' . CustomProcess::class); - } - $customProcess = new $customProcess($this->server); - } - return $customProcess; + $server = $this->server; + return new Process( + function (Process $soloProcess) use ($customProcess, $server) { + if (is_string($customProcess)) { + $customProcess = Kiri::createObject($customProcess, [$server]); + } + $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 + ); }