Browse Source

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

hl 3 weeks ago
parent
commit
275aa5a45b
1 changed files with 52 additions and 20 deletions
  1. 52 20
      src/utils/file.js

+ 52 - 20
src/utils/file.js

@@ -92,42 +92,74 @@ function delFile() {
 
 /***********************************/
 function getLastFile(dirPath, number, callback) {
-    fs.readdir(dirPath,  (err, files) => {
+    fs.readdir(dirPath, (err, files) => {
         // logger.info(`---------------------4`)
         if (err) {
             logger.error(`无法列出目录。`, err);
             // process.exit(1);
         } else {
-            let fileList = [];
+            // let fileList = [];
             //提示日志默认是根据日志生成的,拿到的files 也是 按照生成日志排序的,所以
             // 按照倒叙获取,如果一个文件内容满足num,就不在继续查询,如果不够,继续获取文件
-            for (file of files) {
+            const filePromises = files.map(file => {
                 const filePath = path.join(dirPath, file);
-                // logger.info("所有文件:",filePath)
-                fs.promises.stat(filePath).then((stats) =>{
+                return fs.promises.stat(filePath).then((stats) => {
                     if (stats.isFile()) {
-                        let tt = stats.birthtime.getTime()
+                        let tt = stats.birthtime.getTime();
                         fileList.push({name: file, time: tt});
-                        logger.info("----------------所有文件:" + filePath + "时间:" + tt)
+                        logger.info("----------------所有文件:" + filePath + "时间:" + tt);
                     }
                 });
-            }
+            });
+            // for (file of files) {
+            //     const filePath = path.join(dirPath, file);
+            //     // logger.info("所有文件:",filePath)
+            //      fs.promises.stat(filePath).then((stats) =>{
+            //         if (stats.isFile()) {
+            //             let tt = stats.birthtime.getTime()
+            //             filePromises.push({name: file, time: tt});
+            //             logger.info("----------------所有文件:" + filePath + "时间:" + tt)
+            //         }
+            //     });
+            // }
 
-            for (a of fileList) {
-                const filePath = path.join(dirPath, a.name);
-                logger.info("复查:" + filePath + "\ttime:" + a.time)
-            }
+            // 等待所有的 Promise 完成
+            Promise.all(filePromises).then(() => {
+                for (a of fileList) {
+                    const filePath = path.join(dirPath, a.name);
+                    logger.info("复查:" + filePath + "\ttime:" + a.time)
+                }
 
 
-            fileList.sort((a, b) => b.time - a.time);
-            for (a of fileList) {
-                const filePath = path.join(dirPath, a.name);
-                logger.info("排序:" + filePath + "\ttime:" + a.time)
-            }
-            let lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
-            let lastFileNameList = lastFileList.map((item) => item.name);
+                fileList.sort((a, b) => b.time - a.time);
+                for (a of fileList) {
+                    const filePath = path.join(dirPath, a.name);
+                    logger.info("排序:" + filePath + "\ttime:" + a.time)
+                }
+                let lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
+                let lastFileNameList = lastFileList.map((item) => item.name);
 
-            callback(lastFileNameList.reverse(), lastFileList.reverse());
+                callback(lastFileNameList.reverse(), lastFileList.reverse());
+            }).catch(err => {
+                logger.error("Error occurred:", err);
+            });
+
+
+            // for (a of fileList) {
+            //     const filePath = path.join(dirPath, a.name);
+            //     logger.info("复查:" + filePath + "\ttime:" + a.time)
+            // }
+            //
+            //
+            // fileList.sort((a, b) => b.time - a.time);
+            // for (a of fileList) {
+            //     const filePath = path.join(dirPath, a.name);
+            //     logger.info("排序:" + filePath + "\ttime:" + a.time)
+            // }
+            // let lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
+            // let lastFileNameList = lastFileList.map((item) => item.name);
+            //
+            // callback(lastFileNameList.reverse(), lastFileList.reverse());
         }
     });
 }