This commit is contained in:
xl
2024-06-20 16:41:45 +08:00
parent 858b9bc9f9
commit 684c5a3ebb
+12 -33
View File
@@ -30,6 +30,9 @@ class ProcessManager extends Component
private array $_process = [];
/** @var array<string, Process> $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);
}
/**