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
+40 -31
View File
@@ -10,6 +10,7 @@ use Kiri\Abstracts\Component;
use Kiri\Server\Contract\OnProcessInterface; use Kiri\Server\Contract\OnProcessInterface;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
use Kiri\Annotation\Inject; use Kiri\Annotation\Inject;
use Kiri\Di\ContainerInterface; use Kiri\Di\ContainerInterface;
@@ -21,7 +22,7 @@ class ProcessManager extends Component
{ {
/** @var array<string, Process> */ /** @var array<string, BaseProcess> */
private array $_process = []; private array $_process = [];
@@ -54,8 +55,20 @@ class ProcessManager extends Component
public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void
{ {
$server = $this->container->get(ServerInterface::class); $server = $this->container->get(ServerInterface::class);
foreach ($this->_process as $process) { foreach ($this->_process as $custom) {
$server->addProcess($process); 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)) { if (is_string($custom)) {
$custom = Kiri::getDi()->get($custom); $custom = Kiri::getDi()->get($custom);
} }
if (isset($this->_process[$custom->getName()])) { if (isset($this->_process[$custom->getName()])) {
throw new Exception('Process(' . $custom->getName() . ') is exists.'); throw new Exception('Process(' . $custom->getName() . ') is exists.');
} }
$this->_process[$custom->getName()] = new Process(function (Process $process) use ($custom) {
$this->extracted($custom, $process); $this->_process[$custom->getName()] = $custom;
},
$custom->getRedirectStdinAndStdout(),
$custom->getPipeType(),
$custom->isEnableCoroutine()
);
} }
@@ -96,9 +105,9 @@ class ProcessManager extends Component
*/ */
public function shutdown(): void public function shutdown(): void
{ {
foreach ($this->_process as $process) { // foreach ($this->_process as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15); // Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
} // }
} }
@@ -121,18 +130,18 @@ class ProcessManager extends Component
*/ */
public function get(?string $name = null, string $tag = 'default'): array|Process|null public function get(?string $name = null, string $tag = 'default'): array|Process|null
{ {
$process = $this->_process[$tag] ?? null; // $process = $this->_process[$tag] ?? null;
if (empty($process)) { // if (empty($process)) {
// return null;
// }
// if (!empty($name)) {
// if (!isset($process[$name])) {
// return null;
// }
// return $process[$name];
// }
return null; return null;
} }
if (!empty($name)) {
if (!isset($process[$name])) {
return null;
}
return $process[$name];
}
return $process;
}
/** /**
@@ -140,9 +149,9 @@ class ProcessManager extends Component
*/ */
public function stop(): void public function stop(): void
{ {
foreach ($this->_process as $process) { // foreach ($this->_process as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15); // 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 public function push(string $name, string $message): void
{ {
if (!isset($this->_process[$name])) { // if (!isset($this->_process[$name])) {
return; // return;
} // }
$process = $this->_process[$name]; // $process = $this->_process[$name];
$process->write($message); // $process->write($message);
} }
/** /**
+2 -2
View File
@@ -16,9 +16,9 @@ interface OnProcessInterface
/** /**
* @param Process $process * @param ?Process $process
*/ */
public function process(Process $process): void; public function process(?Process $process): void;
+1
View File
@@ -25,6 +25,7 @@ use Swoole\Coroutine\Server as ScServer;
use Swoole\Coroutine\Http\Server as SchServer; use Swoole\Coroutine\Http\Server as SchServer;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\Process;
use Swoole\Server; use Swoole\Server;
class CoroutineServer implements ServerInterface class CoroutineServer implements ServerInterface