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

Commit 6083435

Browse files
committed
Feat: 恢复日志记录功能
1 parent d11312e commit 6083435

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

src/routers/instance_event_router.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,29 @@ import RouterContext from "../entity/ctx";
2525
import * as protocol from "../service/protocol";
2626
import InstanceSubsystem from "../service/system_instance";
2727
import fs from "fs-extra";
28-
const TIME_SPEED = 100;
29-
const MAX_CHAR_SIZE = 40;
30-
const MAX_LOG_SIZE = 256;
28+
const MAX_LOG_SIZE = 1024;
3129

32-
// 输出流记录到文本
33-
// 此函数执行频率极快,可能有待优化效率
30+
// 缓存区
31+
const buffer = new Map<string, string>();
32+
setInterval(() => {
33+
buffer.forEach((buf, instanceUuid) => {
34+
const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`);
35+
if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR);
36+
try {
37+
const fileInfo = fs.statSync(logFilePath);
38+
if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath);
39+
} catch (err) {}
40+
fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {});
41+
});
42+
}, 1000);
43+
44+
// 输出流记录到缓存区
3445
async function outputLog(instanceUuid: string, text: string) {
35-
const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`);
36-
if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR);
37-
try {
38-
const fileInfo = fs.statSync(logFilePath);
39-
if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath);
40-
} catch (err) {}
41-
await fs.writeFile(logFilePath, text, { encoding: "utf-8", flag: "a" });
46+
const buf = buffer.get(instanceUuid) + text;
47+
if (buf.length > 1024 * 1024) buffer.set(instanceUuid, "");
48+
buffer.set(instanceUuid, buf);
4249
}
4350

44-
// 定时发送程序输出流日志广播
45-
// 此设计可以一次性打包多次内容,一并发送
46-
const outputStreamCache: any = {};
47-
setInterval(function () {
48-
for (const instanceUuid in outputStreamCache) {
49-
const text = outputStreamCache[instanceUuid];
50-
InstanceSubsystem.forEachForward(instanceUuid, (socket) => {
51-
protocol.msg(new RouterContext(null, socket), "instance/stdout", {
52-
instanceUuid: instanceUuid,
53-
text: text
54-
});
55-
});
56-
delete outputStreamCache[instanceUuid];
57-
}
58-
}, TIME_SPEED);
59-
6051
// 实例输出流事件
6152
// 默认加入到数据缓存中以控制发送速率确保其稳定性
6253
InstanceSubsystem.on("data", (instanceUuid: string, text: string) => {
@@ -66,18 +57,10 @@ InstanceSubsystem.on("data", (instanceUuid: string, text: string) => {
6657
text: text
6758
});
6859
});
69-
// if (outputStreamCache[instanceUuid]) {
70-
// if (outputStreamCache[instanceUuid].length > 1000 * MAX_CHAR_SIZE)
71-
// return (outputStreamCache[instanceUuid] +=
72-
// "\n[warning] the output data is too fast, more content has been blocked at this moment in order to ensure stability.\n[警告] 输出流数据过快,为保证稳定性,已屏蔽此刻更多内容....\n");
73-
// outputStreamCache[instanceUuid] += text;
74-
// } else {
75-
// outputStreamCache[instanceUuid] = text;
76-
// }
77-
// // 输出内容追加到log文件
78-
// outputLog(instanceUuid, text)
79-
// .then(() => {})
80-
// .catch(() => {});
60+
// 输出内容追加到log文件
61+
outputLog(instanceUuid, text)
62+
.then(() => {})
63+
.catch(() => {});
8164
});
8265

8366
// 实例退出事件

src/service/system_instance.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,8 @@ class InstanceSubsystem extends EventEmitter {
9999
cfg.cwd = path.normalize(`${INSTANCE_DATA_DIR}/${instance.instanceUuid}`);
100100
if (!fs.existsSync(cfg.cwd)) fs.mkdirsSync(cfg.cwd);
101101
}
102-
// 针对中文操作系统编码自动选择
103-
if (os.platform() === "win32") {
104-
cfg.ie = cfg.oe = cfg.fileCode = "gbk";
105-
} else {
106-
cfg.ie = cfg.oe = cfg.fileCode = "utf-8";
107-
}
102+
// 设置默认输入输出编码
103+
cfg.ie = cfg.oe = cfg.fileCode = "utf8";
108104
// 根据参数构建并初始化类型
109105
instance.parameters(cfg);
110106
instance.forceExec(new FunctionDispatcher());

0 commit comments

Comments
 (0)