qqq
This commit is contained in:
-102
@@ -1,102 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Kiri\Server;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri;
|
|
||||||
use Kiri\Events\EventDispatch;
|
|
||||||
use Kiri\Router\Router;
|
|
||||||
use Kiri\Server\Events\OnShutdown;
|
|
||||||
use Kiri\Server\Abstracts\AsyncServer;
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
|
|
||||||
|
|
||||||
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Server
|
|
||||||
* @package Http
|
|
||||||
*/
|
|
||||||
class Server
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
private int $daemon = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param AsyncServer $manager
|
|
||||||
* @param State $state
|
|
||||||
* @param EventDispatch $dispatch
|
|
||||||
* @param Router $router
|
|
||||||
*/
|
|
||||||
public function __construct(public AsyncServer $manager,
|
|
||||||
public State $state,
|
|
||||||
public EventDispatch $dispatch,
|
|
||||||
public Router $router)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function start(): void
|
|
||||||
{
|
|
||||||
if (\config('reload.hot', false) === true) {
|
|
||||||
$this->manager->addProcess(HotReload::class);
|
|
||||||
} else {
|
|
||||||
$this->router->scan_build_route();
|
|
||||||
}
|
|
||||||
$this->manager->initCoreServers(\config('server', []), $this->daemon);
|
|
||||||
$this->manager->start();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function shutdown(): void
|
|
||||||
{
|
|
||||||
$configs = \config('server', []);
|
|
||||||
$instances = $this->manager->sortService($configs['ports'] ?? []);
|
|
||||||
foreach ($instances as $config) {
|
|
||||||
$this->state->exit($config->port);
|
|
||||||
}
|
|
||||||
$this->dispatch->dispatch(new OnShutdown());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+42
-8
@@ -6,7 +6,10 @@ namespace Kiri\Server;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Events\EventDispatch;
|
||||||
|
use Kiri\Router\Router;
|
||||||
|
use Kiri\Server\Abstracts\AsyncServer;
|
||||||
|
use Kiri\Server\Events\OnShutdown;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@@ -16,7 +19,8 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP','http');
|
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
|
||||||
|
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Command
|
* Class Command
|
||||||
@@ -26,16 +30,32 @@ class ServerCommand extends Command
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private Server $server;
|
public AsyncServer $manager;
|
||||||
|
public State $state;
|
||||||
|
public EventDispatch $dispatch;
|
||||||
|
public Router $router;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $name
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function __construct(string $name = null)
|
||||||
|
{
|
||||||
|
parent::__construct($name);
|
||||||
|
$container = Kiri::getDi();
|
||||||
|
$this->manager = $container->get(AsyncServer::class);
|
||||||
|
$this->state = $container->get(State::class);
|
||||||
|
$this->dispatch = $container->get(EventDispatch::class);
|
||||||
|
$this->router = $container->get(Router::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
*/
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
{
|
{
|
||||||
$this->server = Kiri::getDi()->get(Server::class);
|
|
||||||
$this->setName('sw:server')
|
$this->setName('sw:server')
|
||||||
->setDescription('server start|stop|reload|restart')
|
->setDescription('server start|stop|reload|restart')
|
||||||
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
||||||
@@ -68,6 +88,7 @@ class ServerCommand extends Command
|
|||||||
* @return int
|
* @return int
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
protected function restart(InputInterface $input): int
|
protected function restart(InputInterface $input): int
|
||||||
{
|
{
|
||||||
@@ -81,10 +102,17 @@ class ServerCommand extends Command
|
|||||||
* @return int
|
* @return int
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function stop(): int
|
protected function stop(): int
|
||||||
{
|
{
|
||||||
$this->server->shutdown();
|
$configs = \config('server', []);
|
||||||
|
$instances = $this->manager->sortService($configs['ports'] ?? []);
|
||||||
|
foreach ($instances as $config) {
|
||||||
|
$this->state->exit($config->port);
|
||||||
|
}
|
||||||
|
$this->dispatch->dispatch(new OnShutdown());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,8 +124,14 @@ class ServerCommand extends Command
|
|||||||
*/
|
*/
|
||||||
protected function start(InputInterface $input): int
|
protected function start(InputInterface $input): int
|
||||||
{
|
{
|
||||||
$this->server->setDaemon((int)($input->getOption('daemon')));
|
$daemon = (int)$input->getOption('daemon');
|
||||||
$this->server->start();
|
if (\config('reload.hot', false) === true) {
|
||||||
|
$this->manager->addProcess(HotReload::class);
|
||||||
|
} else {
|
||||||
|
$this->router->scan_build_route();
|
||||||
|
}
|
||||||
|
$this->manager->initCoreServers(\config('server', []), $daemon);
|
||||||
|
$this->manager->start();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user