This commit is contained in:
2021-09-02 13:59:00 +08:00
parent 648e698d9e
commit c5b370dabc
6 changed files with 31 additions and 35 deletions
+139
View File
@@ -0,0 +1,139 @@
<?php
namespace Server;
use Annotation\Inject;
use Exception;
use Http\Abstracts\HttpService;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config;
use Kiri\Error\LoggerProcess;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Rpc\Service;
use Server\Events\OnShutdown;
use Swoole\Runtime;
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
/**
* Class Server
* @package Http
*/
class Server extends HttpService
{
private array $process = [
LoggerProcess::class
];
/**
* @Inject ServerManager
* @var null|ServerManager
*/
#[Inject(ServerManager::class)]
public ?ServerManager $manager = null;
private mixed $daemon = 0;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
*
*/
public function init()
{
}
/**
* @param $process
*/
public function addProcess($process)
{
$this->process[] = $process;
}
/**
* @return string start server
*
* start server
* @throws ConfigException
* @throws Exception
*/
public function start(): string
{
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
Service::addRpcListener($this->manager, $rpcService);
}
$processes = array_merge($this->process, Config::get('processes', []));
foreach ($processes as $process) {
$this->manager->addProcess($process);
}
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
return $this->manager->getServer()->start();
}
/**
* @return void
*
* start server
* @throws Exception
*/
public function shutdown()
{
$configs = Config::get('server', [], true);
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
$this->manager->stopServer($config['port']);
}
$this->eventDispatch->dispatch(new OnShutdown());
}
/**
* @return bool
* @throws ConfigException
*/
public function isRunner(): bool
{
return $this->manager->isRunner();
}
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{
return $this->manager->getServer();
}
}
+98
View File
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Server;
use Annotation\Inject;
use Exception;
use Kiri\Events\EventProvider;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnWorkerStart;
use Server\Worker\OnServerWorker;
use Server\Worker\OnWorkerStart as WorkerDispatch;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Command
* @package Http
*/
class ServerCommand extends Command
{
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)
->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');
}
$this->generate_runtime_builder($manager);
} catch (\Throwable $throwable) {
$output->write($throwable->getMessage());
} finally {
return 1;
}
}
/**
* @param $manager
* @return void
* @throws NotFindClassException
* @throws ReflectionException
*/
private function generate_runtime_builder($manager): void
{
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']);
$manager->start();
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Server;
use Exception;
use Kiri\Abstracts\Providers;
use Kiri\Application;
use Kiri\Kiri;
/**
* Class DatabasesProviders
* @package Database
*/
class ServerProviders extends Providers
{
/**
* @param Application $application
* @throws Exception
*/
public function onImport(Application $application)
{
$application->set('server', ['class' => Server::class]);
$container = Kiri::getDi();
$console = $container->get(\Symfony\Component\Console\Application::class);
$console->add($container->get(ServerCommand::class));
}
}