Log Auto Mantainance Implemented

This commit is contained in:
Luis Fernando Molina committed 2019-11-15 14:30:26 +01:00
1 parent 5890b62ec2
commit 5b5a752e79
3 files changed
+68 -14

No files matched your search

+8 -13
View File
@@ -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) {
+56 -1
View File
@@ -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 } = {}) {
+4
View File
@@ -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