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-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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected bool $isStop = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected bool $redirect_stdin_and_stdout = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int $pipe_type = SOCK_DGRAM;
|
|
|
|
|
|
|
|
|
|
|
2022-07-11 16:34:13 +08:00
|
|
|
protected bool $enable_coroutine = false;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-06-17 12:27:33 +08:00
|
|
|
public string $name = '';
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName(): string
|
|
|
|
|
{
|
2022-06-17 12:27:33 +08:00
|
|
|
if (empty($this->name)) {
|
2022-06-22 18:55:51 +08:00
|
|
|
$this->name = uniqid('p.');
|
2022-06-17 12:27:33 +08:00
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isStop(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->isStop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
2022-01-12 11:20:33 +08:00
|
|
|
*/
|
2022-01-09 03:49:02 +08:00
|
|
|
public function getRedirectStdinAndStdout(): bool
|
2022-01-12 11:20:33 +08:00
|
|
|
{
|
2022-01-09 03:49:02 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function onProcessStop(): void
|
|
|
|
|
{
|
|
|
|
|
$this->isStop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-07-11 17:45:15 +08:00
|
|
|
* @return $this
|
2022-01-09 03:49:02 +08:00
|
|
|
*/
|
2022-07-11 17:45:15 +08:00
|
|
|
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
|
|
|
|
|
*/
|
2022-01-09 03:49:02 +08:00
|
|
|
protected function onShutdown($data): void
|
|
|
|
|
{
|
|
|
|
|
$this->isStop = true;
|
2023-04-03 11:08:10 +08:00
|
|
|
$value = Context::get('waite:process:message');
|
2023-08-14 21:09:44 +08:00
|
|
|
$this->logger->alert('Process ' . $this->getName() . ' stop');
|
2022-04-28 11:56:19 +08:00
|
|
|
if (!is_null($value) && Coroutine::exists((int)$value)) {
|
|
|
|
|
Coroutine::cancel((int)$value);
|
2022-01-12 11:20:33 +08:00
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|