This commit is contained in:
xl
2024-06-21 14:35:43 +08:00
parent def6675c81
commit b14b18040b
2 changed files with 1 additions and 173 deletions
-172
View File
@@ -1,172 +0,0 @@
<?php
namespace Kiri\Server\Abstracts;
use Closure;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Di\Inject\Container;
use Kiri\Error\StdoutLogger;
use Kiri\Server\Contract\OnProcessInterface;
use Psr\Log\LoggerInterface;
use ReflectionException;
use Swoole\Process;
use Kiri\Server\ServerInterface;
use Kiri\Server\Events\OnServerBeforeStart;
class ProcessManager extends Component
{
/**
* @var StdoutLogger
*/
#[Container(LoggerInterface::class)]
public StdoutLogger $logger;
/** @var array<string, BaseProcess> */
private array $_process = [];
/** @var array<string, Process> $runner */
private array $runner = [];
/**
* @return void
* @throws Exception
*/
public function init(): void
{
on(OnServerBeforeStart::class, [$this, 'OnServerBeforeStart']);
}
/**
* @param OnServerBeforeStart $beforeStart
* @return void
*/
public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void
{
$server = Kiri::getDi()->get(ServerInterface::class);
foreach ($this->_process as $custom) {
$this->runner[$custom->getName()] = new Process(function (Process $process) use ($custom) {
$this->extracted($custom, $process);
},
$custom->getRedirectStdinAndStdout(),
$custom->getPipeType(),
$custom->isEnableCoroutine()
);
$server->addProcess($this->runner[$custom->getName()]);
}
}
/**
* @param string|OnProcessInterface|BaseProcess $custom
* @throws
*/
public function add(string|OnProcessInterface|BaseProcess $custom): void
{
if (is_string($custom)) {
$custom = Kiri::getDi()->get($custom);
}
if (isset($this->_process[$custom->getName()])) {
throw new Exception('Process(' . $custom->getName() . ') is exists.');
}
$this->_process[$custom->getName()] = $custom;
}
/**
* @return void
*/
public function shutdown(): void
{
foreach ($this->runner as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
}
}
/**
* @param BaseProcess $customProcess
* @return Closure
*/
public function resolve(BaseProcess $customProcess): Closure
{
return static function (Process $process) use ($customProcess) {
$this->extracted($customProcess, $process);
};
}
/**
* @param string|null $name
* @return null|Process
*/
public function get(?string $name = null): ?Process
{
return $this->runner[$name] ?? null;
}
/**
* @return void
*/
public function stop(): void
{
foreach ($this->runner as $process) {
Process::kill($process->pid, 0) && Process::kill($process->pid, 15);
}
}
/**
* @param array|null $processes
* @return void
* @throws Exception
*/
public function batch(?array $processes): void
{
if (empty($processes)) {
return;
}
foreach ($processes as $process) {
$this->add($process);
}
}
/**
* @param string $message
* @param string $name
* @return void
*/
public function push(string $name, string $message): void
{
$this->runner[$name]?->write($message);
}
/**
* @param mixed $custom
* @param Process $process
* @return void
* @throws Kiri\Exception\ConfigException
* @throws ReflectionException
*/
public function extracted(mixed $custom, Process $process): void
{
set_env('environmental', Kiri::PROCESS);
$system = sprintf('[%s].Custom Process', \config('id', 'system-service'));
$this->logger->alert($system . ' ' . $custom->getName() . ' start.');
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '[' . $process->pid . '].' . $custom->getName());
}
$custom->onSigterm()->process($process);
}
}
+1 -1
View File
@@ -64,7 +64,7 @@ trait TraitServer
private function genProcess(BaseProcess $name): Process private function genProcess(BaseProcess $name): Process
{ {
return new Process(function (Process $process) use ($name) { return new Process(function (Process $process) use ($name) {
$process->name($name->getName()); $process->name('[' . \config('id','system-service') . ']' . $name->getName());
$name->onSigterm()->process($process); $name->onSigterm()->process($process);
}, },
$name->getRedirectStdinAndStdout(), $name->getRedirectStdinAndStdout(),