Files
kiri-http-server/Server.php
T

163 lines
3.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-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;
2023-04-15 23:48:47 +08:00
use Kiri\Router\Router;
2023-04-22 02:04:31 +08:00
use Kiri\Server\Abstracts\ProcessManager;
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;
use Kiri\Server\Abstracts\AsyncServer;
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-04-17 01:01:45 +08:00
private string $class;
2023-04-02 00:32:35 +08:00
2023-04-17 01:01:45 +08:00
private int $daemon = 0;
2023-04-02 00:32:35 +08:00
2023-04-04 13:56:42 +08:00
2023-04-17 01:01:45 +08:00
/**
*
*/
public function __construct()
{
$this->class = Config::get('server.type', AsyncServer::class);
}
2023-04-16 02:15:51 +08:00
2023-04-17 01:01:45 +08:00
/**
* @throws ReflectionException
*/
2023-04-22 02:04:31 +08:00
private function manager(): AsyncServer
2023-04-17 01:01:45 +08:00
{
return Kiri::getDi()->get($this->class);
}
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
*/
2023-04-15 23:48:47 +08:00
public function addProcess($process): void
2022-06-08 15:31:17 +08:00
{
2023-04-22 02:04:31 +08:00
$this->manager()->addProcess($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
{
2023-04-22 02:04:31 +08:00
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
on(OnWorkerStart::class, [$this, 'setWorkerName']);
on(OnTaskerStart::class, [$this, 'setTaskerName']);
$manager = Kiri::getDi()->get(Router::class);
$manager->scan_build_route();
2023-04-17 01:01:45 +08:00
$manager = $this->manager();
$manager->initCoreServers(Config::get('server', [], true), $this->daemon);
$manager->start();
2022-06-08 15:31:17 +08:00
}
2023-04-02 00:32:35 +08:00
2022-06-16 17:38:22 +08:00
/**
* @param OnWorkerStart $onWorkerStart
*/
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
*/
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 ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function shutdown(): void
{
$configs = Config::get('server', [], true);
2023-04-17 01:01:45 +08:00
$state = Kiri::getDi()->get(State::class);
foreach ($this->manager()->sortService($configs['ports'] ?? []) as $config) {
$state->exit($config['port']);
2022-06-08 15:31:17 +08:00
}
2023-04-17 01:01:45 +08:00
2023-04-17 01:48:27 +08:00
$manager = Kiri::getDi()->get(EventDispatch::class);
2023-04-17 01:01:45 +08:00
$manager->dispatch(new OnShutdown());
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 bool
* @throws Exception
*/
public function isRunner(): bool
{
2023-04-17 01:01:45 +08:00
$state = Kiri::getDi()->get(State::class);
return $state->isRunner();
2022-06-08 15:31:17 +08:00
}
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
}