From db99134e10c96b4aca5cc5000c270a40dbdf2001 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 18 Sep 2019 20:05:19 +0200 Subject: [PATCH] server module completelly cleaned up --- MultiPeriod.js | 2 +- TradingEngineProcessMainLoop.js | 4 +- server.js | 98 ++++++++------------------------- 3 files changed, 26 insertions(+), 78 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index 646254c90..6a6d44f2e 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -506,7 +506,7 @@ bot.multiPeriodDailyProcessDatetime = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() + ONE_DAY_IN_MILISECONDS); previousDay = new Date(bot.multiPeriodDailyProcessDatetime.valueOf() - ONE_DAY_IN_MILISECONDS); - console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " " + " bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime.toISOString()); + console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + " " + "bot.multiPeriodDailyProcessDatetime = " + bot.multiPeriodDailyProcessDatetime.toISOString()); if (global.WRITE_LOGS_TO_FILES === 'true') { logger.newInternalLoop(bot.codeName, bot.process); diff --git a/TradingEngineProcessMainLoop.js b/TradingEngineProcessMainLoop.js index d065b14c3..d1cd78698 100644 --- a/TradingEngineProcessMainLoop.js +++ b/TradingEngineProcessMainLoop.js @@ -278,7 +278,9 @@ /* Checking if we should process this loop or not.*/ if (global.STOP_PROCESSING === true) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> We are going to skip this Loop bacause we were requested to stop or never asked to start."); } - console.log("[INFO] run -> loop -> We are going to skip this Loop bacause we were requested to stop or never asked to start.") + + console.log(new Date().toISOString() + " " + pad(bot.codeName, 20) + " " + pad(bot.process, 30) + + " We are going to skip this Loop bacause we were requested to stop or never asked to start. "); global.SYSTEM_EVENT_HANDLER.raiseEvent('Jason-Multi-Period', 'Process Stopped') diff --git a/server.js b/server.js index 2af751565..7d63eab48 100644 --- a/server.js +++ b/server.js @@ -43,11 +43,6 @@ process.on('exit', function (code) { console.log('[INFO] Task Server -> server -> process.on.exit -> About to exit -> code = ' + code) }) -/* Local Variables */ - -let sequenceList = [] -let heartBeatInterval - /* We read the first string sent as an argument when the process was created by the Task Manager. Ther we will find the information of the identity @@ -82,7 +77,7 @@ global.WRITE_LOGS_TO_FILES = process.env.WRITE_LOGS_TO_FILES /* Default parameters can be changed by the execution configuration */ global.EXCHANGE_NAME = 'Poloniex' global.MARKET = { assetA: 'USDT', assetB: 'BTC' } -global.CLONE_EXECUTOR = { codeName: 'AACloud', version: '1.1' } +global.CLONE_EXECUTOR = { codeName: 'AACloud', version: '1.1' } // NOTE: To refactor the name of this variable you would need to go through the bots code that are using it. /* We need to count how many process instances we deployd and how many of them have already finished their job, either @@ -142,39 +137,10 @@ const EVENT_HANDLER_MODULE = require('./SystemEventHandler.js'); const IPC = require('node-ipc'); global.SYSTEM_EVENT_HANDLER = EVENT_HANDLER_MODULE.newSystemEventHandler(IPC) global.SYSTEM_EVENT_HANDLER.initialize('Task Server', bootLoader) - +global.STOP_TASK_GRACEFULLY = false; function bootLoader() { - for (let i = 0; i < global.TASK_NODE.bot.processes.length; i++) { - let code = global.TASK_NODE.bot.processes[i].code - - /* Validate that the minimun amount of parameters required are defined. */ - - if (global.TASK_NODE.bot.processes[i].code.bot === undefined) { - console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'bot' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[i].name); - continue - } - - if (global.TASK_NODE.bot.processes[i].code.team === undefined) { - console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'team' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[i].name); - continue - } - - if (global.TASK_NODE.bot.processes[i].code.process === undefined) { - console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'process' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[i].name); - continue - } - - /* Add to the execution sequence list. */ - sequenceList.push(code) - - /* Create the event handler for each process. This event handlers are where the status reports updated events are raised. */ - - let key = code.team + "-" + code.bot + "-" + code.process - global.SYSTEM_EVENT_HANDLER.createEventHandler(key) - } - /* Heartbeat sent to the UI */ let key = global.TASK_NODE.name + '-' + global.TASK_NODE.type + '-' + global.TASK_NODE.id @@ -191,57 +157,37 @@ function bootLoader() { global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Heartbeat', event) } - startSequence() -} - -/* Old Run.js code follows... */ - -function startSequence() { - for (let processIndex = 0; processIndex < global.TASK_NODE.bot.processes.length; processIndex++) { - let execution = sequenceList[processIndex]; + let code = global.TASK_NODE.bot.processes[processIndex].code - global.STOP_TASK_GRACEFULLY = false; + /* Validate that the minimun amount of parameters required are defined. */ - execution.bot ? process.env.BOT = execution.bot : undefined; - execution.resumeExecution = true; - - readExecutionConfiguration(execution, processIndex); - } -} - -async function readExecutionConfiguration(execution, processIndex) { - try { - console.log("[INFO] Task Server -> server -> readExecutionConfiguration -> Entering function. "); - - let botProcess - - if (global.TASK_NODE.bot.type === 'Trading Engine') { - - /* The Trading Engine only resumes its execution after the first sequence was completed. */ - if (notFirstSequence === false) { - execution.resumeExecution = false - } + if (global.TASK_NODE.bot.processes[processIndex].code.bot === undefined) { + console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'bot' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[processIndex].name); + continue } + if (global.TASK_NODE.bot.processes[processIndex].code.team === undefined) { + console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'team' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[processIndex].name); + continue + } + + if (global.TASK_NODE.bot.processes[processIndex].code.process === undefined) { + console.log("[INFO] Task Server -> server -> bootLoader -> Parameter 'process' is undefined. This process will not be executed. -> Process = " + global.TASK_NODE.bot.processes[processIndex].name); + continue + } + + /* Create the event handler for each process. This event handlers are where the status reports updated events are raised. */ + + let key = code.team + "-" + code.bot + "-" + code.process + global.SYSTEM_EVENT_HANDLER.createEventHandler(key) + if (global.TASK_NODE.bot.processes[processIndex].code.repo === undefined) { global.TASK_NODE.bot.processes[processIndex].code.repo = global.TASK_NODE.bot.processes[processIndex].code.bot + "-" + global.TASK_NODE.bot.type + "-Bot" } - if (botProcess === undefined) { botProcess = process.env.PROCESS } // Only use the .env when nothing comes at Definition.json - - global.CLONE_EXECUTOR = { - codeName: 'AACloud', - version: '1.1' - } - startRoot(processIndex); } - - catch (err) { - console.log("[ERROR] readExecutionConfiguration -> err = " + err.stack); - console.log("[ERROR] readExecutionConfiguration -> Please verify that the Start Mode for the type of Bot configured applies to that type."); - } } function startRoot(processIndex) {