feat: 新增启动webui attach父进程

This commit is contained in:
zhiyu1998 2024-11-22 15:53:08 +08:00
parent be8c95e6fd
commit 41352ce7e0
5 changed files with 52 additions and 2 deletions

View File

@ -1,3 +1,4 @@
isOpenWebUI: false # 是否开启webui
defaultPath: './data/rcmp4/' # 保存视频的位置 defaultPath: './data/rcmp4/' # 保存视频的位置
videoSizeLimit: 70 # 视频大小限制单位MB超过大小则转换成群文件上传 videoSizeLimit: 70 # 视频大小限制单位MB超过大小则转换成群文件上传
proxyAddr: '127.0.0.1' # 魔法地址 proxyAddr: '127.0.0.1' # 魔法地址

View File

@ -1,12 +1,15 @@
import fs from "node:fs"; import fs from "node:fs";
import path from "path"; import path from "path";
import config from "./model/config.js"; import config from "./model/config.js";
import { startNextJs } from "./start-nextjs.js";
if (!global.segment) { if (!global.segment) {
global.segment = (await import("oicq")).segment global.segment = (await import("oicq")).segment
} }
// 加载版本号 // 加载版本号
const versionData = config.getConfig("version"); const versionData = config.getConfig("version");
// 加载是否使用WebUI
const isOpenWebUI = config.getConfig("tools").isOpenWebUI;
// 加载名称 // 加载名称
const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json'); const packageJsonPath = path.join('./plugins', 'rconsole-plugin', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
@ -35,4 +38,10 @@ for (let i in files) {
} }
apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]; apps[name] = ret[i].value[Object.keys(ret[i].value)[0]];
} }
// 检查是否启动 webui
if (isOpenWebUI) {
startNextJs();
}
export { apps }; export { apps };

View File

@ -2,6 +2,11 @@
"name": "rconsole-plugin", "name": "rconsole-plugin",
"description": "R-Plugin", "description": "R-Plugin",
"type": "module", "type": "module",
"scripts": {
"dev": "cd server && next dev -p 4016",
"start": "cd server && next start -p 4016",
"build": "cd server && next build"
},
"dependencies": { "dependencies": {
"axios": "^1.3.4", "axios": "^1.3.4",
"chart.js": "^4.4.6", "chart.js": "^4.4.6",

View File

@ -2,8 +2,9 @@ import fs from 'fs';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import path from 'path'; import path from 'path';
const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml');
export async function GET(req, res) { export async function GET(req, res) {
const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml');
try { try {
const yamlContent = await fs.promises.readFile(configPath, 'utf8'); const yamlContent = await fs.promises.readFile(configPath, 'utf8');
const config = yaml.load(yamlContent); const config = yaml.load(yamlContent);
@ -21,7 +22,6 @@ export async function GET(req, res) {
} }
export async function POST(req, res) { export async function POST(req, res) {
const configPath = path.join(process.cwd(), "../", 'config', 'tools.yaml');
try { try {
const updates = await req.json(); const updates = await req.json();

35
start-nextjs.js Normal file
View File

@ -0,0 +1,35 @@
import { spawn } from 'child_process';
logger.info(`[R插件][Next.js监测], 父进程 PID: ${process.pid}`);
let childProcess = null;
// 启动子进程运行 Next.js
export const startNextJs = () => {
logger.info(logger.yellow('[R插件][Next.js监测],启动 Next.js 进程...'));
// 加载名称
childProcess = spawn('pnpm', ['run', 'dev'], {
cwd: './plugins/rconsole-plugin', // 指定工作目录
stdio: 'inherit' // 继承父进程的标准输入输出
});
// 子进程异常退出时捕获信号
childProcess.on('close', (code) => {
logger.error(`[R插件][Next.js监测]Next.js 进程发生异常 ${code}`);
childProcess = null;
});
};
// 捕获父进程退出信号
const cleanup = () => {
logger.info(logger.yellow('[R插件][Next.js监测] 父进程退出,终止子进程...'));
if (childProcess) {
childProcess.kill(); // 终止子进程
}
process.exit();
};
// 绑定父进程的退出信号
process.on('SIGINT', cleanup); // Ctrl+C 信号
process.on('SIGTERM', cleanup); // kill 命令信号
process.on('exit', cleanup); // 正常退出