This commit is contained in:
2023-04-02 23:49:09 +08:00
parent 5138c429fe
commit b7ee0b3b62
3 changed files with 83 additions and 73 deletions
+41 -32
View File
@@ -10,6 +10,7 @@ use Kiri\Abstracts\Component;
use Kiri\Server\Contract\OnProcessInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine;
use Swoole\Process;
use Kiri\Annotation\Inject;
use Kiri\Di\ContainerInterface;
@@ -21,7 +22,7 @@ class ProcessManager extends Component
{
/** @var array<string, Process> */
/** @var array<string, BaseProcess> */
private array $_process = [];
@@ -54,8 +55,20 @@ class ProcessManager extends Component
public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void
{
$server = $this->container->get(ServerInterface::class);
foreach ($this->_process as $process) {
$server->addProcess($process);
foreach ($this->_process as $custom) {
if (Kiri\Di\Context::inCoroutine()) {
Coroutine::create(function () use ($custom) {
$custom->onSigterm()->process(null);
});
} else {
$server->addProcess(new Process(function (Process $process) use ($custom) {
$this->extracted($custom, $process);
},
$custom->getRedirectStdinAndStdout(),
$custom->getPipeType(),
$custom->isEnableCoroutine()
));
}
}
}
@@ -78,16 +91,12 @@ class ProcessManager extends Component
if (is_string($custom)) {
$custom = Kiri::getDi()->get($custom);
}
if (isset($this->_process[$custom->getName()])) {
throw new Exception('Process(' . $custom->getName() . ') is exists.');
}
$this->_process[$custom->getName()] = new Process(function (Process $process) use ($custom) {
$this->extracted($custom, $process);
},
$custom->getRedirectStdinAndStdout(),
$custom->getPipeType(),
$custom->isEnableCoroutine()
);
$this->_process[$custom->getName()] = $custom;
}
@@ -96,9 +105,9 @@ class ProcessManager extends Component
*/
public function shutdown(): void
{
foreach ($this->_process as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
}
// foreach ($this->_process as $process) {
// Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
// }
}
@@ -121,17 +130,17 @@ class ProcessManager extends Component
*/
public function get(?string $name = null, string $tag = 'default'): array|Process|null
{
$process = $this->_process[$tag] ?? null;
if (empty($process)) {
return null;
}
if (!empty($name)) {
if (!isset($process[$name])) {
return null;
}
return $process[$name];
}
return $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;
}
@@ -140,9 +149,9 @@ class ProcessManager extends Component
*/
public function stop(): void
{
foreach ($this->_process as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
}
// foreach ($this->_process as $process) {
// Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
// }
}
@@ -169,11 +178,11 @@ 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);
// if (!isset($this->_process[$name])) {
// return;
// }
// $process = $this->_process[$name];
// $process->write($message);
}
/**