state = Kiri::getDi()->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 = $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('processes', [])); if (\config('reload.hot', false) === true) { $this->asyncServer->addProcess([HotReload::class]); } else { di(Router::class)->scan_build_route(); } $this->asyncServer->initCoreServers(config('server', []), (int)$input->getOption('daemon')); $this->asyncServer->start(); return 1; } }