Files
kiri-http-server/Server.php
T

211 lines
5.7 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;
2022-01-09 03:49:02 +08:00
use Exception;
2022-01-13 18:41:15 +08:00
use JetBrains\PhpStorm\Pure;
2022-01-12 14:54:51 +08:00
use Kiri;
2022-01-09 03:49:02 +08:00
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
2022-02-23 16:32:07 +08:00
use Kiri\Message\Constrict\Request;
use Kiri\Message\Constrict\RequestInterface;
use Kiri\Message\Constrict\Response;
use Kiri\Message\Constrict\ResponseInterface;
2022-01-12 11:20:33 +08:00
use Kiri\Message\Handler\Abstracts\HttpService;
2022-02-18 15:28:28 +08:00
use Kiri\Message\Handler\Router;
2022-02-14 10:55:56 +08:00
use Kiri\Server\Events\OnServerBeforeStart;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Events\OnShutdown;
2022-01-09 03:49:02 +08:00
use Psr\Container\ContainerExceptionInterface;
2022-03-03 18:11:24 +08:00
use Psr\Container\ContainerInterface;
2022-01-09 03:49:02 +08:00
use Psr\Container\NotFoundExceptionInterface;
2022-01-17 18:45:00 +08:00
use ReflectionException;
2022-01-09 17:56:47 +08:00
use Swoole\Coroutine;
2022-05-31 11:37:00 +08:00
use Kiri\Server\Events\OnBeforeShutdown;
2022-04-29 14:46:45 +08:00
use Kiri\Events\EventProvider;
use Kiri\Server\Events\OnWorkerStart;
2022-01-09 03:49:02 +08:00
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
/**
* Class Server
* @package Http
*/
class Server extends HttpService
{
2022-04-29 14:46:45 +08:00
private array $process = [];
2022-01-09 03:49:02 +08:00
2022-04-29 14:46:45 +08:00
private mixed $daemon = 0;
2022-01-09 03:49:02 +08:00
2022-04-10 03:37:58 +08:00
/**
* @param State $state
* @param ServerManager $manager
* @param ContainerInterface $container
* @param ProcessManager $processManager
* @param EventDispatch $eventDispatch
2022-04-29 14:46:45 +08:00
* @param EventProvider $eventProvider
2022-04-10 03:37:58 +08:00
* @param Router $router
* @param array $config
* @throws Exception
*/
2022-04-29 14:46:45 +08:00
public function __construct(public State $state,
public ServerManager $manager,
public ContainerInterface $container,
public ProcessManager $processManager,
public EventDispatch $eventDispatch,
public EventProvider $eventProvider,
public Router $router,
array $config = [])
{
parent::__construct($config);
}
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function init(): void
{
$this->container->mapping(ResponseInterface::class, Response::class);
$this->container->mapping(RequestInterface::class, Request::class);
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
if (!$enable_coroutine) {
return;
}
Coroutine::set([
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
'enable_deadlock_check' => FALSE,
'exit_condition' => function () {
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
}
/**
* @param $process
*/
public function addProcess($process)
{
$this->process[] = $process;
}
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-04-10 03:37:58 +08:00
* @throws Exception
2022-04-29 14:46:45 +08:00
*/
public function start(): void
{
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
}
$reload = Config::get('reload.hot', false);
if ($reload !== false) {
2022-05-03 06:56:28 +08:00
$this->eventProvider->on(OnWorkerStart::class, [$this, 'onWorkerStart']);
2022-01-09 03:49:02 +08:00
2022-05-03 06:56:28 +08:00
$this->process[] = Scaner::class;
2022-04-29 14:46:45 +08:00
} else {
2022-05-03 06:56:28 +08:00
$this->onWorkerStart();
2022-04-29 14:46:45 +08:00
}
2022-05-31 11:37:00 +08:00
pcntl_signal(SIGINT, function () {
try {
$this->eventDispatch->dispatch(new OnBeforeShutdown());
}catch (\Throwable $exception) {
$this->logger->error($exception->getMessage());
}
$this->manager->getServer()->shutdown();
});
2022-01-09 03:49:02 +08:00
2022-04-29 14:46:45 +08:00
$processes = array_merge($this->process, Config::get('processes', []));
2022-04-10 03:37:58 +08:00
2022-04-29 14:46:45 +08:00
$this->processManager->batch($processes);
2022-01-12 11:20:33 +08:00
2022-04-29 14:46:45 +08:00
$this->eventDispatch->dispatch(new OnServerBeforeStart());
2022-01-09 03:49:02 +08:00
2022-04-29 14:46:45 +08:00
$this->manager->start();
}
2022-01-13 18:36:25 +08:00
2022-04-29 14:46:45 +08:00
2022-05-03 06:56:28 +08:00
/**
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function onWorkerStart(): void
{
scan_directory(MODEL_PATH, 'app\Model');
$this->router->scan_build_route();
}
2022-04-29 14:46:45 +08:00
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function shutdown(): void
{
$configs = Config::get('server', [], true);
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
$this->state->exit($config['port']);
}
$this->eventDispatch->dispatch(new OnShutdown());
}
2022-01-12 11:20:33 +08:00
2022-04-29 14:46:45 +08:00
/**
* @return bool
* @throws ConfigException
2022-04-10 03:37:58 +08:00
* @throws Exception
2022-04-29 14:46:45 +08:00
*/
public function isRunner(): bool
{
return $this->state->isRunner();
}
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{
return $this->manager->getServer();
}
2022-01-09 03:49:02 +08:00
}