From c74a43cd74cc5a9dd3f38f10d87fb936ed0d4d85 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 14 May 2022 23:40:22 +0800 Subject: [PATCH] remove and refactor daemon/server command, add useful messages --- src/ZM/Command/Daemon/DaemonReloadCommand.php | 27 -------------- src/ZM/Command/Daemon/DaemonStatusCommand.php | 35 ------------------ src/ZM/Command/Daemon/DaemonStopCommand.php | 37 ------------------- .../ServerCommand.php} | 18 ++++++--- src/ZM/Command/Server/ServerReloadCommand.php | 3 +- .../ServerStartCommand.php} | 17 ++++++--- src/ZM/Command/Server/ServerStatusCommand.php | 3 +- src/ZM/Command/Server/ServerStopCommand.php | 7 ++-- src/ZM/ConsoleApplication.php | 12 ++---- src/ZM/ZMServer.php | 8 ++-- 10 files changed, 37 insertions(+), 130 deletions(-) delete mode 100644 src/ZM/Command/Daemon/DaemonReloadCommand.php delete mode 100644 src/ZM/Command/Daemon/DaemonStatusCommand.php delete mode 100644 src/ZM/Command/Daemon/DaemonStopCommand.php rename src/ZM/Command/{Daemon/DaemonCommand.php => Server/ServerCommand.php} (50%) rename src/ZM/Command/{RunServerCommand.php => Server/ServerStartCommand.php} (90%) diff --git a/src/ZM/Command/Daemon/DaemonReloadCommand.php b/src/ZM/Command/Daemon/DaemonReloadCommand.php deleted file mode 100644 index 0b0fe80f..00000000 --- a/src/ZM/Command/Daemon/DaemonReloadCommand.php +++ /dev/null @@ -1,27 +0,0 @@ -setDescription('重载框架'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - parent::execute($input, $output); - Process::kill(intval($this->daemon_file['pid']), SIGUSR1); - $output->writeln('成功重载!'); - return 0; - } -} diff --git a/src/ZM/Command/Daemon/DaemonStatusCommand.php b/src/ZM/Command/Daemon/DaemonStatusCommand.php deleted file mode 100644 index 19ab4b8d..00000000 --- a/src/ZM/Command/Daemon/DaemonStatusCommand.php +++ /dev/null @@ -1,35 +0,0 @@ -setDescription('查看框架的运行状态'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - parent::execute($input, $output); - $output->writeln('框架' . ($this->daemon_file['daemon'] ? '以守护进程模式' : '') . '运行中,pid:' . $this->daemon_file['pid'] . ''); - if ($this->daemon_file['daemon']) { - $output->writeln('----- 以下是stdout内容 -----'); - $stdout = file_get_contents($this->daemon_file['stdout']); - $stdout = explode("\n", $stdout); - for ($i = 15; $i > 0; --$i) { - if (isset($stdout[count($stdout) - $i])) { - echo $stdout[count($stdout) - $i] . PHP_EOL; - } - } - } - return 0; - } -} diff --git a/src/ZM/Command/Daemon/DaemonStopCommand.php b/src/ZM/Command/Daemon/DaemonStopCommand.php deleted file mode 100644 index f36e4e04..00000000 --- a/src/ZM/Command/Daemon/DaemonStopCommand.php +++ /dev/null @@ -1,37 +0,0 @@ -setDescription('停止运行的框架'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - parent::execute($input, $output); - Process::kill(intval($this->daemon_file['pid']), SIGTERM); - $i = 10; - while (Framework::getProcessState(ZM_PROCESS_MASTER) !== false && $i > 0) { - sleep(1); - --$i; - } - if ($i === 0) { - $output->writeln('停止失败,请检查进程pid #' . $this->daemon_file['pid'] . ' 是否响应!'); - } else { - $output->writeln('成功停止!'); - } - return 0; - } -} diff --git a/src/ZM/Command/Daemon/DaemonCommand.php b/src/ZM/Command/Server/ServerCommand.php similarity index 50% rename from src/ZM/Command/Daemon/DaemonCommand.php rename to src/ZM/Command/Server/ServerCommand.php index 27d17dce..173c7efc 100644 --- a/src/ZM/Command/Daemon/DaemonCommand.php +++ b/src/ZM/Command/Server/ServerCommand.php @@ -2,23 +2,31 @@ declare(strict_types=1); -namespace ZM\Command\Daemon; +namespace ZM\Command\Server; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Framework; +use ZM\Exception\ZMKnownException; +use ZM\Utils\Manager\ProcessManager; -abstract class DaemonCommand extends Command +abstract class ServerCommand extends Command { protected $daemon_file; + /** + * @throws ZMKnownException + */ protected function execute(InputInterface $input, OutputInterface $output): int { - $file = Framework::getProcessState(ZM_PROCESS_MASTER); + $file = ProcessManager::getProcessState(ZM_PROCESS_MASTER); if ($file === false || posix_getsid(intval($file['pid'])) === false) { $output->writeln('未检测到正在运行的守护进程或框架进程!'); - Framework::removeProcessState(ZM_PROCESS_MASTER); + if (ProcessManager::isStateEmpty()) { + ProcessManager::removeProcessState(ZM_PROCESS_MASTER); + } else { + $output->writeln('检测到可能残留的守护进程或框架进程,请使用命令关闭:server:stop --force'); + } exit(1); } $this->daemon_file = $file; diff --git a/src/ZM/Command/Server/ServerReloadCommand.php b/src/ZM/Command/Server/ServerReloadCommand.php index 8d87503c..7829cd13 100644 --- a/src/ZM/Command/Server/ServerReloadCommand.php +++ b/src/ZM/Command/Server/ServerReloadCommand.php @@ -7,9 +7,8 @@ namespace ZM\Command\Server; use Swoole\Process; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Command\Daemon\DaemonCommand; -class ServerReloadCommand extends DaemonCommand +class ServerReloadCommand extends ServerCommand { protected static $defaultName = 'server:reload'; diff --git a/src/ZM/Command/RunServerCommand.php b/src/ZM/Command/Server/ServerStartCommand.php similarity index 90% rename from src/ZM/Command/RunServerCommand.php rename to src/ZM/Command/Server/ServerStartCommand.php index 6633bb6a..8fbf706d 100644 --- a/src/ZM/Command/RunServerCommand.php +++ b/src/ZM/Command/Server/ServerStartCommand.php @@ -2,19 +2,22 @@ declare(strict_types=1); -namespace ZM\Command; +namespace ZM\Command\Server; -use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use ZM\Exception\ConfigException; +use ZM\Exception\ZMKnownException; use ZM\Framework; +use ZM\Utils\Manager\ProcessManager; -class RunServerCommand extends Command +class ServerStartCommand extends ServerCommand { protected static $defaultName = 'server'; - public static function exportDefinition() + public static function exportDefinition(): InputDefinition { $cmd = new self(); $cmd->configure(); @@ -54,6 +57,10 @@ class RunServerCommand extends Command $this->setHelp('直接运行可以启动'); } + /** + * @throws ZMKnownException + * @throws ConfigException + */ protected function execute(InputInterface $input, OutputInterface $output): int { if (($opt = $input->getOption('env')) !== null) { @@ -62,7 +69,7 @@ class RunServerCommand extends Command return 1; } } - $state = Framework::getProcessState(ZM_PROCESS_MASTER); + $state = ProcessManager::getProcessState(ZM_PROCESS_MASTER); if (!$input->getOption('no-state-check')) { if (is_array($state) && posix_getsid($state['pid'] ?? -1) !== false) { $output->writeln("检测到已经在 pid: {$state['pid']} 进程启动了框架!"); diff --git a/src/ZM/Command/Server/ServerStatusCommand.php b/src/ZM/Command/Server/ServerStatusCommand.php index d3abb6ff..4b0fe449 100644 --- a/src/ZM/Command/Server/ServerStatusCommand.php +++ b/src/ZM/Command/Server/ServerStatusCommand.php @@ -6,9 +6,8 @@ namespace ZM\Command\Server; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Command\Daemon\DaemonCommand; -class ServerStatusCommand extends DaemonCommand +class ServerStatusCommand extends ServerCommand { protected static $defaultName = 'server:status'; diff --git a/src/ZM/Command/Server/ServerStopCommand.php b/src/ZM/Command/Server/ServerStopCommand.php index c78eeefc..d2ef5006 100644 --- a/src/ZM/Command/Server/ServerStopCommand.php +++ b/src/ZM/Command/Server/ServerStopCommand.php @@ -8,11 +8,10 @@ use Swoole\Process; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Command\Daemon\DaemonCommand; -use ZM\Framework; use ZM\Utils\DataProvider; +use ZM\Utils\Manager\ProcessManager; -class ServerStopCommand extends DaemonCommand +class ServerStopCommand extends ServerCommand { protected static $defaultName = 'server:stop'; @@ -47,7 +46,7 @@ class ServerStopCommand extends DaemonCommand Process::kill(intval($this->daemon_file['pid']), SIGTERM); } $i = 10; - while (Framework::getProcessState(ZM_PROCESS_MASTER) !== false && $i > 0) { + while (ProcessManager::getProcessState(ZM_PROCESS_MASTER) !== false && $i > 0) { sleep(1); --$i; } diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 1a2d9f11..ad8ef2bf 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -11,17 +11,14 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use ZM\Command\BuildCommand; use ZM\Command\CheckConfigCommand; -use ZM\Command\Daemon\DaemonReloadCommand; -use ZM\Command\Daemon\DaemonStatusCommand; -use ZM\Command\Daemon\DaemonStopCommand; use ZM\Command\Generate\SystemdGenerateCommand; use ZM\Command\InitCommand; use ZM\Command\Module\ModuleListCommand; use ZM\Command\Module\ModulePackCommand; use ZM\Command\Module\ModuleUnpackCommand; use ZM\Command\PureHttpCommand; -use ZM\Command\RunServerCommand; use ZM\Command\Server\ServerReloadCommand; +use ZM\Command\Server\ServerStartCommand; use ZM\Command\Server\ServerStatusCommand; use ZM\Command\Server\ServerStopCommand; use ZM\Exception\InitException; @@ -84,13 +81,10 @@ class ConsoleApplication extends Application } $this->addCommands([ - new DaemonStatusCommand(), - new DaemonReloadCommand(), - new DaemonStopCommand(), - new RunServerCommand(), // 运行主服务的指令控制器 new ServerStatusCommand(), - new ServerStopCommand(), new ServerReloadCommand(), + new ServerStopCommand(), + new ServerStartCommand(), // 运行主服务的指令控制器 new PureHttpCommand(), // 纯HTTP服务器指令 new SystemdGenerateCommand(), ]); diff --git a/src/ZM/ZMServer.php b/src/ZM/ZMServer.php index cbc1203f..0df2a012 100644 --- a/src/ZM/ZMServer.php +++ b/src/ZM/ZMServer.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace ZM; -use ZM\Command\RunServerCommand; +use ZM\Command\Server\ServerStartCommand; use ZM\Console\Console; use ZM\Event\EventManager; use ZM\Exception\InitException; @@ -59,11 +59,11 @@ class ZMServer define('SOURCE_ROOT_DIR', WORKING_DIR); define('LOAD_MODE', is_dir(SOURCE_ROOT_DIR . '/src/ZM') ? 0 : 1); define('FRAMEWORK_ROOT_DIR', realpath(__DIR__ . '/../../')); - define('ZM_VERSION_ID', ConsoleApplication::VERSION_ID); - define('ZM_VERSION', ConsoleApplication::VERSION); + define('ZM_VERSION_ID', Framework::VERSION_ID); + define('ZM_VERSION', Framework::VERSION); $options = array_map(function ($x) { return $x->getDefault(); - }, RunServerCommand::exportDefinition()->getOptions()); + }, ServerStartCommand::exportDefinition()->getOptions()); (new Framework($options, true))->start(); }