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

100 lines
2.4 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-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-08-29 04:57:50 +08:00
use Server\Events\OnBeforeWorkerStart;
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-08-29 01:18:42 +08:00
use Server\Worker\OnWorkerStart as WorkerDispatch;
2021-09-07 15:24:42 +08:00
use Swoole\Runtime;
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;
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
const ACTIONS = ['start', 'stop', 'restart'];
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
*
*/
protected function configure()
{
2021-09-02 11:26:11 +08:00
$this->setName('sw:server')
2021-09-02 12:02:26 +08:00
->setDescription('server start|stop|reload|restart')
2021-09-02 13:48:57 +08:00
->addArgument('action', InputArgument::REQUIRED)
->addArgument('daemon', InputArgument::OPTIONAL, '', 0);
2021-09-02 11:13:11 +08:00
}
/**
2021-09-02 11:26:11 +08:00
* @param InputInterface $input
* @param OutputInterface $output
2021-09-02 13:59:00 +08:00
* @return int
2021-09-02 11:26:11 +08:00
* @throws Exception
2021-09-02 11:13:11 +08:00
*/
2021-09-02 13:48:57 +08:00
public function execute(InputInterface $input, OutputInterface $output): int
2021-09-02 11:13:11 +08:00
{
2021-09-02 13:59:00 +08:00
try {
$manager = Kiri::app()->getServer();
$manager->setDaemon($input->getArgument('daemon'));
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');
}
2021-09-07 15:24:42 +08:00
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
if ($enable_coroutine === true) {
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
}
2021-09-02 13:59:00 +08:00
$this->generate_runtime_builder($manager);
} catch (\Throwable $throwable) {
$output->write($throwable->getMessage());
} finally {
2021-09-02 13:48:57 +08:00
return 1;
2021-09-02 11:13:11 +08:00
}
}
/**
* @param $manager
*/
2021-09-02 13:59:00 +08:00
private function generate_runtime_builder($manager): void
2021-09-02 11:13:11 +08:00
{
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']);
2021-09-02 13:48:57 +08:00
$manager->start();
2021-09-02 11:13:11 +08:00
}
2021-04-30 11:15:57 +08:00
2020-09-03 00:15:57 +08:00
}