This commit is contained in:
2023-08-18 14:32:51 +08:00
parent 5150b9027e
commit 436e78aea6
2 changed files with 100 additions and 168 deletions
-102
View File
@@ -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
View File
@@ -6,7 +6,10 @@ namespace Kiri\Server;
use Exception;
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\NotFoundExceptionInterface;
use ReflectionException;
@@ -16,7 +19,8 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
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
@@ -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
* @throws ReflectionException
*/
protected function configure(): void
{
$this->server = Kiri::getDi()->get(Server::class);
$this->setName('sw:server')
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
@@ -68,6 +88,7 @@ class ServerCommand extends Command
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
protected function restart(InputInterface $input): int
{
@@ -81,10 +102,17 @@ class ServerCommand extends Command
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception
*/
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;
}
@@ -96,8 +124,14 @@ class ServerCommand extends Command
*/
protected function start(InputInterface $input): int
{
$this->server->setDaemon((int)($input->getOption('daemon')));
$this->server->start();
$daemon = (int)$input->getOption('daemon');
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;
}