2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server\Abstracts;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2022-12-12 17:31:11 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-08-14 21:09:44 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2023-08-14 22:14:35 +08:00
|
|
|
use Kiri\Error\StdoutLogger;
|
2022-01-10 11:39:55 +08:00
|
|
|
use Kiri\Server\Contract\OnProcessInterface;
|
2023-08-16 10:26:48 +08:00
|
|
|
use Psr\Container\ContainerInterface;
|
2023-08-14 21:09:44 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
abstract class BaseProcess implements OnProcessInterface
|
|
|
|
|
{
|
|
|
|
|
|
2023-11-30 11:40:04 +08:00
|
|
|
private bool $stop = false;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2023-11-30 11:40:04 +08:00
|
|
|
protected bool $redirect_stdin_and_stdout = FALSE;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2023-11-30 11:40:04 +08:00
|
|
|
protected int $pipe_type = SOCK_DGRAM;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2023-11-30 11:40:04 +08:00
|
|
|
protected bool $enable_coroutine = false;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected bool $enable_queue = false;
|
|
|
|
|
|
|
|
|
|
|
2023-08-14 21:09:44 +08:00
|
|
|
/**
|
2023-08-14 22:14:35 +08:00
|
|
|
* @var StdoutLogger
|
2023-08-14 21:09:44 +08:00
|
|
|
*/
|
|
|
|
|
#[Container(LoggerInterface::class)]
|
2023-08-14 22:14:35 +08:00
|
|
|
public StdoutLogger $logger;
|
2023-08-14 21:09:44 +08:00
|
|
|
|
|
|
|
|
|
2023-08-16 10:26:48 +08:00
|
|
|
/**
|
|
|
|
|
* @var \Kiri\Di\Container
|
|
|
|
|
*/
|
|
|
|
|
#[Container(ContainerInterface::class)]
|
|
|
|
|
public \Kiri\Di\Container $container;
|
|
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2023-11-30 11:40:04 +08:00
|
|
|
public string $name = '';
|
|
|
|
|
|
|
|
|
|
|
2024-06-20 17:30:49 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isEnableQueue(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->enable_queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-11-30 11:40:04 +08:00
|
|
|
/**
|
|
|
|
|
* @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;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2023-07-31 23:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-11-30 11:40:04 +08:00
|
|
|
protected function onShutdown($data): void
|
|
|
|
|
{
|
|
|
|
|
$this->stop = true;
|
|
|
|
|
$value = Context::get('waite:process:message');
|
|
|
|
|
$this->logger->alert('Process ' . $this->getName() . ' stop');
|
|
|
|
|
if (!is_null($value) && Coroutine::exists((int)$value)) {
|
|
|
|
|
Coroutine::cancel((int)$value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|