Bug fixes that improve resilience when running live

This commit is contained in:
Luis Fernando Molina committed 2020-04-27 10:13:52 +02:00
1 parent 7cd9727bab
commit ec6e9fc907
20 files changed
+288 -153

No files matched your search

+7 -1
View File
@@ -17,7 +17,8 @@ exports.newDebugLog = function newDebugLog() {
write: write,
newInternalLoop: newInternalLoop,
persist: persist, // This method is executed at the end of each Main Loop.
initialize: initialize
initialize: initialize,
finalize: finalize
};
let accumulatedLog = "[";
@@ -37,6 +38,10 @@ exports.newDebugLog = function newDebugLog() {
}
}
function finalize() {
persist()
}
function newInternalLoop(pBot, pProcess, date, percentage) {
if (percentage === undefined) {
percentage = ""
@@ -64,6 +69,7 @@ exports.newDebugLog = function newDebugLog() {
/* Here we actually write the content of the in-memory log to a file */
try {
if (accumulatedLog === "[") {return} // nothing to persist at the moment.
internalLoopCounter++;
+1 -1
View File
@@ -38,7 +38,7 @@
'secret': secret,
'timeout': 30000,
'enableRateLimit': true,
verbose: true,
verbose: false,
options: options
}
+36 -23
View File
@@ -58,7 +58,7 @@ exports.newFileStorage = function newFileStorage(logger, host, port) {
fs.readFile(fileLocation, onFileRead)
} else {
/* We read the file via a web server over http */
getFileViaHTTP(fileLocation, onFileRead)
getFileViaHTTP(fileLocation, onFileRead, callBackFunction)
}
function onFileRead(err, text) {
@@ -130,7 +130,7 @@ exports.newFileStorage = function newFileStorage(logger, host, port) {
fs.readFile(fileLocation + '.Previous.json', onPreviousFileRead)
} else {
/* We read the file via a web server over http */
getFileViaHTTP(fileLocation + '.Previous.json', onPreviousFileRead)
getFileViaHTTP(fileLocation + '.Previous.json', onPreviousFileRead, callBackFunction)
}
function onPreviousFileRead(err, text) {
@@ -435,7 +435,7 @@ exports.newFileStorage = function newFileStorage(logger, host, port) {
}, initDir);
}
function getFileViaHTTP(filePath, callback) {
function getFileViaHTTP(filePath, callback, callBackFunction) {
try {
/* The filePath received is the one that is needed to fetch data from with fs. To do it via http we need to remove the prefix that includes this: ./Data-Storage/ */
@@ -447,36 +447,49 @@ exports.newFileStorage = function newFileStorage(logger, host, port) {
'/Storage/' +
filePath
http.get(url, onResponse);
logger.write(MODULE_NAME, '[INFO] FileStorage -> getFileViaHTTP -> url = ' + url)
let request = http.get(url, onResponse);
request.on('error', function (err) {
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> onError -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> onError -> Failed to fetch file via HTTP. Will retry later. ");
callBackFunction(global.DEFAULT_RETRY_RESPONSE);
})
function onResponse(response) {
const chunks = []
try {
const chunks = []
response.on('data', onMessegesArrived)
response.on('end', onEnd)
response.on('data', onMessegesArrived)
response.on('end', onEnd)
function onMessegesArrived(chunk) {
chunks.push(chunk)
}
function onEnd() {
let fileContent = Buffer.concat(chunks).toString('utf8')
let err = null
if (fileContent.indexOf('does not exist') >= 0) {
err = {
code: "ENOENT" // This is how fs would have returned upon this situation.
}
fileContent = undefined
function onMessegesArrived(chunk) {
chunks.push(chunk)
}
callback(err, fileContent)
function onEnd() {
let fileContent = Buffer.concat(chunks).toString('utf8')
let err = null
if (fileContent.indexOf('does not exist') >= 0) {
err = {
code: "ENOENT" // This is how fs would have returned upon this situation.
}
fileContent = undefined
}
callback(err, fileContent)
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> onResponse -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> onResponse -> Failed to fetch file via HTTP. Will retry later. ");
callBackFunction(global.DEFAULT_RETRY_RESPONSE);
}
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
abort = true
logger.write(MODULE_NAME, "[ERROR] getFileViaHTTP -> Failed to fetch file via HTTP. Will retry later. ");
callBackFunction(global.DEFAULT_RETRY_RESPONSE);
}
}
}
@@ -116,7 +116,11 @@
/* For each loop we want to create a new log file. */
if (logger !== undefined) {
logger.finalize()
}
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set(MODULE_NAME, logger)
logger.bot = bot;
logger.initialize();
+1 -1
View File
@@ -124,7 +124,7 @@
thisReport = statusReport.file;
if (thisReport.lastFile === undefined) {
logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> reportKey = " + reportKey);
logger.write(MODULE_NAME, "[WARN] start -> getContextVariables -> Undefined Last File. -> thisReport = " + JSON.stringify(thisReport));
let customOK = {
result: global.CUSTOM_OK_RESPONSE.result,
+3
View File
@@ -391,6 +391,7 @@
let logger;
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set('runSensorBot', logger)
logger.bot = pBotConfig;
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runSensorBot -> Entering function."); }
@@ -455,6 +456,7 @@
let logger;
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set('runIndicatorBot', logger)
logger.bot = pBotConfig;
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runIndicatorBot -> Entering function."); }
@@ -517,6 +519,7 @@
let logger;
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set('runTradingBot', logger)
logger.bot = pBotConfig;
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> bootingBot -> runTradingBot -> Entering function."); }
+4
View File
@@ -121,7 +121,11 @@
/* For each loop we want to create a new log file. */
if (logger !== undefined) {
logger.finalize()
}
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set(MODULE_NAME, logger)
logger.bot = bot;
logger.initialize();
+1
View File
@@ -39,6 +39,7 @@
/* Listen to event to start or stop the session. */
bot.sessionKey = bot.processNode.session.name + '-' + bot.processNode.session.type + '-' + bot.processNode.session.id
global.SESSION_MAP.set(bot.sessionKey, bot.sessionKey)
global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, runSession)
global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, stopSession)
+4 -1
View File
@@ -124,8 +124,11 @@
}
/* For each loop we want to create a new log file. */
if (logger !== undefined) {
logger.finalize()
}
logger = DEBUG_MODULE.newDebugLog();
global.LOGGER_MAP.set(MODULE_NAME, logger)
logger.bot = bot;
logger.initialize();
+29 -2
View File
@@ -26,19 +26,37 @@ global.CUSTOM_FAIL_RESPONSE = {
};
const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000;
global.LOGGER_MAP = new Map()
global.SESSION_MAP = new Map()
process.on('uncaughtException', function (err) {
console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message)
console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack)
process.exit(1)
global.EXIT_NODE_PROCESS()
})
process.on('unhandledRejection', (reason, p) => {
console.log('[ERROR] Task Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason))
console.log('[ERROR] Task Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p))
process.exit(1)
global.EXIT_NODE_PROCESS()
})
function finalizeLoggers() {
global.LOGGER_MAP.forEach(forEachLogger)
function forEachLogger(logger) {
logger.finalize()
}
}
function finalizeSessions() {
global.SESSION_MAP.forEach(forEachSession)
function forEachSession(session) {
global.EVENT_SERVER_CLIENT.raiseEvent(session, 'Stopped')
}
}
process.on('exit', function (code) {
if (global.TASK_NODE !== undefined) {
@@ -69,8 +87,16 @@ process.on('message', message => {
}
});
let shuttingDownProcess = false
global.EXIT_NODE_PROCESS = function exitProcess() {
if (shuttingDownProcess === true) { return }
shuttingDownProcess = true
/* Signal that all sessions are stopping. */
finalizeSessions()
/* Cleaning Before Exiting. */
clearInterval(global.HEARTBEAT_INTERVAL_HANDLER)
@@ -82,6 +108,7 @@ global.EXIT_NODE_PROCESS = function exitProcess() {
global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Stopped') // Meaning Process Stopped
}
finalizeLoggers()
//console.log("[INFO] Task Server -> " + global.TASK_NODE.name + " -> EXIT_NODE_PROCESS -> Task Server will stop in 10 seconds.");
setTimeout(process.exit, 10000) // We will give 10 seconds to logs be written on file