From 5b5a752e79fb4632ac2d137ac032c90bebb5a5e4 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 15 Nov 2019 14:30:26 +0100 Subject: [PATCH] Log Auto Mantainance Implemented --- DebugLog.js | 21 +++++++------------ FileStorage.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- Root.js | 4 ++++ 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/DebugLog.js b/DebugLog.js index 87137a604..a51b02b0b 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -21,9 +21,7 @@ exports.newDebugLog = function newDebugLog() { }; let accumulatedLog = "["; - let disableLogging; - let internalLoopCounter = -1; return thisObject; @@ -80,35 +78,32 @@ exports.newDebugLog = function newDebugLog() { } let fileName; - if (internalLoopCounter >= 0) { - fileName = "Loop." + pad(thisObject.bot.loopCounter, 8) + "." + pad(internalLoopCounter, 4) + ".json"; - } else { - fileName = "Loop." + pad(thisObject.bot.loopCounter, 8) + ".json"; - } writeLog(); + /* This is the implementation of the mechanism to auto-mantain logs. */ + thisObject.bot.LOGS_TO_DELETE_QUEUE.push(filePath + '/' + fileName) + if (thisObject.bot.LOGS_TO_DELETE_QUEUE.length > thisObject.bot.DELETE_QUEUE_SIZE) { + let fileToDelete = thisObject.bot.LOGS_TO_DELETE_QUEUE[0] + thisObject.bot.LOGS_TO_DELETE_QUEUE.splice(0,1) + fileStorage.deleteTextFile(thisObject.bot.devTeam, fileToDelete); + } + function writeLog() { fileStorage.createTextFile(thisObject.bot.devTeam, filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated); - function onFileCreated(err) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> err = "+ err.message); - setTimeout(writeLog, 10000); // Lets retry until we make it. return; } - contentToPersist = ""; - //thisObject = {}; } } } catch (err) { diff --git a/FileStorage.js b/FileStorage.js index 5f5534c3d..ee784f5fa 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -19,7 +19,8 @@ exports.newFileStorage = function newFileStorage(logger) { let thisObject = { getTextFile: getTextFile, - createTextFile: createTextFile + createTextFile: createTextFile, + deleteTextFile: deleteTextFile } return thisObject @@ -207,6 +208,60 @@ exports.newFileStorage = function newFileStorage(logger) { } } + function deleteTextFile(container, filePath, callBackFunction) { + + logger.write(MODULE_NAME, '[INFO] FileStorage -> deleteTextFile -> Entering Function.') + + container = container.toLowerCase() + + /* Choose path for either logs or data */ + let fileLocation + if (filePath.indexOf("/Logs/") > 0) { + fileLocation = process.env.LOG_PATH + '/' + container + '/' + filePath + } else { + fileLocation = process.env.STORAGE_PATH + '/' + container + '/' + filePath + } + + try { + + logger.write(MODULE_NAME, '[INFO] FileStorage -> deleteTextFile -> fileLocation: ' + fileLocation) + + const fs = require('fs') + fs.unlink(fileLocation, onUnlinked) + + function onUnlinked(err) { + if (err) { + if (callBackFunction !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } else { + if (callBackFunction !== undefined) { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } + } + } + } catch (err) { + logger.write(MODULE_NAME, '[WARN] FileStorage -> deleteTextFile -> Error writing file -> file = ' + fileLocation) + logger.write(MODULE_NAME, '[WARN] FileStorage -> deleteTextFile -> Error writing file -> err = ' + err.stack) + if (callBackFunction !== undefined) { + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } + + function retry() { + if (currentRetryWriteTextFile < MAX_RETRY) { + currentRetryWriteTextFile++ + logger.write(MODULE_NAME, '[WARN] FileStorage -> createTextFile -> retry -> Will try to write the file again -> Retry #: ' + currentRetryWriteTextFile) + createTextFile(container, filePath, fileContent, callBackFunction) + } else { + currentRetryWriteTextFile = 0 + logger.write(MODULE_NAME, '[ERROR] FileStorage -> createTextFile -> retry -> Max retries reached writting a file. Giving up.') + logger.write(MODULE_NAME, '[ERROR] FileStorage -> createTextFile -> retry -> file = ' + fileLocation) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } + } + /* Function to create folders of missing folders at any path. */ function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) { diff --git a/Root.js b/Root.js index e7e21d57d..263742304 100644 --- a/Root.js +++ b/Root.js @@ -234,6 +234,10 @@ botConfig.debug = {}; botConfig.processNode = global.TASK_NODE.bot.processes[processIndex] + /* Logs Mantainance Stuff */ + botConfig.LOGS_TO_DELETE_QUEUE = [] + botConfig.DELETE_QUEUE_SIZE = 10 // This number represents how many log files can be at the queue at any point in time, which means how many logs are not still deleted. + /* Simplifying the access to basic info */ botConfig.devTeam = global.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.parentNode.code.codeName