111
This commit is contained in:
@@ -15,6 +15,7 @@ use Server\Events\OnWorkerStart;
|
||||
use Server\Worker\OnServerWorker;
|
||||
use Server\Worker\OnWorkerStart as WorkerDispatch;
|
||||
use Server\Worker\OnTaskerStart as TaskerDispatch;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Runtime;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
@@ -29,74 +30,88 @@ class ServerCommand extends Command
|
||||
{
|
||||
|
||||
|
||||
const ACTIONS = ['start', 'stop', 'restart'];
|
||||
const ACTIONS = ['start', 'stop', 'restart'];
|
||||
|
||||
|
||||
/**
|
||||
* @var EventProvider
|
||||
*/
|
||||
#[Inject(EventProvider::class)]
|
||||
public EventProvider $eventProvider;
|
||||
/**
|
||||
* @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)
|
||||
->addArgument('daemon', InputArgument::OPTIONAL, '', 0);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('sw:server')
|
||||
->setDescription('server start|stop|reload|restart')
|
||||
->addArgument('action', InputArgument::REQUIRED)
|
||||
->addArgument('daemon', InputArgument::OPTIONAL, '', 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
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');
|
||||
}
|
||||
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
|
||||
if ($enable_coroutine === true) {
|
||||
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
|
||||
}
|
||||
$this->generate_runtime_builder($manager);
|
||||
} catch (\Throwable $throwable) {
|
||||
$output->write($throwable->getMessage());
|
||||
} finally {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
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');
|
||||
}
|
||||
$this->generate_runtime_builder($manager);
|
||||
} catch (\Throwable $throwable) {
|
||||
$output->write($throwable->getMessage());
|
||||
} finally {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $manager
|
||||
*/
|
||||
private function generate_runtime_builder($manager): void
|
||||
{
|
||||
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
|
||||
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;
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
/**
|
||||
* @param $manager
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user