From 36a3458581b9f2f248e97ccd3b2f03bb949c7f1c Mon Sep 17 00:00:00 2001 From: Suwings Date: Sun, 17 Jul 2022 14:04:17 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=20=E5=AE=8C=E5=96=84=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E9=97=AE=E9=A2=98=E4=B8=8E=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routers/Instance_router.ts | 4 +++- src/routers/instance_event_router.ts | 11 +++++++---- src/routers/stream_router.ts | 4 +++- src/service/protocol.ts | 8 ++++++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/routers/Instance_router.ts b/src/routers/Instance_router.ts index 78d5aef..a0e3b5d 100644 --- a/src/routers/Instance_router.ts +++ b/src/routers/Instance_router.ts @@ -391,7 +391,9 @@ routerApp.on("instance/outputlog", async (ctx, data) => { const text = await fs.readFile(filePath, { encoding: "utf-8" }); return protocol.response(ctx, text); } - protocol.responseError(ctx, new Error("终端日志文件不存在")); + protocol.responseError(ctx, new Error("终端日志文件不存在"), { + notPrintErr: true + }); } catch (err) { protocol.responseError(ctx, err); } diff --git a/src/routers/instance_event_router.ts b/src/routers/instance_event_router.ts index 27f1759..df7e2e7 100644 --- a/src/routers/instance_event_router.ts +++ b/src/routers/instance_event_router.ts @@ -31,21 +31,24 @@ const MAX_LOG_SIZE = 512; const buffer = new Map(); setInterval(() => { buffer.forEach((buf, instanceUuid) => { + if (!buf || !instanceUuid) return; const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`); if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR); try { const fileInfo = fs.statSync(logFilePath); if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath); } catch (err) {} - fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {}); + fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => { + buffer.set(instanceUuid, ""); + }); }); -}, 1000); +}, 500); // 输出流记录到缓存区 async function outputLog(instanceUuid: string, text: string) { - const buf = buffer.get(instanceUuid) + text; + const buf = (buffer.get(instanceUuid) ?? "") + text; if (buf.length > 1024 * 1024) buffer.set(instanceUuid, ""); - buffer.set(instanceUuid, buf); + buffer.set(instanceUuid, buf ?? null); } // 实例输出流事件 diff --git a/src/routers/stream_router.ts b/src/routers/stream_router.ts index 647b517..c370d5f 100644 --- a/src/routers/stream_router.ts +++ b/src/routers/stream_router.ts @@ -73,7 +73,9 @@ routerApp.on("stream/auth", (ctx, data) => { }); protocol.response(ctx, true); } catch (error) { - protocol.responseError(ctx, error); + protocol.responseError(ctx, error, { + notPrintErr: true + }); } }); diff --git a/src/service/protocol.ts b/src/service/protocol.ts index 9e53e36..cb0c709 100644 --- a/src/service/protocol.ts +++ b/src/service/protocol.ts @@ -36,6 +36,10 @@ export interface IPacket { data: any; } +export interface IResponseErrorConfig { + notPrintErr: boolean; +} + // 全局 Socket 储存 const globalSocket = new Map(); @@ -48,14 +52,14 @@ export function response(ctx: RouterContext, data: any) { ctx.socket.emit(ctx.event, packet); } -export function responseError(ctx: RouterContext, err: Error | string) { +export function responseError(ctx: RouterContext, err: Error | string, config?: IResponseErrorConfig) { let errinfo: any = ""; if (err) errinfo = err.toString(); else errinfo = err; const packet = new Packet(ctx.uuid, STATUS_ERR, ctx.event, errinfo); // 忽略因为重启守护进程没有刷新网页的权限不足错误 if (err.toString().includes("[Unauthorized Access]")) return ctx.socket.emit(ctx.event, packet); - logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err); + if (!config?.notPrintErr) logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err); ctx.socket.emit(ctx.event, packet); }