2024-09-03 14:47:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Processes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Swoole\Coroutine;
|
2024-09-04 09:40:31 +08:00
|
|
|
use Swoole\Process;
|
2024-09-03 14:47:27 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-09-03 15:05:18 +08:00
|
|
|
abstract class AbstractProcess implements OnProcessInterface
|
2024-09-03 14:47:27 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private bool $stop = false;
|
|
|
|
|
|
|
|
|
|
|
2024-09-04 09:40:31 +08:00
|
|
|
public Process $process;
|
|
|
|
|
|
|
|
|
|
|
2024-09-03 14:47:27 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $redirect_stdin_and_stdout = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
protected int $pipe_type = SOCK_DGRAM;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $enable_coroutine = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $enable_queue = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $name = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isEnableQueue(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->enable_queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName(): string
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->name)) {
|
|
|
|
|
$this->name = uniqid('p.');
|
|
|
|
|
}
|
|
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isStop(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->stop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getRedirectStdinAndStdout(): bool
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function stop(): void
|
|
|
|
|
{
|
|
|
|
|
$this->stop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-09-04 09:40:31 +08:00
|
|
|
* @return void
|
2024-09-03 14:47:27 +08:00
|
|
|
*/
|
2024-09-04 09:40:31 +08:00
|
|
|
abstract public function onSigterm(): void;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Process $process
|
|
|
|
|
* @return AbstractProcess
|
|
|
|
|
*/
|
|
|
|
|
public function onShutdown(Process $process): static
|
|
|
|
|
{
|
|
|
|
|
$this->process = $process;
|
|
|
|
|
if ($this->enable_coroutine) {
|
|
|
|
|
Coroutine::create(fn () => $this->coroutineWaitSignal());
|
|
|
|
|
} else {
|
|
|
|
|
pcntl_signal(SIGTERM, [$this, 'pointWaitSignal']);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2024-09-03 14:47:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2024-09-04 09:40:31 +08:00
|
|
|
private function pointWaitSignal($data): void
|
2024-09-03 14:47:27 +08:00
|
|
|
{
|
|
|
|
|
$this->stop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-04 09:40:31 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function coroutineWaitSignal(): void
|
|
|
|
|
{
|
|
|
|
|
$value = Coroutine::waitSignal(SIGTERM);
|
|
|
|
|
if ($value) {
|
|
|
|
|
$this->stop = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 14:47:27 +08:00
|
|
|
}
|