From f202c826b74972775fe97ad91b2c38e5c7d97014 Mon Sep 17 00:00:00 2001 From: inhere Date: Tue, 15 Oct 2019 00:15:48 +0800 Subject: [PATCH] up: simplify server management command logic --- .../src/Command/HttpServerCommand.php | 73 +--------- .../src/Command/ServiceServerCommand.php | 96 ++----------- src/server/src/Command/BaseServerCommand.php | 136 ++++++++++++++++-- .../src/Command/TcpServerCommand.php | 71 +-------- .../src/Command/WsServerCommand.php | 91 +++--------- 5 files changed, 160 insertions(+), 307 deletions(-) diff --git a/src/http-server/src/Command/HttpServerCommand.php b/src/http-server/src/Command/HttpServerCommand.php index ddea21b18..3954d31c1 100644 --- a/src/http-server/src/Command/HttpServerCommand.php +++ b/src/http-server/src/Command/HttpServerCommand.php @@ -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; @@ -42,42 +41,7 @@ public function start(): void { $server = $this->createServer(); - // Check if it has started - if ($server->isRunning()) { - $masterPid = $server->getPid(); - output()->writeln("The HTTP server have been running!(PID: {$masterPid})"); - 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('HTTP server start success !'); + $this->showServerInfoPanel($server); // Start the server $server->start(); @@ -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('The HTTP server is not running! cannot reload'); - return; - } - - output()->writef('Server %s is reloading', $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('HTTP server %s reload success', $script); + // Reload server + $this->reloadServer($server); } /** @@ -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('Server HTTP restart success !'); - $server->startWithDaemonize(); + // Restart server + $this->restartServer($server); } /** diff --git a/src/rpc-server/src/Command/ServiceServerCommand.php b/src/rpc-server/src/Command/ServiceServerCommand.php index 6de8354b1..cbf7789ff 100644 --- a/src/rpc-server/src/Command/ServiceServerCommand.php +++ b/src/rpc-server/src/Command/ServiceServerCommand.php @@ -1,23 +1,17 @@ createServer(); - // Check if it has started - if ($server->isRunning()) { - $masterPid = $server->getPid(); - output()->writeln("The RPC server have been running!(PID: {$masterPid})"); - 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('RPC server start success !'); + $this->showServerInfoPanel($server); // Start the server $server->start(); @@ -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('The RPC server is not running! cannot reload'); - return; - } - - output()->writef('RPC server %s is reloading', $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('RPC server %s reload success', $script); + // Reload server + $this->reloadServer($server); } /** @@ -165,18 +97,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('RPC server reload success !'); - $server->startWithDaemonize(); + // Restart server + $this->restartServer($server); } /** @@ -184,7 +106,7 @@ public function restart(): void */ private function createServer(): ServiceServer { - $script = input()->getScriptFile(); + $script = input()->getScriptFile(); $command = $this->getFullCommand(); /** @var ServiceServer $server */ diff --git a/src/server/src/Command/BaseServerCommand.php b/src/server/src/Command/BaseServerCommand.php index dd9ff0b27..2c564002a 100644 --- a/src/server/src/Command/BaseServerCommand.php +++ b/src/server/src/Command/BaseServerCommand.php @@ -2,10 +2,12 @@ namespace Swoft\Server\Command; +use Swoft\Console\Helper\Show; use Swoft\Server\Contract\ServerInterface; use Swoft\Server\Server; use Swoft\Stdlib\Helper\Sys; use function input; +use function output; use function sprintf; use function strtoupper; use function trim; @@ -16,6 +18,40 @@ */ abstract class BaseServerCommand { + /** + * Show server information panel in terminal + * + * @param Server $server + */ + protected function showServerInfoPanel(Server $server): void + { + // Check if it has started + if ($server->isRunning()) { + $masterPid = $server->getPid(); + output()->writeln("The server have been running!(PID: {$masterPid})"); + return; + } + + // Startup config + $this->configStartOption($server); + + // Server startup parameters + $sType = $server->getServerType(); + + // Main server info + $panel = [ + $sType => $this->buildMainServerInfo($server), + ]; + + // Port listeners + $panel = $this->appendPortsToPanel($server, $panel); + + // Show server info + Show::panel($panel, 'Server Information'); + + output()->writef("$sType server start success :)"); + } + /** * Set startup options to override configuration options * @@ -30,23 +66,25 @@ protected function configStartOption(Server $server): void } /** - * @return string + * @param Server $server + * + * @return array */ - protected function getFullCommand(): string + protected function buildMainServerInfo(Server $server): array { - // Script file - $script = input()->getScriptFile(); + // Server setting + $settings = $server->getSetting(); + $workerNum = $settings['worker_num']; - // Full command - $command = input()->getFullScript(); - - $phpBin = 'php'; - [$ok, $ret,] = Sys::run('which php'); - if ($ok === 0) { - $phpBin = trim($ret); - } + $mainHost = $server->getHost(); + $mainPort = $server->getPort(); - return sprintf('%s %s %s', $phpBin, $script, $command); + return [ + 'listen' => $mainHost . ':' . $mainPort, + 'type' => $server->getTypeName(), + 'mode' => $server->getModeName(), + 'worker' => $workerNum, + ]; } /** @@ -67,11 +105,81 @@ protected function appendPortsToPanel(Server $server, array $panel): array $upperName = strtoupper($name); $panel[$upperName] = [ - 'listen' => sprintf('%s:%s', $listener->getHost(), $listener->getPort()), + 'listen' => $listener->getHost() . ':' . $listener->getPort(), 'type' => $listener->getTypeName() ]; } return $panel; } + + /** + * Reload Server - reload worker processes + * + * @param Server $server + */ + protected function reloadServer(Server $server): void + { + $script = input()->getScriptFile(); + + // Check if it has started + if (!$server->isRunning()) { + output()->writeln('The server is not running! cannot reload'); + return; + } + + output()->writef('Server %s is reloading', $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('Server %s reload success', $script); + } + + /** + * @param Server $server + */ + protected function restartServer(Server $server): void + { + // If it's has started, stop old server. + if ($server->isRunning()) { + $success = $server->stop(); + + if (!$success) { + output()->error('Stop the old server failed!'); + return; + } + } + + output()->writef('Swoft server restart success!'); + + // Restart server + $server->startWithDaemonize(); + } + + /** + * @return string + */ + protected function getFullCommand(): string + { + // Script file + $script = input()->getScriptFile(); + + // Full command + $command = input()->getFullScript(); + + $phpBin = 'php'; + [$ok, $ret,] = Sys::run('which php'); + if ($ok === 0) { + $phpBin = trim($ret); + } + + return sprintf('%s %s %s', $phpBin, $script, $command); + } } diff --git a/src/tcp-server/src/Command/TcpServerCommand.php b/src/tcp-server/src/Command/TcpServerCommand.php index d4d2713c0..f99879bad 100644 --- a/src/tcp-server/src/Command/TcpServerCommand.php +++ b/src/tcp-server/src/Command/TcpServerCommand.php @@ -5,7 +5,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\Server\Command\BaseServerCommand; use Swoft\Server\Exception\ServerException; use Swoft\Tcp\Server\TcpServer; @@ -42,41 +41,7 @@ public function start(): void { $server = $this->createServer(); - // Check if it has started - if ($server->isRunning()) { - $masterPid = $server->getPid(); - output()->writeln("The server have been running!(PID: {$masterPid})"); - return; - } - - // Startup settings - $this->configStartOption($server); - - $settings = $server->getSetting(); - // Setting - $workerNum = $settings['worker_num']; - - // Server startup parameters - $mainHost = $server->getHost(); - $mainPort = $server->getPort(); - - $server->getServerType(); - // Main server - $panel = [ - 'TCP' => [ - 'listen' => $mainHost . ':' . $mainPort, - 'type' => $server->getTypeName(), - 'mode' => $server->getModeName(), - 'worker' => $workerNum, - ], - ]; - - // Port Listeners - $panel = $this->appendPortsToPanel($server, $panel); - - Show::panel($panel); - - output()->writef('Tcp server start success !'); + $this->showServerInfoPanel($server); // Start the server $server->start(); @@ -87,31 +52,13 @@ public function start(): void * * @CommandMapping(usage="{fullCommand} [-t]") * @CommandOption("t", desc="Only to reload task processes, default to reload worker and task") - * */ public function reload(): void { $server = $this->createServer(); - $script = input()->getScriptFile(); - - // Check if it has started - if (!$server->isRunning()) { - output()->writeln('The server is not running! cannot reload'); - return; - } - - output()->writef('Server %s is reloading', $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('Tcp Server %s reload success', $script); + // Reload server + $this->reloadServer($server); } /** @@ -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; - } - } - // Restart server - $server->startWithDaemonize(); + $this->restartServer($server); } /** diff --git a/src/websocket-server/src/Command/WsServerCommand.php b/src/websocket-server/src/Command/WsServerCommand.php index 64e993f3a..415adc2a2 100644 --- a/src/websocket-server/src/Command/WsServerCommand.php +++ b/src/websocket-server/src/Command/WsServerCommand.php @@ -5,9 +5,9 @@ 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\Server\Command\BaseServerCommand; use Swoft\Server\Exception\ServerException; +use Swoft\Server\Server; use Swoft\Server\SwooleEvent; use Swoft\WebSocket\Server\WebSocketServer; use Throwable; @@ -43,50 +43,26 @@ public function start(): void { $server = $this->createServer(); - // Check if it has started - if ($server->isRunning()) { - $masterPid = $server->getPid(); - output()->writeln("The server have been running!(PID: {$masterPid})"); - return; - } - - // Startup settings - $this->configStartOption($server); - - $settings = $server->getSetting(); - // Setting - $workerNum = $settings['worker_num']; - - // Server startup parameters - $mainHost = $server->getHost(); - $mainPort = $server->getPort(); - - $allowHttp = $server->hasListener(SwooleEvent::REQUEST); - $httpText = $allowHttp ? 'enabled' : 'disabled'; - - // Main server - $panel = [ - 'WebSocket' => [ - 'listen' => $mainHost . ':' . $mainPort, - 'type' => $server->getTypeName(), - 'mode' => $server->getModeName(), - 'worker' => $workerNum . " (HTTP:$httpText)", - ], - // 'ExtraInfo' => [ - // 'HttpHandle' => $server->hasListener(SwooleEvent::REQUEST), - // 'pidFile' => $server->getPidFile(), - // ], - ]; + $this->showServerInfoPanel($server); - // Port Listeners - $panel = $this->appendPortsToPanel($server, $panel); + // Start the server + $server->start(); + } - Show::panel($panel); + /** + * @param Server $server + * + * @return array + */ + protected function buildMainServerInfo(Server $server): array + { + $info = parent::buildMainServerInfo($server); - output()->writef('WebSocket Server start success !'); + $openHttp = $server->hasListener(SwooleEvent::REQUEST); + $httpText = $openHttp ? 'enabled' : 'disabled'; - // Start the server - $server->start(); + $info['worker'] .= " (HTTP:$httpText)"; + return $info; } /** @@ -98,26 +74,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('The server is not running! cannot reload'); - return; - } - - output()->writef('Server %s is reloading', $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('Server %s reload success', $script); + // Reload server + $this->reloadServer($server); } /** @@ -153,18 +112,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; - } - } - // Restart server - $server->startWithDaemonize(); + $this->restartServer($server); } /**