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

195 lines
3.9 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-10-11 15:15:04 +08:00
use Exception;
2022-01-14 16:11:23 +08:00
use Kiri;
2022-10-11 15:15:04 +08:00
use Kiri\Abstracts\Component;
2023-08-14 21:09:44 +08:00
use Kiri\Di\Inject\Container;
2023-08-14 22:14:35 +08:00
use Kiri\Error\StdoutLogger;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Contract\OnProcessInterface;
2023-08-14 21:09:44 +08:00
use Psr\Log\LoggerInterface;
2023-06-12 15:31:44 +08:00
use ReflectionException;
2022-01-12 11:20:33 +08:00
use Swoole\Process;
2022-10-11 15:15:04 +08:00
use Kiri\Server\ServerInterface;
use Kiri\Server\Events\OnServerBeforeStart;
2022-01-12 11:20:33 +08:00
2022-10-11 15:15:04 +08:00
class ProcessManager extends Component
2022-01-12 11:20:33 +08:00
{
2023-08-14 21:09:44 +08:00
/**
2023-08-14 22:14:35 +08:00
* @var StdoutLogger
2023-08-14 21:09:44 +08:00
*/
#[Container(LoggerInterface::class)]
2023-08-14 22:14:35 +08:00
public StdoutLogger $logger;
2023-08-14 21:09:44 +08:00
2023-04-02 23:49:09 +08:00
/** @var array<string, BaseProcess> */
2022-01-12 11:20:33 +08:00
private array $_process = [];
2022-10-11 15:15:04 +08:00
/**
* @return void
* @throws Exception
*/
public function init(): void
2022-06-16 17:38:22 +08:00
{
2023-07-31 23:08:58 +08:00
on(OnServerBeforeStart::class, [$this, 'OnServerBeforeStart']);
2022-06-16 17:38:22 +08:00
}
2022-02-11 19:00:55 +08:00
2022-01-12 11:32:36 +08:00
2023-06-12 15:31:44 +08:00
/**
* @param OnServerBeforeStart $beforeStart
* @return void
* @throws ReflectionException
*/
2022-10-11 15:15:04 +08:00
public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void
2022-01-12 11:20:33 +08:00
{
2023-06-12 15:31:44 +08:00
$server = Kiri::getDi()->get(ServerInterface::class);
2023-04-02 23:49:09 +08:00
foreach ($this->_process as $custom) {
2023-07-31 23:08:58 +08:00
$server->addProcess(new Process(function (Process $process) use ($custom) {
$this->extracted($custom, $process);
},
$custom->getRedirectStdinAndStdout(),
$custom->getPipeType(),
$custom->isEnableCoroutine()
));
2022-01-12 11:20:33 +08:00
}
2022-10-11 15:15:04 +08:00
}
2022-01-12 11:32:36 +08:00
2022-06-16 17:38:22 +08:00
2023-02-13 11:50:27 +08:00
/**
* @return Process[]
*/
public function getProcesses(): array
{
return $this->_process;
}
2022-10-11 15:15:04 +08:00
/**
* @param string|OnProcessInterface|BaseProcess $custom
* @throws Exception
*/
public function add(string|OnProcessInterface|BaseProcess $custom): void
{
if (is_string($custom)) {
$custom = Kiri::getDi()->get($custom);
}
2023-04-02 23:49:09 +08:00
2022-10-11 23:28:51 +08:00
if (isset($this->_process[$custom->getName()])) {
2022-10-11 15:15:04 +08:00
throw new Exception('Process(' . $custom->getName() . ') is exists.');
}
2023-04-02 23:49:09 +08:00
$this->_process[$custom->getName()] = $custom;
2022-06-16 17:38:22 +08:00
}
2022-06-20 18:14:25 +08:00
/**
* @return void
*/
public function shutdown(): void
{
2023-07-31 23:08:58 +08:00
foreach ($this->_process as $process) {
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-10-11 23:27:56 +08:00
$this->extracted($customProcess, $process);
2022-06-16 17:38:22 +08:00
};
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
{
2023-07-31 23:08:58 +08:00
$process = $this->_process[$tag] ?? null;
if (empty($process)) {
return null;
}
if (!empty($name)) {
if (!isset($process[$name])) {
return null;
}
return $process[$name];
}
2023-04-02 23:49:09 +08:00
return null;
2022-02-11 19:00:55 +08:00
}
/**
* @return void
*/
2022-06-16 17:38:22 +08:00
public function stop(): void
2022-02-11 19:00:55 +08:00
{
2023-07-31 23:08:58 +08:00
foreach ($this->_process as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
}
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
* @return void
2022-10-11 15:15:04 +08:00
* @throws Exception
2022-01-12 11:20:33 +08:00
*/
2022-10-11 15:15:04 +08:00
public function batch(?array $processes): 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-10-11 15:15:04 +08:00
$this->add($process);
2022-01-12 11:20:33 +08:00
}
}
/**
* @param string $message
* @param string $name
* @return void
*/
2022-10-11 15:15:04 +08:00
public function push(string $name, string $message): void
2022-01-12 11:20:33 +08:00
{
2023-07-31 23:08:58 +08:00
if (!isset($this->_process[$name])) {
return;
}
$process = $this->_process[$name];
$process->write($message);
2022-01-12 11:20:33 +08:00
}
2023-02-13 11:50:27 +08:00
2022-10-11 23:27:56 +08:00
/**
* @param mixed $custom
* @param Process $process
* @return void
* @throws Kiri\Exception\ConfigException
2023-06-12 15:31:44 +08:00
* @throws ReflectionException
2022-10-11 23:27:56 +08:00
*/
public function extracted(mixed $custom, Process $process): void
{
set_env('environmental', Kiri::PROCESS);
2023-05-25 16:59:17 +08:00
$system = sprintf('[%s].Custom Process', \config('id', 'system-service'));
2023-08-14 21:09:44 +08:00
$this->logger->alert($system . ' ' . $custom->getName() . ' start.');
2022-10-11 23:27:56 +08:00
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '[' . $process->pid . '].' . $custom->getName());
}
$custom->onSigterm()->process($process);
}
2023-02-13 11:50:27 +08:00
2022-01-12 11:20:33 +08:00
}