🎈 pref:压缩 WebUI 78.75% 内存占用(400MB -> 85MB)

This commit is contained in:
zhiyu1998 2024-11-25 19:54:32 +08:00
parent cc4488f666
commit 49f82b695a
6 changed files with 38 additions and 6 deletions

View File

@ -2,6 +2,7 @@ import { REDIS_YUNZAI_WEBUI } from "../constants/constant.js";
import config from "../model/config.js"; import config from "../model/config.js";
import { constructPublicIPsMsg } from "../utils/network.js"; import { constructPublicIPsMsg } from "../utils/network.js";
import { redisSetKey } from "../utils/redis-util.js"; import { redisSetKey } from "../utils/redis-util.js";
import { buildNextJs } from "../utils/start-nextjs.js";
import { getBotLoginInfo, getBotStatus, getBotVersionInfo, sendPrivateMsg } from "../utils/yunzai-util.js"; import { getBotLoginInfo, getBotStatus, getBotVersionInfo, sendPrivateMsg } from "../utils/yunzai-util.js";
export class WebUI extends plugin { export class WebUI extends plugin {
@ -48,13 +49,15 @@ export class WebUI extends plugin {
async rWebSwitch(e) { async rWebSwitch(e) {
config.updateField("tools", "isOpenWebUI", !this.isOpenWebUI); config.updateField("tools", "isOpenWebUI", !this.isOpenWebUI);
const realIsOpenWebUI = config.getConfig("tools").isOpenWebUI; const realIsOpenWebUI = config.getConfig("tools").isOpenWebUI;
// 初始化数据
await this.initData(e, realIsOpenWebUI);
// 这里有点延迟,需要写反
e.reply(`R插件可视化面板${ realIsOpenWebUI ? "✅已开启" : "❌已关闭" },重启后生效`);
if (realIsOpenWebUI) { if (realIsOpenWebUI) {
// 初始化数据
await this.initData(e, realIsOpenWebUI);
e.reply(`R插件可视化面板正在构建中请稍等...`);
// 动态编译生产环境
await buildNextJs();
await sendPrivateMsg(e, constructPublicIPsMsg()); await sendPrivateMsg(e, constructPublicIPsMsg());
} }
e.reply(`R插件可视化面板${ realIsOpenWebUI ? "✅已开启" : "❌已关闭" },重启后生效`);
return true; return true;
} }

View File

@ -42,7 +42,7 @@ for (let i in files) {
// 检查是否启动 webui // 检查是否启动 webui
if (isOpenWebUI) { if (isOpenWebUI) {
startNextJs('dev'); startNextJs('start');
logger.info(constructPublicIPsMsg()); logger.info(constructPublicIPsMsg());
} }

View File

@ -6,6 +6,7 @@
"dev": "cd server && next dev -p 4016", "dev": "cd server && next dev -p 4016",
"dev6": "cd server && HOST=:: next dev -p 4016", "dev6": "cd server && HOST=:: next dev -p 4016",
"start": "cd server && next start -p 4016", "start": "cd server && next start -p 4016",
"start6": "cd server && HOST=:: next start -p 4016",
"build": "cd server && next build" "build": "cd server && next build"
}, },
"dependencies": { "dependencies": {

View File

@ -1,3 +1,4 @@
import { unstable_noStore as noStore } from 'next/cache';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import os from 'os'; import os from 'os';
import si from 'systeminformation'; import si from 'systeminformation';
@ -85,6 +86,8 @@ async function getNetworkStats() {
} }
export async function GET() { export async function GET() {
// 这个不允许删除,否则无法做到实时获取
noStore();
try { try {
const stats = await getNetworkStats(); const stats = await getNetworkStats();
return new Response(JSON.stringify(stats), { return new Response(JSON.stringify(stats), {

View File

@ -1,7 +1,10 @@
import { unstable_noStore as noStore } from 'next/cache';
import si from 'systeminformation'; import si from 'systeminformation';
import os from 'os'; import os from 'os';
export async function GET(request, { params }) { export async function GET(request, { params }) {
// 这个不允许删除,否则无法做到实时获取
noStore();
try { try {
// 获取CPU信息 // 获取CPU信息
const cpuInfo = await si.cpu(); const cpuInfo = await si.cpu();

View File

@ -5,6 +5,28 @@ logger.mark(`[R插件][WebUI], 父进程 PID: ${process.pid}`);
let nextjsProcess = null; let nextjsProcess = null;
// 构建应用程序
export const buildNextJs = () => {
logger.info(logger.yellow('[R插件][WebUI],正在构建 Next.js 应用...'));
return new Promise((resolve, reject) => {
const buildProcess = spawn('pnpm', ['run', 'build'], {
cwd: './plugins/rconsole-plugin/server',
stdio: 'ignore',
shell: true,
});
buildProcess.on('close', (code) => {
if (code === 0) {
logger.info(logger.yellow('[R插件][Next.js监测],构建完成。'));
resolve();
} else {
logger.error(`[R插件][WebUI监测],构建失败,退出码:${code}`);
reject(new Error('Build failed'));
}
});
});
};
// 启动子进程运行 Next.js // 启动子进程运行 Next.js
export const startNextJs = (mode = 'start') => { export const startNextJs = (mode = 'start') => {
let script = mode === 'start' ? 'start' : 'dev'; let script = mode === 'start' ? 'start' : 'dev';
@ -13,7 +35,7 @@ export const startNextJs = (mode = 'start') => {
// 判断是不是只有ipv6地址 // 判断是不是只有ipv6地址
if (hasIPv6Only()) { if (hasIPv6Only()) {
script = 'dev6'; script = 'start6';
} }
nextjsProcess = spawn('pnpm', ['run', script], { nextjsProcess = spawn('pnpm', ['run', script], {