Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 36a3458

Browse files
committed
Feat: 完善一些细节问题与日志
1 parent 1287190 commit 36a3458

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/routers/Instance_router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ routerApp.on("instance/outputlog", async (ctx, data) => {
391391
const text = await fs.readFile(filePath, { encoding: "utf-8" });
392392
return protocol.response(ctx, text);
393393
}
394-
protocol.responseError(ctx, new Error("终端日志文件不存在"));
394+
protocol.responseError(ctx, new Error("终端日志文件不存在"), {
395+
notPrintErr: true
396+
});
395397
} catch (err) {
396398
protocol.responseError(ctx, err);
397399
}

src/routers/instance_event_router.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,24 @@ const MAX_LOG_SIZE = 512;
3131
const buffer = new Map<string, string>();
3232
setInterval(() => {
3333
buffer.forEach((buf, instanceUuid) => {
34+
if (!buf || !instanceUuid) return;
3435
const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`);
3536
if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR);
3637
try {
3738
const fileInfo = fs.statSync(logFilePath);
3839
if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath);
3940
} catch (err) {}
40-
fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {});
41+
fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {
42+
buffer.set(instanceUuid, "");
43+
});
4144
});
42-
}, 1000);
45+
}, 500);
4346

4447
// 输出流记录到缓存区
4548
async function outputLog(instanceUuid: string, text: string) {
46-
const buf = buffer.get(instanceUuid) + text;
49+
const buf = (buffer.get(instanceUuid) ?? "") + text;
4750
if (buf.length > 1024 * 1024) buffer.set(instanceUuid, "");
48-
buffer.set(instanceUuid, buf);
51+
buffer.set(instanceUuid, buf ?? null);
4952
}
5053

5154
// 实例输出流事件

src/routers/stream_router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ routerApp.on("stream/auth", (ctx, data) => {
7373
});
7474
protocol.response(ctx, true);
7575
} catch (error) {
76-
protocol.responseError(ctx, error);
76+
protocol.responseError(ctx, error, {
77+
notPrintErr: true
78+
});
7779
}
7880
});
7981

src/service/protocol.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export interface IPacket {
3636
data: any;
3737
}
3838

39+
export interface IResponseErrorConfig {
40+
notPrintErr: boolean;
41+
}
42+
3943
// 全局 Socket 储存
4044
const globalSocket = new Map<String, Socket>();
4145

@@ -48,14 +52,14 @@ export function response(ctx: RouterContext, data: any) {
4852
ctx.socket.emit(ctx.event, packet);
4953
}
5054

51-
export function responseError(ctx: RouterContext, err: Error | string) {
55+
export function responseError(ctx: RouterContext, err: Error | string, config?: IResponseErrorConfig) {
5256
let errinfo: any = "";
5357
if (err) errinfo = err.toString();
5458
else errinfo = err;
5559
const packet = new Packet(ctx.uuid, STATUS_ERR, ctx.event, errinfo);
5660
// 忽略因为重启守护进程没有刷新网页的权限不足错误
5761
if (err.toString().includes("[Unauthorized Access]")) return ctx.socket.emit(ctx.event, packet);
58-
logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err);
62+
if (!config?.notPrintErr) logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err);
5963
ctx.socket.emit(ctx.event, packet);
6064
}
6165

0 commit comments

Comments
 (0)