Files
kiri-http-server/ProcessManager.php
T

167 lines
3.7 KiB
PHP
Raw Normal View History

2022-01-12 11:20:33 +08:00
<?php
namespace Kiri\Server;
2022-01-14 16:11:23 +08:00
use Kiri;
2022-01-12 11:20:33 +08:00
use Kiri\Abstracts\Config;
2022-01-12 11:32:36 +08:00
use Kiri\Annotation\Inject;
2022-01-12 11:20:33 +08:00
use Kiri\Context;
2022-01-14 16:11:23 +08:00
use Kiri\Events\EventDispatch;
2022-01-12 11:20:33 +08:00
use Kiri\Exception\ConfigException;
use Kiri\Server\Abstracts\BaseProcess;
2022-01-12 11:29:50 +08:00
use Kiri\Server\Broadcast\Message;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Contract\OnProcessInterface;
2022-01-14 16:11:23 +08:00
use Kiri\Server\Events\OnProcessStart;
2022-01-12 11:32:36 +08:00
use Psr\Log\LoggerInterface;
2022-01-12 11:20:33 +08:00
use Swoole\Coroutine;
use Swoole\Process;
class ProcessManager
{
/** @var array<string, Process> */
private array $_process = [];
2022-02-11 19:00:55 +08:00
/** @var array<string, Process> */
private array $_taskProcess = [];
2022-01-12 11:32:36 +08:00
#[Inject(LoggerInterface::class)]
public LoggerInterface $logger;
2022-01-12 11:20:33 +08:00
/**
* @param string|OnProcessInterface|BaseProcess $customProcess
2022-02-11 19:00:55 +08:00
* @param string $tag
2022-01-12 11:20:33 +08:00
* @return void
* @throws ConfigException
*/
2022-02-11 19:00:55 +08:00
public function add(string|OnProcessInterface|BaseProcess $customProcess, string $tag = 'default')
2022-01-12 11:20:33 +08:00
{
if (is_string($customProcess)) {
$customProcess = Kiri::getDi()->get($customProcess);
}
2022-01-12 11:32:36 +08:00
2022-01-12 11:20:33 +08:00
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
2022-01-12 11:32:36 +08:00
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
2022-01-17 19:04:26 +08:00
$process = $this->parse($customProcess, $system);
2022-02-14 11:31:25 +08:00
if (!Kiri::getDi()->has(SwooleServerInterface::class)) {
2022-02-11 16:44:10 +08:00
$process->start();
2022-01-17 19:04:26 +08:00
} else {
2022-02-14 11:31:25 +08:00
$server = Kiri::getDi()->get(SwooleServerInterface::class);
2022-01-17 19:04:26 +08:00
$server->addProcess($process = $this->parse($customProcess, $system));
}
2022-02-11 19:00:55 +08:00
$this->_process[$tag][$customProcess->getName()] = $process;
}
/**
* @param string|null $name
* @param string $tag
2022-02-14 18:05:50 +08:00
* @return array|Process|null
2022-02-11 19:00:55 +08:00
*/
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;
}
/**
* @return void
*/
public function stop()
{
foreach ($this->_process as $process) {
$process->exit(0);
}
foreach ($this->_taskProcess as $process) {
$process->exit(0);
}
2022-01-12 11:20:33 +08:00
}
/**
* @param $customProcess
* @param $system
* @return Process
*/
private function parse($customProcess, $system): Process
{
return new Process(function (Process $process) use ($customProcess, $system) {
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '(' . $customProcess->getName() . ')');
}
2022-01-14 16:11:23 +08:00
Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnProcessStart());
2022-01-12 11:52:58 +08:00
set_env('environmental', Kiri::PROCESS);
2022-01-12 11:20:33 +08:00
$channel = Coroutine::create(function () use ($process, $customProcess) {
while (!$customProcess->isStop()) {
$message = $process->read();
if (!empty($message)) {
2022-01-12 11:29:50 +08:00
$message = unserialize($message);
}
if (is_null($message)) {
continue;
2022-01-12 11:20:33 +08:00
}
2022-01-12 11:29:50 +08:00
$customProcess->onBroadcast($message);
2022-01-12 11:20:33 +08:00
}
});
Context::setContext('waite:process:message', $channel);
$customProcess->onSigterm()->process($process);
},
$customProcess->getRedirectStdinAndStdout(),
$customProcess->getPipeType(),
$customProcess->isEnableCoroutine()
);
}
/**
* @param array $processes
2022-02-11 19:00:55 +08:00
* @param string $tag
2022-01-12 11:20:33 +08:00
* @return void
* @throws ConfigException
*/
2022-02-11 19:00:55 +08:00
public function batch(array $processes, string $tag = 'default')
2022-01-12 11:20:33 +08:00
{
foreach ($processes as $process) {
2022-02-11 19:00:55 +08:00
$this->add($process, $tag);
2022-01-12 11:20:33 +08:00
}
}
/**
* @param string $message
* @param string $name
2022-02-11 19:00:55 +08:00
* @param string $tag
2022-01-12 11:20:33 +08:00
* @return void
*/
2022-02-11 19:00:55 +08:00
public function push(string $message, string $name = '', string $tag = 'default')
2022-01-12 11:20:33 +08:00
{
$processes = $this->_process;
if (!empty($this->_process[$name])) {
$processes = [$this->_process[$name]];
}
foreach ($processes as $process) {
2022-01-12 11:29:50 +08:00
$process->write(serialize(new Message($message)));
2022-01-12 11:20:33 +08:00
}
}
}