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;
}