From 00e9ee45e311293861120f13e52d55d326da7a8e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 25 Sep 2020 20:20:29 +0200 Subject: [PATCH] Session Syncro --- LowFrequencyTrading/TradingProcess.js | 4 +++- Session.js | 29 ++++++++++++++++++++------- TradingBot.js | 10 ++++----- server.js | 12 +++++++++++ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/LowFrequencyTrading/TradingProcess.js b/LowFrequencyTrading/TradingProcess.js index b6298e6f0..5a37df7c6 100644 --- a/LowFrequencyTrading/TradingProcess.js +++ b/LowFrequencyTrading/TradingProcess.js @@ -146,6 +146,8 @@ } else { /* We are processing Daily Files */ do { + global.EMIT_SESSION_STATUS (bot.SESSION_STATUS, bot.sessionKey) + if (checkStopTaskGracefully() === false) { break } if (checkStopProcessing() === false) { break } @@ -401,7 +403,7 @@ async function processDailyFiles() { /* Telling the world we are alive and doing well and which date we are processing right now. */ let processingDateString = tradingProcessDate.getUTCFullYear() + '-' + utilities.pad(tradingProcessDate.getUTCMonth() + 1, 2) + '-' + utilities.pad(tradingProcessDate.getUTCDate(), 2); - bot.processHeartBeat(processingDateString) + bot.processHeartBeat(processingDateString, undefined, "Running...") /* We will iterate through all posible timeFrames. diff --git a/Session.js b/Session.js index 64b4e0c30..926fbcc09 100644 --- a/Session.js +++ b/Session.js @@ -38,8 +38,9 @@ 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, onSessionRan) - global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, onSessionStopped) + global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Session Status', undefined, bot.sessionKey, undefined, onSessionStatus) + global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, onSessionRun) + global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, onSessionStop) /* Connect this here so that it is accesible from other places */ bot.sessionError = sessionError @@ -52,11 +53,25 @@ callBackFunction(global.DEFAULT_OK_RESPONSE) return - function onSessionRan(message) { + function onSessionStatus() { + if (bot.SESSION_STATUS === 'Running') { + let event = { + status: 'Session Runnning' + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Status Response', event) + } else { + let event = { + status: 'Not Session Runnning' + } + global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Status Response', event) + } + } + + function onSessionRun(message) { try { /* This happens when the UI is reloaded, the session was running and tries to run it again. */ if (bot.SESSION_STATUS === 'Idle' || bot.SESSION_STATUS === 'Running') { - parentLogger.write(MODULE_NAME, "[WARN] onSessionRan -> Event receive to run the Session while it was already running. ") + parentLogger.write(MODULE_NAME, "[WARN] onSessionRun -> Event receive to run the Session while it was already running. ") return } @@ -100,16 +115,16 @@ bot.STOP_SESSION = false } else { bot.STOP_SESSION = true - if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, '[IMPORTANT] onSessionRan -> Stopping the Session now. ') } + if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, '[IMPORTANT] onSessionRun -> Stopping the Session now. ') } } socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.") } catch (err) { - parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onSessionRan -> err = " + err.stack); + parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onSessionRun -> err = " + err.stack); } } - function onSessionStopped() { + function onSessionStop() { stopSession('Session Stopped From the User Interface.') } diff --git a/TradingBot.js b/TradingBot.js index e8c03c194..9d4d48ee7 100644 --- a/TradingBot.js +++ b/TradingBot.js @@ -74,7 +74,7 @@ let nextWaitTime; /* We tell the UI that we are running. */ - processHeartBeat() + processHeartBeat(undefined, undefined, "Running...") /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ const UTILITIES = require(global.ROOT_DIR + 'CloudUtilities'); @@ -94,14 +94,14 @@ /* Checking if we need to need to emit any event */ if (bot.SESSION_STATUS === 'Idle' && bot.STOP_SESSION === false) { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Running') bot.SESSION_STATUS = 'Running' } if (bot.SESSION_STATUS === 'Running' && bot.STOP_SESSION === true) { - global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Stopped') bot.SESSION_STATUS = 'Stopped' - } + } + + global.EMIT_SESSION_STATUS (bot.SESSION_STATUS, bot.sessionKey) /* Checking if we should process this loop or not.*/ if (bot.STOP_SESSION === true) { @@ -523,7 +523,7 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> loopControl -> nextWaitTime = " + nextWaitTime); } /* We show we reached the end of the loop. */ - processHeartBeat() + processHeartBeat(undefined, undefined, "Running...") /* Here we check if we must stop the loop gracefully. */ shallWeStop(onStop, onContinue); diff --git a/server.js b/server.js index 754888678..3ff27cf3f 100644 --- a/server.js +++ b/server.js @@ -285,6 +285,18 @@ global.FILTER_OUT_NODES_WITHOUT_REFERENCE_PARENT_FROM_NODE_ARRAY = function (nod return filteredNodeArray } +global.EMIT_SESSION_STATUS = function (status, key) { + switch(status) { + case 'Running': { + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Running') + break + } + case 'Stopped': { + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Stopped') + break + } + } +} process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message)