This commit is contained in:
2021-08-19 16:01:07 +08:00
parent 6fa51c2fe3
commit fb453f674d
5 changed files with 76 additions and 9 deletions
+44 -1
View File
@@ -3,6 +3,7 @@
namespace Server\Abstracts;
use JetBrains\PhpStorm\Pure;
use Swoole\Coroutine;
use Swoole\Process;
@@ -16,20 +17,31 @@ abstract class CustomProcess implements \Server\SInterface\CustomProcess
protected bool $enableSwooleCoroutine = true;
/** @var Coroutine\Channel */
protected Coroutine\Channel $channel;
/**
* @param Process $process
*/
public function signListen(Process $process): void
{
$this->channel = new Coroutine\Channel(1);
$this->channel->push(1);
if (!$this->enableSwooleCoroutine) {
Process::signal(SIGTERM | SIGKILL, function ($signo)
use ($process) {
putenv('processStatus=exit');
$this->waiteExit($process);
});
} else {
go(function () use ($process) {
$data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
if ($data) {
putenv('processStatus=exit');
$this->waiteExit($process);
}
});
@@ -37,9 +49,40 @@ abstract class CustomProcess implements \Server\SInterface\CustomProcess
}
/**
* @return string
*/
#[Pure] protected function getStatus(): string
{
return env('processStatus', 'working');
}
/**
* @return bool
*/
#[Pure] protected function isExit(): bool
{
return $this->getStatus() == 'exit';
}
/**
*
*/
protected function exit()
{
$this->channel->pop();
$this->channel->close();
}
/**
* @return bool
*/
public function isWorking(): bool
{
return false;
return $this->channel->isEmpty();
}