diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index af97aebab..547360851 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -343,6 +343,12 @@ "propertyName": "timeConstants", "functionName": "newSuperalgosGlobalsTimeConstants", "fileName": "TimeConstants.js" + }, + { + "name": "Process Variables", + "propertyName": "processVariables", + "functionName": "newSuperalgosGlobalsProcessVariables", + "fileName": "ProcessVariables.js" } ], "functionLibraries": [ diff --git a/Projects/Superalgos/TS/Function-Libraries/NodeJSFunctions.js b/Projects/Superalgos/TS/Function-Libraries/NodeJSFunctions.js index ee4887ceb..e04ada915 100644 --- a/Projects/Superalgos/TS/Function-Libraries/NodeJSFunctions.js +++ b/Projects/Superalgos/TS/Function-Libraries/NodeJSFunctions.js @@ -4,7 +4,7 @@ exports.newSuperalgosFunctionLibrariesNodeJSFunctions = function () { exitProcess: exitProcess } - let isNodeJsProcessShuttingDown = false + let isNodeJsProcessShuttingDown = false return thisObject function exitProcess() { @@ -13,7 +13,7 @@ exports.newSuperalgosFunctionLibrariesNodeJSFunctions = function () { TS.projects.superalgos.functionLibraries.taskFunctions.taskError(undefined, "An unexpected error caused the Task to stop.") } - if (isNodeJsProcessShuttingDown=== true) { return } + if (isNodeJsProcessShuttingDown === true) { return } isNodeJsProcessShuttingDown = true /* Signal that all sessions are stopping. */ @@ -32,9 +32,18 @@ exports.newSuperalgosFunctionLibrariesNodeJSFunctions = function () { } } - global.FINALIZE_LOGGERS() - //console.log("[INFO] Task Server -> " + global.TASK_NODE.name + " -> EXIT_NODE_PROCESS -> Task Server will stop in 10 seconds."); + finalizeLoggers() setTimeout(process.exit, 10000) // We will give 10 seconds to logs be written on file + + function finalizeLoggers() { + global.LOGGER_MAP.forEach(forEachLogger) + + function forEachLogger(logger) { + if (logger !== undefined) { + logger.finalize() + } + } + } } } \ No newline at end of file diff --git a/Projects/Superalgos/TS/Globals/ProcessVariables.js b/Projects/Superalgos/TS/Globals/ProcessVariables.js new file mode 100644 index 000000000..ebaad4620 --- /dev/null +++ b/Projects/Superalgos/TS/Globals/ProcessVariables.js @@ -0,0 +1,13 @@ +exports.newSuperalgosGlobalsProcessVariables = function () { + /* + We need to count how many process instances we deployd and how many of them have already finished their job, either + because they just finished or because there was a request to stop the proceses. In this way, once we reach the + amount of instances started, we can safelly destroy the rest of the objects running and let this nodejs process die. + */ + let thisObject = { + ENDED_PROCESSES_COUNTER: 0, + TOTAL_PROCESS_INSTANCES_CREATED: 0 + } + + return thisObject +} \ No newline at end of file diff --git a/TaskServer/Globals.js b/TaskServer/Globals.js index b00c98769..570eda75e 100644 --- a/TaskServer/Globals.js +++ b/TaskServer/Globals.js @@ -16,26 +16,6 @@ exports.newGlobals = function () { global.LOGGER_MAP = new Map() // We will put all the loggers in a map, so that we can eventually finalize them. global.SESSION_MAP = new Map() // We will put all the sessions in a map, so that we can eventually finalize them. - global.FINALIZE_LOGGERS = function () { - global.LOGGER_MAP.forEach(forEachLogger) - - function forEachLogger(logger) { - if (logger !== undefined) { - logger.finalize() - } - } - } - - /* - We need to count how many process instances we deployd and how many of them have already finished their job, either - because they just finished or because there was a request to stop the proceses. In this way, once we reach the - amount of instances started, we can safelly destroy the rest of the objects running and let this nodejs process die. - */ - global.ENDED_PROCESSES_COUNTER = 0 - global.TOTAL_PROCESS_INSTANCES_CREATED = 0 - global.STOP_TASK_GRACEFULLY = false; - - } } \ No newline at end of file diff --git a/TaskServer/ProcessInstance.js b/TaskServer/ProcessInstance.js index 5b68f1ef2..c0403b5d0 100644 --- a/TaskServer/ProcessInstance.js +++ b/TaskServer/ProcessInstance.js @@ -212,7 +212,7 @@ function runSensorBot() { try { - global.TOTAL_PROCESS_INSTANCES_CREATED++ + TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED++ global.LOGGER_MAP.set('runSensorBot', logger) logger.bot = botConfig; @@ -228,7 +228,7 @@ function runIndicatorBot() { try { - global.TOTAL_PROCESS_INSTANCES_CREATED++ + TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED++ global.LOGGER_MAP.set('runIndicatorBot', logger) logger.bot = botConfig; @@ -245,7 +245,7 @@ function runTradingBot() { - global.TOTAL_PROCESS_INSTANCES_CREATED++ + TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED++ try { global.LOGGER_MAP.set('runTradingBot', logger) @@ -262,7 +262,7 @@ function runLearningBot() { - global.TOTAL_PROCESS_INSTANCES_CREATED++ + TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED++ try { global.LOGGER_MAP.set('runLearningBot', logger) @@ -317,11 +317,9 @@ } function exitProcessInstance() { + TS.projects.superalgos.globals.processVariables.ENDED_PROCESSES_COUNTER++ - global.ENDED_PROCESSES_COUNTER++ - //console.log("[INFO] Task Server -> " + global.TASK_NODE.name + " -> exitProcessInstance -> Process #" + global.ENDED_PROCESSES_COUNTER + " from " + global.TOTAL_PROCESS_INSTANCES_CREATED + " exiting."); - - if (global.ENDED_PROCESSES_COUNTER === global.TOTAL_PROCESS_INSTANCES_CREATED) { + if (TS.projects.superalgos.globals.processVariables.ENDED_PROCESSES_COUNTER === TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED) { TS.projects.superalgos.functionLibraries.nodeJSFunctions.exitProcess() } }