Skip to content

Commit

Permalink
up: simplify server management command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 14, 2019
1 parent d3f7bc3 commit f202c82
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 307 deletions.
73 changes: 5 additions & 68 deletions src/http-server/src/Command/HttpServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Console\Annotation\Mapping\CommandOption;
use Swoft\Console\Helper\Show;
use Swoft\Http\Server\HttpServer;
use Swoft\Server\Command\BaseServerCommand;
use Swoft\Server\Exception\ServerException;
Expand Down Expand Up @@ -42,42 +41,7 @@ public function start(): void
{
$server = $this->createServer();

// Check if it has started
if ($server->isRunning()) {
$masterPid = $server->getPid();
output()->writeln("<error>The HTTP server have been running!(PID: {$masterPid})</error>");
return;
}

// Startup settings
$this->configStartOption($server);

$settings = $server->getSetting();
// Setting
$workerNum = $settings['worker_num'];

// Server startup parameters
$mainHost = $server->getHost();
$mainPort = $server->getPort();
$modeName = $server->getModeName();
$typeName = $server->getTypeName();

// Http
$panel = [
'HTTP' => [
'listen' => $mainHost . ':' . $mainPort,
'type' => $typeName,
'mode' => $modeName,
'worker' => $workerNum,
],
];

// Port Listeners
$panel = $this->appendPortsToPanel($server, $panel);

Show::panel($panel);

output()->writeln('<success>HTTP server start success !</success>');
$this->showServerInfoPanel($server);

// Start the server
$server->start();
Expand All @@ -92,26 +56,9 @@ public function start(): void
public function reload(): void
{
$server = $this->createServer();
$script = input()->getScriptFile();

// Check if it has started
if (!$server->isRunning()) {
output()->writeln('<error>The HTTP server is not running! cannot reload</error>');
return;
}

output()->writef('<info>Server %s is reloading</info>', $script);

if ($reloadTask = input()->hasOpt('t')) {
Show::notice('Will only reload task worker');
}

if (!$server->reload($reloadTask)) {
Show::error('The swoole server worker process reload fail!');
return;
}

output()->writef('<success>HTTP server %s reload success</success>', $script);
// Reload server
$this->reloadServer($server);
}

/**
Expand Down Expand Up @@ -147,18 +94,8 @@ public function restart(): void
{
$server = $this->createServer();

// Check if it has started
if ($server->isRunning()) {
$success = $server->stop();

if (!$success) {
output()->error('Stop the old server failed!');
return;
}
}

output()->writef('<success>Server HTTP restart success !</success>');
$server->startWithDaemonize();
// Restart server
$this->restartServer($server);
}

/**
Expand Down
96 changes: 9 additions & 87 deletions src/rpc-server/src/Command/ServiceServerCommand.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?php declare(strict_types=1);


namespace Swoft\Rpc\Server\Command;


use function bean;
use function input;
use function output;
use ReflectionException;
use Swoft;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Console\Annotation\Mapping\CommandOption;
use Swoft\Console\Helper\Show;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\Server\Command\BaseServerCommand;
use Swoft\Server\Exception\ServerException;
use Swoft\Server\ServerInterface;
use function bean;
use function input;
use function output;

/**
* Class ServiceServerCommand
Expand Down Expand Up @@ -48,52 +42,7 @@ public function start(): void
{
$server = $this->createServer();

// Check if it has started
if ($server->isRunning()) {
$masterPid = $server->getPid();
output()->writeln("<error>The RPC server have been running!(PID: {$masterPid})</error>");
return;
}

// Startup settings
$this->configStartOption($server);

$settings = $server->getSetting();

// Setting
$workerNum = $settings['worker_num'];

// Server startup parameters
$mainHost = $server->getHost();
$mainPort = $server->getPort();
$modeName = $server->getModeName();
$typeName = $server->getTypeName();

// RPC
$panel = [
'RPC' => [
'listen' => $mainHost . ':' . $mainPort,
'type' => $typeName,
'mode' => $modeName,
'worker' => $workerNum,
],
];

// Listener
$listeners = $server->getListener();
foreach ($listeners as $name => $listener) {
if (!$listener instanceof ServerInterface) {
continue;
}
$panel[$name] = [
'listen' => sprintf('%s:%s', $listener->getHost(), $listener->getPort()),
'type' => $listener->getTypeName()
];
}

Show::panel($panel);

output()->writef('<success>RPC server start success !</success>');
$this->showServerInfoPanel($server);

// Start the server
$server->start();
Expand All @@ -109,26 +58,9 @@ public function start(): void
public function reload(): void
{
$server = $this->createServer();
$script = input()->getScriptFile();

// Check if it has started
if (!$server->isRunning()) {
output()->writeln('<error>The RPC server is not running! cannot reload</error>');
return;
}

output()->writef('<info>RPC server %s is reloading</info>', $script);

if ($reloadTask = input()->hasOpt('t')) {
Show::notice('Will only reload task worker');
}

if (!$server->reload($reloadTask)) {
Show::error('The swoole server worker process reload fail!');
return;
}

output()->writef('<success>RPC server %s reload success</success>', $script);
// Reload server
$this->reloadServer($server);
}

/**
Expand Down Expand Up @@ -165,26 +97,16 @@ public function restart(): void
{
$server = $this->createServer();

// Check if it has started
if ($server->isRunning()) {
$success = $server->stop();

if (!$success) {
output()->error('Stop the old server failed!');
return;
}
}

output()->writef('<success>RPC server reload success !</success>');
$server->startWithDaemonize();
// Restart server
$this->restartServer($server);
}

/**
* @return ServiceServer
*/
private function createServer(): ServiceServer
{
$script = input()->getScriptFile();
$script = input()->getScriptFile();
$command = $this->getFullCommand();

/** @var ServiceServer $server */
Expand Down
Loading

0 comments on commit f202c82

Please sign in to comment.