From 684c5a3ebbbb43beda4fa667881de14f7e5edc6a Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 20 Jun 2024 16:41:45 +0800 Subject: [PATCH] eee --- Abstracts/ProcessManager.php | 45 ++++++++++-------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/Abstracts/ProcessManager.php b/Abstracts/ProcessManager.php index f01eea8..b15fff3 100644 --- a/Abstracts/ProcessManager.php +++ b/Abstracts/ProcessManager.php @@ -30,6 +30,9 @@ class ProcessManager extends Component private array $_process = []; + /** @var array $runner */ + private array $runner = []; + /** * @return void * @throws Exception @@ -48,26 +51,17 @@ class ProcessManager extends Component { $server = Kiri::getDi()->get(ServerInterface::class); foreach ($this->_process as $custom) { - $server->addProcess(new Process(function (Process $process) use ($custom) { + $this->runner[$custom->getName()] = new Process(function (Process $process) use ($custom) { $this->extracted($custom, $process); }, $custom->getRedirectStdinAndStdout(), $custom->getPipeType(), $custom->isEnableCoroutine() - )); + ); + $server->addProcess($this->runner[$custom->getName()]); } } - - /** - * @return Process[] - */ - public function getProcesses(): array - { - return $this->_process; - } - - /** * @param string|OnProcessInterface|BaseProcess $custom * @throws @@ -91,7 +85,7 @@ class ProcessManager extends Component */ public function shutdown(): void { - foreach ($this->_process as $process) { + foreach ($this->runner as $process) { Process::kill($process->pid, 0) && Process::kill($process->pid, 15); } } @@ -111,22 +105,11 @@ class ProcessManager extends Component /** * @param string|null $name - * @param string $tag - * @return array|Process|null + * @return null|Process */ - public function get(?string $name = null, string $tag = 'default'): array|Process|null + public function get(?string $name = null): ?Process { - $process = $this->_process[$tag] ?? null; - if (empty($process)) { - return null; - } - if (!empty($name)) { - if (!isset($process[$name])) { - return null; - } - return $process[$name]; - } - return null; + return $this->runner[$name] ?? null; } @@ -135,7 +118,7 @@ class ProcessManager extends Component */ public function stop(): void { - foreach ($this->_process as $process) { + foreach ($this->runner as $process) { Process::kill($process->pid, 0) && Process::kill($process->pid, 15); } } @@ -164,11 +147,7 @@ class ProcessManager extends Component */ public function push(string $name, string $message): void { - if (!isset($this->_process[$name])) { - return; - } - $process = $this->_process[$name]; - $process->write($message); + $this->runner[$name]?->write($message); } /**