Files
kiri-http-server/Server.php
T

222 lines
5.4 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;
2022-06-08 15:31:17 +08:00
use Kiri\Events\EventProvider;
2022-01-09 03:49:02 +08:00
use Kiri\Exception\ConfigException;
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-06-08 15:31:17 +08:00
use Kiri\Server\Events\OnWorkerStart;
2022-06-16 17:38:22 +08:00
use Kiri\Server\Events\OnTaskerStart;
2022-01-09 03:49:02 +08:00
use Psr\Container\ContainerExceptionInterface;
2022-06-16 17:38:22 +08:00
use Kiri\Di\ContainerInterface;
2022-01-09 03:49:02 +08:00
use Psr\Container\NotFoundExceptionInterface;
2022-06-16 17:38:22 +08:00
use Kiri\Server\Events\OnWorkerStop;
2022-01-17 18:45:00 +08:00
use ReflectionException;
2022-06-08 15:31:17 +08:00
use Swoole\WebSocket\Server as WsServer;
use Swoole\Server as SServer;
use Swoole\Http\Server as HServer;
2022-01-09 17:56:47 +08:00
use Swoole\Coroutine;
2022-06-16 17:49:06 +08:00
use Kiri\Server\Abstracts\ProcessManager;
use Kiri\Server\Abstracts\AsyncServer;
2022-01-09 03:49:02 +08:00
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
2022-09-07 13:54:21 +08:00
2022-01-09 03:49:02 +08:00
/**
* Class Server
* @package Http
*/
class Server extends HttpService
{
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
private mixed $daemon = 0;
2023-04-02 00:32:35 +08:00
2023-04-04 14:19:48 +08:00
public AsyncServer|CoroutineServer $manager;
2023-04-04 13:56:42 +08:00
2022-06-08 15:31:17 +08:00
/**
* @param State $state
* @param ContainerInterface $container
* @param ProcessManager $processManager
* @param EventDispatch $dispatch
* @param EventProvider $provider
* @param Router $router
* @param array $config
* @throws Exception
*/
public function __construct(public State $state,
public ContainerInterface $container,
public ProcessManager $processManager,
public EventDispatch $dispatch,
public EventProvider $provider,
public Router $router,
array $config = [])
{
parent::__construct($config);
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @return void
* @throws ConfigException
2023-04-04 13:56:42 +08:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-06-08 15:31:17 +08:00
*/
public function init(): void
{
2023-04-04 14:18:36 +08:00
$this->manager = $this->container->get(Config::get('server.type',AsyncServer::class));
2023-04-04 13:56:42 +08:00
2023-04-04 13:55:38 +08:00
$enable_coroutine = Config::get('server.settings.enable_coroutine', false);
if (!$enable_coroutine) {
return;
}
2022-06-08 15:31:17 +08:00
Coroutine::set([
2023-04-02 00:50:42 +08:00
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
2022-06-08 15:31:17 +08:00
'enable_deadlock_check' => FALSE,
2023-04-02 00:32:35 +08:00
'exit_condition' => function () {
2022-06-08 15:31:17 +08:00
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @param $process
2022-10-11 15:15:04 +08:00
* @throws Exception
2022-06-08 15:31:17 +08:00
*/
public function addProcess($process)
{
2022-10-11 15:15:04 +08:00
$this->processManager->add($process);
2022-06-08 15:31:17 +08:00
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function start(): void
{
2022-06-22 18:12:30 +08:00
$this->onHotReload();
2022-06-16 17:38:22 +08:00
$this->manager->initCoreServers(Config::get('server', [], true), $this->daemon);
2022-06-08 15:31:17 +08:00
$this->manager->start();
}
2023-04-02 00:32:35 +08:00
2022-06-16 17:38:22 +08:00
/**
* @return void
2022-06-16 18:23:24 +08:00
* @throws Exception
2022-06-16 17:38:22 +08:00
*/
protected function onWorkerListener(): void
{
2022-06-22 19:16:07 +08:00
$this->provider->on(OnWorkerStop::class, '\Swoole\Timer::clearAll', 9999);
2022-06-16 17:38:22 +08:00
$this->provider->on(OnWorkerStart::class, [$this, 'setWorkerName']);
$this->provider->on(OnTaskerStart::class, [$this, 'setTaskerName']);
}
2023-04-02 00:32:35 +08:00
2022-06-16 17:38:22 +08:00
/**
* @param OnWorkerStart $onWorkerStart
* @throws ConfigException
*/
2022-06-16 19:00:48 +08:00
public function setWorkerName(OnWorkerStart $onWorkerStart): void
2022-06-16 17:38:22 +08:00
{
2022-06-20 18:45:03 +08:00
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
2022-06-16 17:38:22 +08:00
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::WORKER);
2023-04-02 00:32:35 +08:00
2022-06-16 19:00:48 +08:00
Kiri::setProcessName($prefix);
2022-06-16 17:38:22 +08:00
}
2023-04-02 00:32:35 +08:00
2022-06-16 17:38:22 +08:00
/**
2022-06-17 09:57:57 +08:00
* @param OnTaskerStart $onWorkerStart
2022-06-16 17:38:22 +08:00
* @throws ConfigException
*/
2022-06-17 09:57:57 +08:00
public function setTaskerName(OnTaskerStart $onWorkerStart): void
2022-06-16 17:38:22 +08:00
{
2022-06-20 18:45:03 +08:00
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
2022-06-17 09:57:57 +08:00
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::TASK);
2023-04-02 00:32:35 +08:00
2022-06-16 19:00:48 +08:00
Kiri::setProcessName($prefix);
2022-06-16 17:38:22 +08:00
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @return void
* @throws ConfigException
2022-06-16 19:00:48 +08:00
* @throws Exception
2022-06-08 15:31:17 +08:00
*/
public function onHotReload(): void
{
2022-06-16 17:38:22 +08:00
$this->onWorkerListener();
2022-06-08 15:31:17 +08:00
$reload = Config::get('reload.hot', false);
if ($reload !== false) {
2023-03-30 18:24:05 +08:00
$this->provider->on(OnWorkerStart::class, [$this->router, 'scan_build_route']);
2022-09-07 13:48:49 +08:00
$this->manager->addProcess(Kiri\Reload\Inotify::class);
2022-06-08 15:31:17 +08:00
} else {
2023-03-30 18:24:05 +08:00
$this->router->scan_build_route();
2022-06-08 15:31:17 +08:00
}
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +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->dispatch->dispatch(new OnShutdown());
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @return bool
* @throws ConfigException
* @throws Exception
*/
public function isRunner(): bool
{
return $this->state->isRunner();
}
2023-04-02 00:32:35 +08:00
2022-06-08 15:31:17 +08:00
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
2022-01-09 03:49:02 +08:00
}