2020-08-31 10:11:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
declare(strict_types=1);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
2022-05-14 23:40:22 +08:00
|
|
|
|
namespace ZM\Command\Server;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
2022-08-13 22:01:03 +08:00
|
|
|
|
use OneBot\Driver\Process\ProcessManager;
|
2022-12-17 21:53:08 +08:00
|
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2022-08-01 16:31:54 +08:00
|
|
|
|
use ZM\Exception\InitException;
|
2022-05-14 23:40:22 +08:00
|
|
|
|
use ZM\Exception\ZMKnownException;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Framework;
|
2022-08-01 16:31:54 +08:00
|
|
|
|
use ZM\Process\ProcessStateManager;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
2022-12-17 22:18:50 +08:00
|
|
|
|
#[AsCommand(name: 'server', description: '启动服务器', aliases: ['server:start'])]
|
2022-05-14 23:40:22 +08:00
|
|
|
|
class ServerStartCommand extends ServerCommand
|
2020-08-31 10:11:06 +08:00
|
|
|
|
{
|
2022-08-01 16:31:54 +08:00
|
|
|
|
public static function exportOptionArray(): array
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
$cmd = new self();
|
|
|
|
|
|
$cmd->configure();
|
2022-12-19 20:22:47 +08:00
|
|
|
|
return array_map(fn ($x) => $x->getDefault(), $cmd->getDefinition()->getOptions());
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
|
|
{
|
2020-11-03 21:02:24 +08:00
|
|
|
|
$this->setDefinition([
|
2022-08-01 16:31:54 +08:00
|
|
|
|
new InputOption('config-dir', null, InputOption::VALUE_REQUIRED, '指定其他配置文件目录'),
|
|
|
|
|
|
new InputOption('driver', null, InputOption::VALUE_REQUIRED, '指定驱动类型'),
|
|
|
|
|
|
new InputOption('log-level', null, InputOption::VALUE_REQUIRED, '调整消息等级到debug (log-level=4)'),
|
2022-03-15 18:05:33 +08:00
|
|
|
|
new InputOption('daemon', null, null, '以守护进程的方式运行框架'),
|
|
|
|
|
|
new InputOption('worker-num', null, InputOption::VALUE_REQUIRED, '启动框架时运行的 Worker 进程数量'),
|
|
|
|
|
|
new InputOption('watch', null, null, '监听 src/ 目录的文件变化并热更新'),
|
|
|
|
|
|
new InputOption('env', null, InputOption::VALUE_REQUIRED, '设置环境类型 (production, development, staging)'),
|
|
|
|
|
|
new InputOption('disable-safe-exit', null, null, '关闭安全退出(关闭后按CtrlC时直接杀死进程)'),
|
2022-03-20 21:04:07 +08:00
|
|
|
|
new InputOption('no-state-check', null, null, '关闭启动前框架运行状态检查'),
|
2022-03-29 02:10:09 +08:00
|
|
|
|
new InputOption('private-mode', null, null, '启动时隐藏MOTD和敏感信息'),
|
2022-08-21 16:05:58 +08:00
|
|
|
|
new InputOption('print-process-pid', null, null, '打印所有进程的PID'),
|
2020-11-03 21:02:24 +08:00
|
|
|
|
]);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$this->setHelp('直接运行可以启动');
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-14 23:40:22 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws ZMKnownException
|
2022-08-13 22:01:03 +08:00
|
|
|
|
* @throws InitException
|
2022-11-03 10:18:17 +08:00
|
|
|
|
* @throws \Exception
|
2022-08-01 16:31:54 +08:00
|
|
|
|
* @noinspection PhpComposerExtensionStubsInspection
|
2022-05-14 23:40:22 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
|
{
|
2022-08-01 16:31:54 +08:00
|
|
|
|
// 这段用于config的环境解析,但显然不是很好的方式,应该改成一个独立的方法,不应该在这里检查,但暂时搁置,TODO
|
|
|
|
|
|
/* if (($opt = $input->getOption('env')) !== null) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (!in_array($opt, ['production', 'staging', 'development', ''])) {
|
|
|
|
|
|
$output->writeln('<error> "--env" option only accept production, development, staging and [empty] ! </error>');
|
2021-04-06 01:19:56 +08:00
|
|
|
|
return 1;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
2022-08-01 16:31:54 +08:00
|
|
|
|
}*/
|
2022-08-13 22:01:03 +08:00
|
|
|
|
// 如果是支持多进程模式的,那么就检查框架进程的状态
|
|
|
|
|
|
if (ProcessManager::isSupportedMultiProcess()) {
|
2022-08-01 16:31:54 +08:00
|
|
|
|
$state = ProcessStateManager::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>");
|
|
|
|
|
|
$output->writeln('<error>不可以同时启动两个框架!</error>');
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2022-03-20 21:04:07 +08:00
|
|
|
|
}
|
2021-11-16 15:41:01 +08:00
|
|
|
|
}
|
2022-08-13 22:01:03 +08:00
|
|
|
|
// 框架启动的入口
|
2022-08-13 17:00:29 +08:00
|
|
|
|
(new Framework($input->getOptions()))->init()->start();
|
2021-04-06 01:19:56 +08:00
|
|
|
|
return 0;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|