Bladeren bron

将日志 与 机器人状态详情进行缓存,

hl 3 weken geleden
bovenliggende
commit
95d6d77a52
2 gewijzigde bestanden met toevoegingen van 33 en 35 verwijderingen
  1. 29 32
      src/Interval.js
  2. 4 3
      src/utils/file.js

+ 29 - 32
src/Interval.js

@@ -100,42 +100,39 @@ function cacheLogs() {
     setInterval(() => {
         // logger.info('当前2',robot.appMap);
         robot.appMap.forEach((value, key) => {
-            zz(value)
+            var thisTime = new Date().getTime()
+            let port = value.port
+            let n = 200
+            let messlist = value.messlist
+            if (value.id !== -1 && port !== -1) {
+                logger.info(`~~~缓存日志${value.id}`)
+                try {
+                    var logPath = "./logs" + port
+                    logger.info(`访问-日志相对目录:${logPath}`)
+                    const directoryPath = path.resolve(logPath);
+                    file.checkPathSync(directoryPath)
+                    file.getLastFile(logPath, 5, (fileNameList, _) => {
+                        logger.info('-----文件:', fileNameList);
+                        file.getLatestLogEntries(logPath, fileNameList.reverse(), n)
+                            .then((result) => {
+                                if (messlist.length === 0) {
+                                    value.messlist = result
+                                } else {
+                                    if (messlist[0] !== result[0]) {
+                                        value.messlist = result
+                                    }
+                                }
+                            });
+                    });
+                } catch (e) {
+                    logger.info('异步缓存日志异常~~', e);
+                }
+
+            }
         });
     }, intervalInMilliseconds);
 }
 
- function zz(value) {
-    var thisTime = new Date().getTime()
-    let port = value.port
-    let n = 200
-    let messlist = value.messlist
-    if (value.id !== -1 && port !== -1) {
-
-        logger.info(`~~~缓存日志${value.id}`)
-        try {
-            var logPath = "./logs" + port
-            logger.info(`访问-日志相对目录:${logPath}`)
-            const directoryPath = path.resolve(logPath);
-            file.checkPathSync(directoryPath)
-            file.getLastFile(logPath, 5,  (fileNameList, _) => {
-                logger.info('-----文件:', fileNameList);
-                file.getLatestLogEntries(logPath, fileNameList.reverse(), n).then((result) =>{
-                    if (messlist.length === 0) {
-                        value.messlist = result
-                    } else {
-                        if (messlist[0] !== result[0]) {
-                            value.messlist = result
-                        }
-                    }
-                });
-            });
-        } catch (e) {
-            logger.info('异步缓存日志异常~~', e);
-        }
-
-    }
-}
 
 function cachePredictorState() {
     // 设置定时器,每隔一定时间(比如每隔5秒)发送一次GET请求

+ 4 - 3
src/utils/file.js

@@ -113,10 +113,11 @@ function getLastFile(dirPath, number, callback) {
 
             fileList.sort((a, b) => b.time - a.time);
             for (a of fileList) {
-                logger.info("排序之后的顺序:" + dirPath + a.name + "\ttime:" + a.time)
+                const filePath = path.join(dirPath, a.name);
+                logger.info("排序之后的顺序:" + filePath + "\ttime:" + a.time)
             }
-            lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
-            lastFileNameList = lastFileList.map((item) => item.name);
+            let lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
+            let lastFileNameList = lastFileList.map((item) => item.name);
 
             callback(lastFileNameList.reverse(), lastFileList.reverse());
         }