Files
kiri-http-server/Server.php
T

180 lines
4.0 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-12 14:54:51 +08:00
use Kiri;
2022-01-09 03:49:02 +08:00
use Kiri\Events\EventDispatch;
2023-04-15 23:48:47 +08:00
use Kiri\Router\Router;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Events\OnShutdown;
2022-06-16 17:38:22 +08:00
use Kiri\Server\Events\OnTaskerStart;
2023-04-22 02:04:31 +08:00
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnWorkerStop;
2023-07-20 15:01:23 +08:00
use Kiri\Server\Abstracts\CoroutineServer;
2022-01-09 03:49:02 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-04-17 01:01:45 +08:00
use ReflectionException;
2023-04-22 02:04:31 +08:00
use Swoole\Timer;
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
*/
2023-04-15 23:48:47 +08:00
class Server
2022-01-09 03:49:02 +08:00
{
2023-04-02 00:32:35 +08:00
2023-06-12 15:33:47 +08:00
/**
* @var string|mixed
*/
2023-06-12 15:34:08 +08:00
private string $class;
2023-04-02 00:32:35 +08:00
2023-06-12 15:33:47 +08:00
/**
* @var int
*/
2023-06-12 15:34:08 +08:00
private int $daemon = 0;
/**
*
*/
public function __construct()
{
2023-07-20 15:01:23 +08:00
$this->class = \config('server.type', CoroutineServer::class);
2023-06-12 15:34:08 +08:00
}
/**
* @throws ReflectionException
*/
2023-07-20 15:01:23 +08:00
private function manager(): CoroutineServer
2023-06-12 15:34:08 +08:00
{
return Kiri::getDi()->get($this->class);
}
/**
* @param $process
* @throws Exception
*/
public function addProcess($process): void
{
$this->manager()->addProcess($process);
}
/**
* @return void
* @throws Exception
*/
public function start(): void
{
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
on(OnWorkerStart::class, [$this, 'setWorkerName']);
on(OnTaskerStart::class, [$this, 'setTaskerName']);
2023-07-06 16:00:02 +08:00
if (\config('reload.hot') === false) {
$this->hotLoad();
} else {
on(OnWorkerStart::class, [$this, 'hotLoad']);
$this->addProcess(HotReload::class);
}
2023-06-12 15:34:08 +08:00
$manager = $this->manager();
2023-07-06 16:00:02 +08:00
$manager->initCoreServers(\config('server', []), $this->daemon);
2023-06-12 15:34:08 +08:00
$manager->start();
}
2023-07-06 16:00:02 +08:00
/**
* @return void
* @throws ReflectionException
*/
public function hotLoad(): void
{
$manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route();
}
2023-06-12 15:34:08 +08:00
/**
* @param OnWorkerStart $onWorkerStart
*/
public function setWorkerName(OnWorkerStart $onWorkerStart): void
{
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::WORKER);
Kiri::setProcessName($prefix);
}
/**
* @param OnTaskerStart $onWorkerStart
*/
public function setTaskerName(OnTaskerStart $onWorkerStart): void
{
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::TASK);
Kiri::setProcessName($prefix);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function shutdown(): void
{
2023-07-06 16:00:02 +08:00
$configs = \config('server', []);
2023-06-12 15:34:08 +08:00
$state = Kiri::getDi()->get(State::class);
2023-07-06 16:00:02 +08:00
$instances = $this->manager()->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
2023-06-12 17:06:20 +08:00
$state->exit($config->port);
2023-06-12 15:34:08 +08:00
}
$manager = Kiri::getDi()->get(EventDispatch::class);
$manager->dispatch(new OnShutdown());
}
/**
* @return bool
* @throws Exception
*/
public function isRunner(): bool
{
$state = Kiri::getDi()->get(State::class);
return $state->isRunner();
}
/**
* @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
}