This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apiserver): Create API Server component (#32)
API Server component will allow to manage Swoole HTTP Server trough HTTP interface. Command `swoole:server:status` is added to get informations about currently running HTTP server. Closes #2
- Loading branch information
Showing
31 changed files
with
1,297 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/Bridge/Symfony/Bundle/Command/ServerStatusCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\Command; | ||
|
||
use Assert\Assertion; | ||
use K911\Swoole\Server\Api\ApiServerInterface; | ||
use K911\Swoole\Server\Config\Socket; | ||
use K911\Swoole\Server\Config\Sockets; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
final class ServerStatusCommand extends Command | ||
{ | ||
private $apiServer; | ||
private $sockets; | ||
private $parameterBag; | ||
private $testing = false; | ||
|
||
public function __construct( | ||
Sockets $sockets, | ||
ApiServerInterface $apiServer, | ||
ParameterBagInterface $parameterBag | ||
) { | ||
$this->apiServer = $apiServer; | ||
$this->sockets = $sockets; | ||
$this->parameterBag = $parameterBag; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->setDescription('Get current status of the Swoole HTTP Server by querying running API Server.') | ||
->addOption('api-host', null, InputOption::VALUE_REQUIRED, 'API Server listens on this host.', $this->parameterBag->get('swoole.http_server.api.host')) | ||
->addOption('api-port', null, InputOption::VALUE_REQUIRED, 'API Server listens on this port.', $this->parameterBag->get('swoole.http_server.api.port')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \Assert\AssertionFailedException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$this->prepareClientConfiguration($input); | ||
|
||
$this->goAndWait(function () use ($io): void { | ||
try { | ||
$status = $this->apiServer->status(); | ||
$metrics = $this->apiServer->metrics(); | ||
} catch (\RuntimeException $runtimeException) { | ||
$io->error('Could not connect to Swoole API Server'); | ||
|
||
return; | ||
} | ||
$io->success('Fetched status and metrics'); | ||
$this->showStatus($io, $status); | ||
$this->showMetrics($io, $metrics); | ||
}); | ||
|
||
return 0; | ||
} | ||
|
||
public function goAndWait(callable $callback): void | ||
{ | ||
if ($this->testing) { | ||
$callback(); | ||
|
||
return; | ||
} | ||
|
||
\go($callback); | ||
\swoole_event_wait(); | ||
} | ||
|
||
private function showStatus(SymfonyStyle $io, array $status): void | ||
{ | ||
$server = $status['server']; | ||
$processes = $server['processes']; | ||
|
||
$rows = [ | ||
['Host', $server['host']], | ||
['Port', $server['port']], | ||
['Running mode', $server['runningMode']], | ||
['Master PID', $processes['master']['pid']], | ||
['Manager PID', $processes['manager']['pid']], | ||
[\sprintf('Worker[%d] PID', $processes['worker']['id']), $processes['worker']['pid']], | ||
]; | ||
|
||
foreach ($server['listeners'] as $id => ['host' => $host, 'port' => $port]) { | ||
$rows[] = [\sprintf('Listener[%d] Host', $id), $host]; | ||
$rows[] = [\sprintf('Listener[%d] Port', $id), $port]; | ||
} | ||
|
||
$io->table([ | ||
'Configuration', 'Value', | ||
], $rows); | ||
} | ||
|
||
private function showMetrics(SymfonyStyle $io, array $metrics): void | ||
{ | ||
$date = \DateTimeImmutable::createFromFormat(DATE_ATOM, $metrics['date']); | ||
Assertion::isInstanceOf($date, \DateTimeImmutable::class); | ||
$server = $metrics['server']; | ||
$runningSeconds = $date->getTimestamp() - $server['start_time']; | ||
|
||
$idleWorkers = $server['idle_worker_num']; | ||
$workers = $server['worker_num']; | ||
$activeWorkers = $workers - $idleWorkers; | ||
|
||
$io->table([ | ||
'Metric', 'Quantity', 'Unit', | ||
], [ | ||
['Requests', $server['request_count'], '1'], | ||
['Up time', $runningSeconds, 'Seconds'], | ||
['Active connections', $server['connection_num'], '1'], | ||
['Accepted connections', $server['accept_count'], '1'], | ||
['Closed connections', $server['close_count'], '1'], | ||
['Active workers', $activeWorkers, '1'], | ||
['Idle workers', $idleWorkers, '1'], | ||
]); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* | ||
* @throws \Assert\AssertionFailedException | ||
*/ | ||
protected function prepareClientConfiguration(InputInterface $input): void | ||
{ | ||
$host = $input->getOption('api-host'); | ||
$port = $input->getOption('api-port'); | ||
|
||
Assertion::numeric($port, 'Port must be a number.'); | ||
Assertion::string($host, 'Host must be a string.'); | ||
|
||
$this->sockets->changeApiSocket(new Socket($host, (int) $port)); | ||
} | ||
|
||
public function enableTestMode(): void | ||
{ | ||
$this->testing = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.