2024-09-03 14:47:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Processes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Kiri\Di\Context;
|
|
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
abstract class BaseProcess implements OnProcessInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private bool $stop = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
abstract public function onSigterm(): static;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function onShutdown($data): void
|
|
|
|
|
{
|
|
|
|
|
$this->stop = true;
|
|
|
|
|
$value = Context::get('waite:process:message');
|
|
|
|
|
\Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop');
|
|
|
|
|
if (!is_null($value) && Coroutine::exists((int)$value)) {
|
|
|
|
|
Coroutine::cancel((int)$value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|