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 Kiri\Kiri;
use Swoole\Coroutine;
use Swoole\Process;
@@ -43,18 +44,23 @@ abstract class CustomProcess implements \Server\SInterface\CustomProcess
*/
public function signListen(Process $process): void
{
if (!$this->enableSwooleCoroutine) {
Process::signal(SIGTERM | SIGKILL, function ($signo)
use ($process) {
$this->onProcessStop();
$this->waiteExit($process);
if (Coroutine::getCid() === -1) {
Process::signal(SIGTERM | SIGKILL, function ($signo) use ($process) {
if ($signo) {
$lists = Kiri::app()->getProcess();
foreach ($lists as $process) {
$process->exit(0);
}
}
});
} else {
go(function () use ($process) {
$data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
if ($data) {
$this->onProcessStop();
$this->waiteExit($process);
$lists = Kiri::app()->getProcess();
foreach ($lists as $process) {
$process->exit(0);
}
}
});
}
+22 -13
View File
@@ -94,6 +94,7 @@ class ServerManager
/**
* @return bool
* @throws ConfigException
* @throws Exception
*/
public function isRunner(): bool
{
@@ -115,6 +116,26 @@ class ServerManager
* @throws Exception
*/
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)) {
$implements = class_implements($customProcess);
@@ -123,19 +144,7 @@ class ServerManager
}
$customProcess = new $customProcess($this->server);
}
/** @var Process $process */
$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));
return $customProcess;
}
+34 -2
View File
@@ -11,7 +11,6 @@ namespace Kiri;
use Closure;
use Console\CommandInterface;
use Console\Console;
use Console\ConsoleProviders;
use Database\DatabasesProviders;
@@ -28,8 +27,8 @@ use Kiri\Exception\NotFindClassException;
use Kiri\FileListen\FileChangeCustomProcess;
use ReflectionException;
use Server\ResponseInterface;
use Server\ServerManager;
use stdClass;
use Swoole\Process;
use Swoole\Timer;
/**
@@ -51,6 +50,10 @@ class Application extends BaseApplication
public string $state = '';
/** @var array<Process> */
private array $_process = [];
/**
* @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 ReflectionException