Files
kiri-http-server/ServerCommand.php
T

121 lines
2.7 KiB
PHP
Raw Normal View History

2021-11-03 15:17:52 +08:00
<?php
declare(strict_types=1);
namespace Server;
2021-12-06 13:51:54 +08:00
use Note\Inject;
2021-11-03 15:17:52 +08:00
use Exception;
use Kiri\Abstracts\Config;
2021-11-19 14:46:07 +08:00
use Kiri\Events\EventDispatch;
2021-11-03 15:17:52 +08:00
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
2021-12-23 18:22:26 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2021-12-06 13:51:54 +08:00
use Server\Events\OnServerBeforeStart;
2021-11-03 15:17:52 +08:00
use Swoole\Coroutine;
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;
/**
* Class Command
* @package Http
*/
class ServerCommand extends Command
{
const ACTIONS = ['start', 'stop', 'restart'];
/**
2021-11-19 14:46:07 +08:00
* @var EventDispatch
2021-11-03 15:17:52 +08:00
*/
2021-11-19 14:46:07 +08:00
#[Inject(EventDispatch::class)]
public EventDispatch $eventProvider;
2021-11-03 15:17:52 +08:00
/**
*
*/
protected function configure()
{
$this->setName('sw:server')
->setDescription('server start|stop|reload|restart')
2021-11-04 18:50:42 +08:00
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
2021-11-05 01:03:31 +08:00
->addOption('daemon', 'd', InputOption::VALUE_OPTIONAL, 'is run daemonize');
2021-11-03 15:17:52 +08:00
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
2021-12-23 18:22:26 +08:00
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ReflectionException
2021-11-03 15:17:52 +08:00
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
2021-11-17 16:39:12 +08:00
$manager = Kiri::app()->getServer();
$manager->setDaemon((int)!is_null($input->getOption('daemon')));
if (is_null($input->getArgument('action'))) {
$input->setArgument('action', 'restart');
}
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
throw new Exception('I don\'t know what I want to do.');
2021-11-03 15:17:52 +08:00
}
2021-12-06 13:51:54 +08:00
if ($manager->isRunner() && $input->getArgument('action') == 'start') {
throw new Exception('Service is running. Please use restart.');
}
$manager->shutdown();
if ($input->getArgument('action') == 'stop') {
return 0;
2021-11-17 16:39:12 +08:00
}
return $this->generate_runtime_builder($manager);
2021-11-03 15:17:52 +08:00
}
/**
* @throws ConfigException
*/
private function configure_set()
{
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
if ($enable_coroutine != true) {
return;
}
Coroutine::set([
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
'enable_deadlock_check' => FALSE,
2021-12-06 13:51:54 +08:00
'exit_condition' => function () {
2021-11-03 15:17:52 +08:00
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
}
/**
* @param $manager
2021-11-17 16:39:12 +08:00
* @return int
2021-11-03 15:17:52 +08:00
* @throws ConfigException
2021-11-04 16:31:54 +08:00
* @throws Exception
2021-11-03 15:17:52 +08:00
*/
2021-11-17 16:39:12 +08:00
private function generate_runtime_builder($manager): int
2021-11-03 15:17:52 +08:00
{
$this->configure_set();
2021-11-04 16:40:08 +08:00
Kiri::app()->getRouter()->read_files();
2021-12-06 11:47:12 +08:00
2021-11-03 15:17:52 +08:00
$manager->start();
2021-11-17 16:39:12 +08:00
return 1;
2021-11-03 15:17:52 +08:00
}
}