Files
kiri-http-server/ServerCommand.php
T
2026-07-03 14:25:26 +08:00

151 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Kiri\Server;
use Exception;
use Kiri;
use Kiri\Server\Abstracts\FileWatcher;
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');
/**
* Class Command
* @package Http
*/
class ServerCommand extends Command
{
#[Container(State::class)]
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;
/**
* @return void
*/
protected function configure(): void
{
$this->setName('sw:server')
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
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
* @return int
* @throws
*/
protected function restart(InputInterface $input): int
{
$this->stop();
$this->start($input);
return 1;
}
/**
* @return int
* @throws
*/
protected function stop(): int
{
$configs = config('servers.server', []);
$instances = $this->asyncServer->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
$this->state->exit($config->port);
}
$this->eventDispatch->dispatch(new OnShutdown());
return 1;
}
/**
* @param InputInterface $input
* @return int
* @throws
*/
protected function start(InputInterface $input): int
{
$this->asyncServer->addProcess(config('process', []));
$hotReload = \config('servers.reload.hot', false) === true;
if ($hotReload) {
$this->asyncServer->addProcess([FileWatcher::class]);
}
if (!$hotReload) {
// 非热更模式可在 Master 进程预扫描,Worker 通过 fork 继承扫描结果。
// 热更模式必须让新 Worker 自己加载业务类,避免继承旧类定义后无法重新声明。
di(Router::class)->scan_build_route();
}
$this->asyncServer->initCoreServers(config('servers.server', []), (int)$input->getOption('daemon'));
$this->asyncServer->start();
return 1;
}
}