Files
kiri-http-server/Abstracts/BaseProcess.php
T

112 lines
1.4 KiB
PHP
Raw Normal View History

2021-11-03 15:17:52 +08:00
<?php
namespace Server\Abstracts;
2021-12-06 17:58:11 +08:00
use Kiri\Context;
2021-11-18 11:37:12 +08:00
use Server\Contract\OnProcessInterface;
2021-11-03 15:17:52 +08:00
use Swoole\Coroutine;
use Swoole\Process;
/**
*
*/
abstract class BaseProcess implements OnProcessInterface
{
2021-11-03 17:39:08 +08:00
protected bool $isStop = false;
2021-11-03 15:17:52 +08:00
2021-12-17 04:20:20 +08:00
protected bool $redirect_stdin_and_stdout = FALSE;
2021-11-03 17:39:08 +08:00
protected int $pipe_type = SOCK_DGRAM;
protected bool $enable_coroutine = true;
2021-11-03 17:43:34 +08:00
public string $name = 'swoole process.';
2021-11-03 17:39:08 +08:00
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return bool
*/
public function isStop(): bool
{
return $this->isStop;
}
/**
2021-12-17 04:20:20 +08:00
* @return bool
*/
public function getRedirectStdinAndStdout(): bool
{
2021-11-03 17:39:08 +08:00
return $this->redirect_stdin_and_stdout;
}
/**
* @return int
*/
public function getPipeType(): int
{
return $this->pipe_type;
}
/**
* @return bool
*/
public function isEnableCoroutine(): bool
{
return $this->enable_coroutine;
}
2021-11-03 15:17:52 +08:00
/**
*
*/
public function onProcessStop(): void
{
$this->isStop = true;
}
2021-12-06 17:58:11 +08:00
/**
*
*/
public function onSigterm(): static
{
if (!Context::inCoroutine()) {
Process::signal(SIGTERM, fn($data) => $this->onShutdown($data));
} else {
Coroutine::create(function () {
$data = Coroutine::waitSignal(SIGTERM, -1);
if ($data) {
$this->onShutdown($data);
}
});
}
return $this;
}
/**
* @param $data
*/
protected function onShutdown($data): void
{
$this->isStop = true;
}
2021-11-03 15:17:52 +08:00
}