state = $container->get(State::class); } /** * @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('server', []); $instances = Kiri::getDi()->get(AsyncServer::class)->sortService($configs['ports'] ?? []); foreach ($instances as $config) { $this->state->exit($config->port); } $dispatch = Kiri::getDi()->get(EventDispatch::class); $dispatch->dispatch(new OnShutdown()); return 1; } /** * @param InputInterface $input * @return int * @throws */ protected function start(InputInterface $input): int { $daemon = (int)$input->getOption('daemon'); $manager = Kiri::getDi()->get(AsyncServer::class); $router = Kiri::getDi()->get(Router::class); $router->scan_build_route(); $manager->addProcess(config('processes', [])); $manager->initCoreServers(config('server', []), $daemon); $manager->start(); return 1; } }