Files
kiri-http-server/ServerCommand.php
T

151 lines
3.9 KiB
PHP
Raw Normal View History

2022-01-09 03:49:02 +08:00
<?php
declare(strict_types=1);
2022-01-10 11:39:55 +08:00
namespace Kiri\Server;
2022-01-09 03:49:02 +08:00
use Exception;
2022-02-21 11:02:10 +08:00
use Kiri;
2026-04-17 13:56:30 +08:00
use Kiri\Server\Abstracts\FileWatcher;
2024-11-18 11:24:08 +08:00
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Events\EventProvider;
2023-08-18 14:32:51 +08:00
use Kiri\Events\EventDispatch;
use Kiri\Router\Router;
use Kiri\Server\Abstracts\AsyncServer;
use Kiri\Server\Events\OnShutdown;
2024-11-18 11:24:08 +08:00
use Psr\Container\ContainerInterface;
2022-01-09 03:49:02 +08:00
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;
2023-12-12 10:56:42 +08:00
use function config;
2024-11-18 11:24:08 +08:00
use Kiri\Server\Abstracts\HotReload;
use Kiri\Di\Inject\Container;
2022-01-09 03:49:02 +08:00
2023-08-18 14:32:51 +08:00
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
2023-07-06 16:20:46 +08:00
2022-01-09 03:49:02 +08:00
/**
* Class Command
* @package Http
*/
class ServerCommand extends Command
{
2025-07-14 17:55:15 +08:00
#[Container(State::class)]
2024-09-04 11:43:24 +08:00
public State $state;
2023-08-18 14:32:51 +08:00
2024-11-18 11:24:08 +08:00
/**
* @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;
2023-08-18 14:32:51 +08:00
/**
* @return void
*/
protected function configure(): void
{
$this->setName('sw:server')
2023-11-29 14:49:18 +08:00
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
2023-08-18 14:32:51 +08:00
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
2023-12-12 15:35:34 +08:00
* @throws
2023-08-18 14:32:51 +08:00
*/
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
2023-12-12 15:35:34 +08:00
* @throws
2023-08-18 14:32:51 +08:00
*/
protected function restart(InputInterface $input): int
{
$this->stop();
$this->start($input);
return 1;
}
/**
* @return int
2023-12-12 15:35:34 +08:00
* @throws
2023-08-18 14:32:51 +08:00
*/
protected function stop(): int
{
2025-12-18 15:39:40 +08:00
$configs = config('servers.server', []);
2024-11-18 11:24:08 +08:00
$instances = $this->asyncServer->sortService($configs['ports'] ?? []);
2023-08-18 14:32:51 +08:00
foreach ($instances as $config) {
$this->state->exit($config->port);
}
2024-11-18 11:24:08 +08:00
$this->eventDispatch->dispatch(new OnShutdown());
2023-08-18 14:32:51 +08:00
return 1;
}
2026-06-24 20:17:54 +08:00
/**
* @param InputInterface $input
* @return int
* @throws
*/
protected function start(InputInterface $input): int
{
$this->asyncServer->addProcess(config('process', []));
2026-07-03 14:25:26 +08:00
$hotReload = \config('servers.reload.hot', false) === true;
if ($hotReload) {
2026-06-24 20:17:54 +08:00
$this->asyncServer->addProcess([FileWatcher::class]);
}
2026-07-03 14:25:26 +08:00
if (!$hotReload) {
// 非热更模式可在 Master 进程预扫描,Worker 通过 fork 继承扫描结果。
// 热更模式必须让新 Worker 自己加载业务类,避免继承旧类定义后无法重新声明。
di(Router::class)->scan_build_route();
}
2026-06-24 20:17:54 +08:00
$this->asyncServer->initCoreServers(config('servers.server', []), (int)$input->getOption('daemon'));
$this->asyncServer->start();
return 1;
}
2022-06-16 17:38:22 +08:00
2022-01-09 03:49:02 +08:00
}