Files
kiri-http-server/Abstracts/ProcessManager.php
T

161 lines
3.4 KiB
PHP
Raw Normal View History

2022-01-12 11:20:33 +08:00
<?php
2022-06-16 17:49:06 +08:00
namespace Kiri\Server\Abstracts;
2022-01-12 11:20:33 +08:00
2022-06-16 17:38:22 +08:00
use Closure;
2022-01-14 16:11:23 +08:00
use Kiri;
2022-01-12 11:20:33 +08:00
use Kiri\Abstracts\Config;
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;
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\Process;
2022-06-16 17:38:22 +08:00
use Kiri\Server\Events\OnProcessStop;
use Kiri\Di\ContainerInterface;
2022-01-12 11:20:33 +08:00
class ProcessManager
{
/** @var array<string, Process> */
private array $_process = [];
2022-06-16 17:38:22 +08:00
/**
* @param ContainerInterface $container
* @param LoggerInterface $logger
*/
public function __construct(public ContainerInterface $container, public LoggerInterface $logger)
{
}
2022-02-11 19:00:55 +08:00
2022-01-12 11:32:36 +08:00
2022-01-12 11:20:33 +08:00
/**
* @param string|OnProcessInterface|BaseProcess $customProcess
2022-06-16 17:38:22 +08:00
* @return array
2022-01-12 11:20:33 +08:00
*/
2022-06-16 17:38:22 +08:00
public function add(string|OnProcessInterface|BaseProcess $customProcess): array
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-06-16 17:38:22 +08:00
if (Context::inCoroutine()) {
2022-06-22 18:43:36 +08:00
return [$customProcess, $this->resolve($customProcess)];
2022-01-17 19:04:26 +08:00
}
2022-06-16 17:38:22 +08:00
2022-06-22 18:43:36 +08:00
$process = new Process($this->resolve($customProcess),
2022-06-16 17:38:22 +08:00
$customProcess->getRedirectStdinAndStdout(),
$customProcess->getPipeType(),
$customProcess->isEnableCoroutine()
);
return [$customProcess, $process];
}
2022-06-20 18:14:25 +08:00
/**
* @return void
*/
public function shutdown(): void
{
foreach ($this->_process as $process) {
2022-06-20 18:27:25 +08:00
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
2022-06-20 18:14:25 +08:00
}
}
2022-06-16 17:38:22 +08:00
/**
2022-06-22 18:43:36 +08:00
* @param BaseProcess $customProcess
2022-06-16 17:38:22 +08:00
* @return Closure
*/
2022-06-22 18:43:36 +08:00
public function resolve(BaseProcess $customProcess): Closure
2022-06-16 17:38:22 +08:00
{
2022-06-22 18:43:36 +08:00
return static function (Process $process) use ($customProcess) {
2022-06-16 17:38:22 +08:00
set_env('environmental', Kiri::PROCESS);
2022-06-22 18:43:36 +08:00
$system = sprintf('[%s].Custom Process', Config::get('id', 'system-service'));
Kiri::getLogger()->alert($system . ' ' . $customProcess->getName() . ' start.');
2022-06-16 17:38:22 +08:00
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '(' . $customProcess->getName() . ')');
}
$customProcess->onSigterm()->process($process);
};
2022-02-11 19:00:55 +08:00
}
/**
* @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
*/
2022-06-16 17:38:22 +08:00
public function stop(): void
2022-02-11 19:00:55 +08:00
{
foreach ($this->_process as $process) {
2022-06-20 18:18:38 +08:00
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
2022-02-11 19:00:55 +08:00
}
2022-01-12 11:20:33 +08:00
}
/**
2022-06-17 12:33:14 +08:00
* @param array|null $processes
2022-06-16 17:38:22 +08:00
* @param \Swoole\Server|null $server
* @return void
* @throws ConfigException
2022-01-12 11:20:33 +08:00
*/
2022-06-17 12:33:14 +08:00
public function batch(?array $processes, ?\Swoole\Server $server = null): void
2022-01-12 11:20:33 +08:00
{
2022-06-17 12:33:14 +08:00
if (empty($processes)) {
return;
}
2022-01-12 11:20:33 +08:00
foreach ($processes as $process) {
2022-06-16 17:38:22 +08:00
[$customProcess, $sProcess] = $this->add($process);
2022-06-22 16:29:41 +08:00
$this->_process[$customProcess->getName()] = $customProcess;
2022-06-16 17:38:22 +08:00
2022-06-22 16:29:41 +08:00
$server->addProcess($sProcess);
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-06-16 17:38:22 +08:00
public function push(string $message, string $name = '', string $tag = 'default'): void
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
}
}
}