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

138 lines
2.3 KiB
PHP
Raw Normal View History

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;
/**
* @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');
2024-08-29 17:01:07 +08:00
\Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop');
2023-11-30 11:40:04 +08:00
if (!is_null($value) && Coroutine::exists((int)$value)) {
Coroutine::cancel((int)$value);
}
}
2022-01-09 03:49:02 +08:00
}