mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-18 05:04:51 +08:00
remove and refactor daemon/server command, add useful messages
This commit is contained in:
parent
1b520d3c96
commit
c74a43cd74
@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Command\Daemon;
|
||||
|
||||
use Swoole\Process;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class DaemonReloadCommand extends DaemonCommand
|
||||
{
|
||||
protected static $defaultName = 'daemon:reload';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setDescription('重载框架');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
parent::execute($input, $output);
|
||||
Process::kill(intval($this->daemon_file['pid']), SIGUSR1);
|
||||
$output->writeln('<info>成功重载!</info>');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Command\Daemon;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class DaemonStatusCommand extends DaemonCommand
|
||||
{
|
||||
protected static $defaultName = 'daemon:status';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setDescription('查看框架的运行状态');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
parent::execute($input, $output);
|
||||
$output->writeln('<info>框架' . ($this->daemon_file['daemon'] ? '以守护进程模式' : '') . '运行中,pid:' . $this->daemon_file['pid'] . '</info>');
|
||||
if ($this->daemon_file['daemon']) {
|
||||
$output->writeln('<comment>----- 以下是stdout内容 -----</comment>');
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Command\Daemon;
|
||||
|
||||
use Swoole\Process;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ZM\Framework;
|
||||
|
||||
class DaemonStopCommand extends DaemonCommand
|
||||
{
|
||||
protected static $defaultName = 'daemon:stop';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->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('<error>停止失败,请检查进程pid #' . $this->daemon_file['pid'] . ' 是否响应!</error>');
|
||||
} else {
|
||||
$output->writeln('<info>成功停止!</info>');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -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('<comment>未检测到正在运行的守护进程或框架进程!</comment>');
|
||||
Framework::removeProcessState(ZM_PROCESS_MASTER);
|
||||
if (ProcessManager::isStateEmpty()) {
|
||||
ProcessManager::removeProcessState(ZM_PROCESS_MASTER);
|
||||
} else {
|
||||
$output->writeln('<comment>检测到可能残留的守护进程或框架进程,请使用命令关闭:server:stop --force</comment>');
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
$this->daemon_file = $file;
|
||||
@ -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';
|
||||
|
||||
|
||||
@ -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("<error>检测到已经在 pid: {$state['pid']} 进程启动了框架!</error>");
|
||||
@ -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';
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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(),
|
||||
]);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user