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;
}
}
+100 -66
View File
@@ -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,79 +30,109 @@ class ServerCommand extends Command
{ {
private Server $server; public AsyncServer $manager;
public State $state;
public EventDispatch $dispatch;
public Router $router;
/** /**
* @return void * @param string|null $name
* @throws ReflectionException * @throws ReflectionException
*/ */
protected function configure(): void public function __construct(string $name = null)
{ {
$this->server = Kiri::getDi()->get(Server::class); parent::__construct($name);
$this->setName('sw:server') $container = Kiri::getDi();
->setDescription('server start|stop|reload|restart') $this->manager = $container->get(AsyncServer::class);
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start') $this->state = $container->get(State::class);
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize'); $this->dispatch = $container->get(EventDispatch::class);
} $this->router = $container->get(Router::class);
}
/** /**
* @param InputInterface $input * @return void
* @param OutputInterface $output */
* @return int protected function configure(): void
* @throws ContainerExceptionInterface {
* @throws NotFoundExceptionInterface $this->setName('sw:server')
* @throws Exception ->setDescription('server start|stop|reload|restart')
*/ ->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
public function execute(InputInterface $input, OutputInterface $output): int ->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
{ }
return match ($input->getArgument('action')) {
'restart' => $this->restart($input),
'stop' => $this->stop(),
'start' => $this->start($input),
default =>
throw new Exception('I don\'t know what I want to do.')
};
}
/** /**
* @param InputInterface $input * @param InputInterface $input
* @return int * @param OutputInterface $output
* @throws ContainerExceptionInterface * @return int
* @throws NotFoundExceptionInterface * @throws ContainerExceptionInterface
*/ * @throws NotFoundExceptionInterface
protected function restart(InputInterface $input): int * @throws Exception
{ */
$this->stop(); public function execute(InputInterface $input, OutputInterface $output): int
$this->start($input); {
return 1; return match ($input->getArgument('action')) {
} 'restart' => $this->restart($input),
'stop' => $this->stop(),
'start' => $this->start($input),
default =>
throw new Exception('I don\'t know what I want to do.')
};
}
/** /**
* @return int * @param InputInterface $input
* @throws ContainerExceptionInterface * @return int
* @throws NotFoundExceptionInterface * @throws ContainerExceptionInterface
*/ * @throws NotFoundExceptionInterface
protected function stop(): int * @throws ReflectionException
{ */
$this->server->shutdown(); protected function restart(InputInterface $input): int
return 1; {
} $this->stop();
$this->start($input);
return 1;
}
/** /**
* @param InputInterface $input * @return int
* @return int * @throws ContainerExceptionInterface
* @throws * @throws NotFoundExceptionInterface
*/ * @throws ReflectionException
protected function start(InputInterface $input): int * @throws Exception
{ */
$this->server->setDaemon((int)($input->getOption('daemon'))); protected function stop(): int
$this->server->start(); {
return 1; $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;
}
/**
* @param InputInterface $input
* @return int
* @throws
*/
protected function start(InputInterface $input): int
{
$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;
}
} }