This commit is contained in:
xl
2024-11-18 11:24:08 +08:00
parent ac629ece15
commit a5378658ee
3 changed files with 175 additions and 9 deletions
+42 -9
View File
@@ -6,16 +6,21 @@ namespace Kiri\Server;
use Exception;
use Kiri;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Events\EventProvider;
use Kiri\Events\EventDispatch;
use Kiri\Router\Router;
use Kiri\Server\Abstracts\AsyncServer;
use Kiri\Server\Events\OnShutdown;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function config;
use Kiri\Server\Abstracts\HotReload;
use Kiri\Di\Inject\Container;
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -31,6 +36,34 @@ class ServerCommand extends Command
public State $state;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
/**
* @var AsyncServer
*/
#[Container(AsyncServer::class)]
public AsyncServer $asyncServer;
/**
* @var EventDispatch
*/
#[Container(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @param string|null $name
* @throws Exception
@@ -92,12 +125,11 @@ class ServerCommand extends Command
protected function stop(): int
{
$configs = config('server', []);
$instances = Kiri::getDi()->get(AsyncServer::class)->sortService($configs['ports'] ?? []);
$instances = $this->asyncServer->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
$this->state->exit($config->port);
}
$dispatch = Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnShutdown());
$this->eventDispatch->dispatch(new OnShutdown());
return 1;
}
@@ -109,12 +141,13 @@ class ServerCommand extends Command
*/
protected function start(InputInterface $input): int
{
$manager = Kiri::getDi()->get(AsyncServer::class);
$router = Kiri::getDi()->get(Router::class);
$router->scan_build_route();
$manager->addProcess(config('processes', []));
$manager->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
$manager->start();
$this->eventProvider->on(OnWorkerStart::class, [di(Router::class), 'scan_build_route']);
$this->asyncServer->addProcess(config('processes', []));
if (\config('reload.hot', false) === true) {
$this->asyncServer->addProcess([HotReload::class]);
}
$this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon'));
$this->asyncServer->start();
return 1;
}