@@ -25,38 +25,29 @@ import RouterContext from "../entity/ctx";
2525import * as protocol from "../service/protocol" ;
2626import InstanceSubsystem from "../service/system_instance" ;
2727import 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+ // 输出流记录到缓存区
3445async 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// 默认加入到数据缓存中以控制发送速率确保其稳定性
6253InstanceSubsystem . 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// 实例退出事件
0 commit comments