Przeglądaj źródła

日志查询导致node 被杀死

hl 10 miesięcy temu
rodzic
commit
90e9363bed
4 zmienionych plików z 27 dodań i 30 usunięć
  1. 1 1
      client.js
  2. 1 1
      src/robot.js
  3. 16 14
      src/utils/file.js
  4. 9 14
      src/web.js

+ 1 - 1
client.js

@@ -47,7 +47,7 @@ process.on('SIGTERM', async () => {
 
 // 还可以处理未捕获的异常
 process.on('uncaughtException', (err) => {
-    console.error('捕获到未处理的异常:', err);
+    logger.error('捕获到未处理的异常:', err);
     // 在这里执行清理工作
     process.exit(1); // 非零退出码表示异常退出
 });

+ 1 - 1
src/robot.js

@@ -251,7 +251,7 @@ async function run(param) {
             // logger.info(`message: ${msg}`);
         });
         app.childProcess.on('error', (err) => {
-            console.error('子程序-异常:', err);
+            logger.error('子程序-异常:', err);
             app.threadStatus = RobotStatus.ERROR
             app.errorMessage = `子线程发生异常!! ${err}`
             robotStatus(app)

+ 16 - 14
src/utils/file.js

@@ -46,10 +46,10 @@ function checkFilePath(filePath) {
 function writeFile(filePath, text, breakFun) {
     fs.writeFile(filePath, text, (err) => {
         if (err) {
-            console.error('写入文件时发生错误:', err);
+            logger.error('写入文件时发生错误:', err);
             breakFun(err)
         } else {
-            console.log('文件已成功写入!');
+            logger.log('文件已成功写入!');
             breakFun(null, true)
         }
     });
@@ -93,19 +93,21 @@ function delFile() {
 function getLastFile(dirPath, number, callback) {
     fs.readdir(dirPath, async (err, files) => {
         if (err) {
-            console.error(`无法列出目录。`, err);
-            process.exit(1);
-        }
-        let fileList = [];
-        for (file of files) {
-            const filePath = path.join(dirPath, file);
-            const stats = await fs.promises.stat(filePath);
-            fileList.push({name: file, time: stats.mtimeMs});
+            logger.error(`无法列出目录。`, err);
+            // process.exit(1);
+        } else {
+            let fileList = [];
+            for (file of files) {
+                const filePath = path.join(dirPath, file);
+                const stats = await fs.promises.stat(filePath);
+                fileList.push({name: file, time: stats.mtimeMs});
+            }
+            fileList.sort((a, b) => b.time - a.time);
+            lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
+            lastFileNameList = lastFileList.map((item) => item.name);
+
+            callback(lastFileNameList.reverse(), lastFileList.reverse());
         }
-        fileList.sort((a, b) => b.time - a.time);
-        lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
-        lastFileNameList = lastFileList.map((item) => item.name);
-        callback(lastFileNameList.reverse(), lastFileList.reverse());
     });
 }
 

+ 9 - 14
src/web.js

@@ -28,7 +28,7 @@ function init() {
             var md5 = req.headers.catilla
             //默认32位
             const md5Str = crypto.createHash('md5').update(k + timestamp).digest('hex');
-            logger.info(JSON.stringify(md5Str))
+            // logger.info(JSON.stringify(md5Str))
             if (md5 === md5Str) {
                 next();
             } else {
@@ -94,21 +94,16 @@ function init() {
         let result = [];
         if (port !== -1) {
             var logPath = "./logs" + port
+            logger.info(`访问-日志相对目录:${logPath}`)
             // 将相对路径转换为绝对路径
-            // file.checkPathSync(directoryPath)
-            try {
-                const directoryPath = path.resolve(logPath);
-                logger.info(`日志相对地址:${logPath}`)
-                file.getLastFile(logPath, 10, (fileNameList) => {
-                    // logger.info('??1:', fileNameList);
-                    result = file.readLastNLines(logPath, fileNameList, n);
-                    // logger.info('?????:', result);
-                    res.send({'code': 200, 'data': result, "message": "SUCCESS"});
-                });
-            } catch (error) {
-                logger.info('异常了....');
+            const directoryPath = path.resolve(logPath);
+            file.checkPathSync(directoryPath)
+            file.getLastFile(logPath, 10, (fileNameList) => {
+                // logger.info('??1:', fileNameList);
+                result = file.readLastNLines(logPath, fileNameList, n);
+                // logger.info('?????:', result);
                 res.send({'code': 200, 'data': result, "message": "SUCCESS"});
-            }
+            });
         } else {
             res.send({'code': 200, 'data': result, "message": "SUCCESS"});
         }