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

198 lines
4.1 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;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Contract\OnProcessInterface;
2022-10-11 15:15:04 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-06-12 15:31:44 +08:00
use ReflectionException;
2023-04-02 23:49:09 +08:00
use Swoole\Coroutine;
2022-01-12 11:20:33 +08:00
use Swoole\Process;
2023-04-16 02:01:26 +08:00
use Psr\Container\ContainerInterface;
2022-10-11 15:15:04 +08:00
use Kiri\Events\EventProvider;
use Kiri\Server\ServerInterface;
2023-04-16 02:15:51 +08:00
use Kiri\Di\Inject\Container;
2022-10-11 15:15:04 +08:00
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-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-04-17 01:29:43 +08:00
$provider = Kiri::getDi()->get(EventProvider::class);
$provider->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) {
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()
));
}
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-04-02 23:49:09 +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-04-02 23:49:09 +08:00
// $process = $this->_process[$tag] ?? null;
// if (empty($process)) {
// return null;
// }
// if (!empty($name)) {
// if (!isset($process[$name])) {
// return null;
// }
// return $process[$name];
// }
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-04-02 23:49:09 +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-04-02 23:49:09 +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'));
2022-10-11 23:27:56 +08:00
Kiri::getLogger()->alert($system . ' ' . $custom->getName() . ' start.');
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
}