This commit is contained in:
2021-08-19 17:41:10 +08:00
parent 99b4aee754
commit 633d1fce44
3 changed files with 69 additions and 22 deletions
+13 -7
View File
@@ -4,6 +4,7 @@ namespace Server\Abstracts;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Kiri;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
@@ -43,18 +44,23 @@ abstract class CustomProcess implements \Server\SInterface\CustomProcess
*/ */
public function signListen(Process $process): void public function signListen(Process $process): void
{ {
if (!$this->enableSwooleCoroutine) { if (Coroutine::getCid() === -1) {
Process::signal(SIGTERM | SIGKILL, function ($signo) Process::signal(SIGTERM | SIGKILL, function ($signo) use ($process) {
use ($process) { if ($signo) {
$this->onProcessStop(); $lists = Kiri::app()->getProcess();
$this->waiteExit($process); foreach ($lists as $process) {
$process->exit(0);
}
}
}); });
} else { } else {
go(function () use ($process) { go(function () use ($process) {
$data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1); $data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
if ($data) { if ($data) {
$this->onProcessStop(); $lists = Kiri::app()->getProcess();
$this->waiteExit($process); foreach ($lists as $process) {
$process->exit(0);
}
} }
}); });
} }
+22 -13
View File
@@ -94,6 +94,7 @@ class ServerManager
/** /**
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
* @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
@@ -115,6 +116,26 @@ class ServerManager
* @throws Exception * @throws Exception
*/ */
public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true) public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
{
$customProcess = $this->resolveProcess($customProcess);
Kiri::app()->addProcess($customProcess, $process = new Process(function (Process $soloProcess) use ($customProcess) {
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
if (Kiri::getPlatform()->isLinux()) {
$soloProcess->name($system . '.' . $customProcess->getProcessName($soloProcess) . ' start.');
}
$customProcess->signListen($soloProcess);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Process %s start.", $customProcess->getProcessName($soloProcess)) . PHP_EOL;
$customProcess->onHandler($soloProcess);
}, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine));
$this->server->addProcess($process);
}
/**
* @param $customProcess
* @return mixed
*/
private function resolveProcess($customProcess): CustomProcess
{ {
if (is_string($customProcess)) { if (is_string($customProcess)) {
$implements = class_implements($customProcess); $implements = class_implements($customProcess);
@@ -123,19 +144,7 @@ class ServerManager
} }
$customProcess = new $customProcess($this->server); $customProcess = new $customProcess($this->server);
} }
/** @var Process $process */ return $customProcess;
$this->server->addProcess(new Process(function (Process $soloProcess) use ($customProcess) {
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
if (Kiri::getPlatform()->isLinux()) {
$soloProcess->name($system . '.' . $customProcess->getProcessName($soloProcess) . ' start.');
}
$customProcess->signListen($soloProcess);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Process %s start.", $customProcess->getProcessName($soloProcess)) . PHP_EOL;
$customProcess->onHandler($soloProcess);
},
$redirect_stdin_and_stdout, $pipe_type, $enable_coroutine));
} }
+34 -2
View File
@@ -11,7 +11,6 @@ namespace Kiri;
use Closure; use Closure;
use Console\CommandInterface;
use Console\Console; use Console\Console;
use Console\ConsoleProviders; use Console\ConsoleProviders;
use Database\DatabasesProviders; use Database\DatabasesProviders;
@@ -28,8 +27,8 @@ use Kiri\Exception\NotFindClassException;
use Kiri\FileListen\FileChangeCustomProcess; use Kiri\FileListen\FileChangeCustomProcess;
use ReflectionException; use ReflectionException;
use Server\ResponseInterface; use Server\ResponseInterface;
use Server\ServerManager;
use stdClass; use stdClass;
use Swoole\Process;
use Swoole\Timer; use Swoole\Timer;
/** /**
@@ -51,6 +50,10 @@ class Application extends BaseApplication
public string $state = ''; public string $state = '';
/** @var array<Process> */
private array $_process = [];
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
@@ -79,6 +82,35 @@ class Application extends BaseApplication
} }
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
$this->_process[$class] = $process;
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/** /**
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException