2021-08-19 15:22:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Server\Abstracts;
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 16:01:07 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-08-19 17:41:10 +08:00
|
|
|
use Kiri\Kiri;
|
2021-08-19 15:22:48 +08:00
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
use Swoole\Process;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
abstract class CustomProcess implements \Server\SInterface\CustomProcess
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
protected bool $enableSwooleCoroutine = true;
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 16:15:28 +08:00
|
|
|
protected bool $isStop = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function onProcessStop(): void
|
|
|
|
|
{
|
|
|
|
|
$this->isStop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function checkProcessIsStop(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->isStop === true;
|
|
|
|
|
}
|
2021-08-19 16:01:07 +08:00
|
|
|
|
|
|
|
|
|
2021-08-19 15:22:48 +08:00
|
|
|
/**
|
|
|
|
|
* @param Process $process
|
|
|
|
|
*/
|
|
|
|
|
public function signListen(Process $process): void
|
|
|
|
|
{
|
2021-08-19 17:41:10 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-19 15:22:48 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
go(function () use ($process) {
|
|
|
|
|
$data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
|
|
|
|
|
if ($data) {
|
2021-08-19 17:41:10 +08:00
|
|
|
$lists = Kiri::app()->getProcess();
|
|
|
|
|
foreach ($lists as $process) {
|
|
|
|
|
$process->exit(0);
|
|
|
|
|
}
|
2021-08-19 15:22:48 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 16:01:07 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-08-19 16:15:28 +08:00
|
|
|
protected function exit(): void
|
2021-08-19 16:01:07 +08:00
|
|
|
{
|
2021-08-19 16:15:28 +08:00
|
|
|
putenv('process.status=idle');
|
2021-08-19 16:01:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-08-19 16:15:28 +08:00
|
|
|
#[Pure] public function isWorking(): bool
|
2021-08-19 15:22:48 +08:00
|
|
|
{
|
2021-08-19 16:15:28 +08:00
|
|
|
return env('process.status', 'working') == 'working';
|
2021-08-19 15:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private function waiteExit(Process $process): void
|
|
|
|
|
{
|
|
|
|
|
while ($this->isWorking()) {
|
|
|
|
|
$this->sleep();
|
|
|
|
|
}
|
|
|
|
|
$process->exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private function sleep(): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->enableSwooleCoroutine) {
|
|
|
|
|
Coroutine::sleep(0.1);
|
|
|
|
|
} else {
|
|
|
|
|
usleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|