Files
kiri-core/http-server/ServerCommand.php
T

122 lines
3.0 KiB
PHP
Raw Normal View History

2020-09-03 00:15:57 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-03 00:15:57 +08:00
2021-09-02 13:59:00 +08:00
namespace Server;
2020-09-03 00:15:57 +08:00
2021-08-29 04:44:33 +08:00
use Annotation\Inject;
2020-09-07 18:11:36 +08:00
use Exception;
2021-09-07 15:24:42 +08:00
use Kiri\Abstracts\Config;
2021-08-29 01:18:42 +08:00
use Kiri\Events\EventProvider;
2021-09-24 18:53:14 +08:00
use Kiri\Exception\ConfigException;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-09-24 18:53:14 +08:00
use Server\Abstracts\OnTaskerStart as TaskerDispatch;
use Server\Abstracts\OnWorkerStart as WorkerDispatch;
2021-08-29 04:57:50 +08:00
use Server\Events\OnBeforeWorkerStart;
2021-09-19 16:48:14 +08:00
use Server\Events\OnTaskerStart;
2021-09-02 11:13:11 +08:00
use Server\Events\OnWorkerStart;
2021-08-29 04:57:50 +08:00
use Server\Worker\OnServerWorker;
2021-09-20 00:52:44 +08:00
use Swoole\Coroutine;
2021-09-02 13:59:00 +08:00
use Symfony\Component\Console\Command\Command;
2021-09-02 13:48:57 +08:00
use Symfony\Component\Console\Input\InputArgument;
2021-09-02 11:26:11 +08:00
use Symfony\Component\Console\Input\InputInterface;
2021-10-19 17:04:47 +08:00
use Symfony\Component\Console\Input\InputOption;
2021-09-02 11:26:11 +08:00
use Symfony\Component\Console\Output\OutputInterface;
2020-09-03 00:15:57 +08:00
/**
* Class Command
2021-08-17 16:43:50 +08:00
* @package Http
2020-09-03 00:15:57 +08:00
*/
2021-09-02 13:59:00 +08:00
class ServerCommand extends Command
2020-09-03 00:15:57 +08:00
{
2021-09-02 11:13:11 +08:00
2021-09-24 18:53:14 +08:00
const ACTIONS = ['start', 'stop', 'restart'];
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
*
*/
protected function configure()
{
$this->setName('sw:server')
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::REQUIRED)
2021-10-19 17:10:12 +08:00
->addOption('daemon', 'd', InputOption::VALUE_OPTIONAL,'is run daemonize',-1);
2021-09-24 18:53:14 +08:00
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
$manager = Kiri::app()->getServer();
2021-10-19 17:09:08 +08:00
$manager->setDaemon((int)is_null($input->getOption('daemon')));
2021-09-24 18:53:14 +08:00
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
throw new Exception('I don\'t know what I want to do.');
}
if ($manager->isRunner() && $input->getArgument('action') == 'start') {
throw new Exception('Service is running. Please use restart.');
}
$manager->shutdown();
if ($input->getArgument('action') == 'stop') {
throw new Exception('shutdown success');
}
$this->generate_runtime_builder($manager);
} catch (\Throwable $throwable) {
2021-09-26 00:29:00 +08:00
$output->write(jTraceEx($throwable));
2021-09-24 18:53:14 +08:00
} finally {
return 1;
}
}
/**
* @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,
'exit_condition' => function () {
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
}
/**
* @param $manager
* @throws ConfigException
*/
private function generate_runtime_builder($manager): void
{
$this->configure_set();
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
$this->eventProvider->on(OnTaskerStart::class, [di(TaskerDispatch::class), 'dispatch']);
$manager->start();
}
2021-04-30 11:15:57 +08:00
2020-09-03 00:15:57 +08:00
}