diff --git a/DebugLog.js b/DebugLog.js index efb51d469..bcaf567a3 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -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++; diff --git a/FileStorage.js b/FileStorage.js index 30b69242e..a220960c5 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -447,6 +447,8 @@ exports.newFileStorage = function newFileStorage(logger, host, port) { '/Storage/' + filePath + logger.write(MODULE_NAME, '[INFO] FileStorage -> getFileViaHTTP -> url = ' + url) + http.get(url, onResponse); function onResponse(response) { diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js index 2c2401a15..90892dd72 100644 --- a/IndicatorBotProcessMainLoop.js +++ b/IndicatorBotProcessMainLoop.js @@ -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(); diff --git a/Root.js b/Root.js index bd1a498aa..3a0dec4cd 100644 --- a/Root.js +++ b/Root.js @@ -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."); } diff --git a/SensorBotProcessMainLoop.js b/SensorBotProcessMainLoop.js index e90924148..558d9ffa4 100644 --- a/SensorBotProcessMainLoop.js +++ b/SensorBotProcessMainLoop.js @@ -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(); diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index ab3bee5ad..579de4f33 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -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(); diff --git a/server.js b/server.js index f32a3bc13..2f08161cd 100644 --- a/server.js +++ b/server.js @@ -26,19 +26,28 @@ global.CUSTOM_FAIL_RESPONSE = { }; const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; +global.LOGGER_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() + } +} + process.on('exit', function (code) { if (global.TASK_NODE !== undefined) { @@ -69,8 +78,13 @@ process.on('message', message => { } }); + +let shuttingDownProcess = false global.EXIT_NODE_PROCESS = function exitProcess() { + if (shuttingDownProcess === true) { return } + shuttingDownProcess = true + /* Cleaning Before Exiting. */ clearInterval(global.HEARTBEAT_INTERVAL_HANDLER) @@ -82,6 +96,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