From 3c3aa19caaf8dd1bc82071a086e0991637403286 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 8 Sep 2019 10:33:19 +0200 Subject: [PATCH] Working with Charly --- MultiPeriod.js | 2 +- SensorBotProcessMainLoop.js | 10 +++ sequence.json | 5 +- server.js | 137 +++++++++++++++++++++++------------- 4 files changed, 102 insertions(+), 52 deletions(-) diff --git a/MultiPeriod.js b/MultiPeriod.js index ab5ede991..56cd5480e 100644 --- a/MultiPeriod.js +++ b/MultiPeriod.js @@ -986,7 +986,7 @@ bot.hasTheBotJustStarted = false; /* Emit event that signals that this process finished */ - global.systemEventHandler.raiseEvent('Jason-Multi-Period', 'Status Report Updated', { lastProcessedDay: lastFileDate }) + global.SYSTEM_EVENT_HANDLER.raiseEvent('Jason-Multi-Period', 'Status Report Updated', { lastProcessedDay: lastFileDate }) } catch (err) { diff --git a/SensorBotProcessMainLoop.js b/SensorBotProcessMainLoop.js index 73c03c2ed..0173d8611 100644 --- a/SensorBotProcessMainLoop.js +++ b/SensorBotProcessMainLoop.js @@ -122,6 +122,16 @@ if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> Entering function."); } + /* Loop Heartbeat sent to the UI */ + //hearBeat() + function hearBeat() { + let key = global.USER_DEFINITION.name + '-' + global.USER_DEFINITION.type + let event = { + seconds: (new Date()).getSeconds() + } + global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Heartbeat', event) + } + /* We define here all the modules that the rest of the infraestructure, including the bots themselves can consume. */ const UTILITIES = require(ROOT_DIR + 'CloudUtilities'); diff --git a/sequence.json b/sequence.json index 0ae6028e3..c0db3b13f 100644 --- a/sequence.json +++ b/sequence.json @@ -6,10 +6,7 @@ "resumeExecution": true, "type": "Sensor", "exchangeName": "Poloniex", - "process": "Live-Trades", - "startYear": 2019, - "endYear": 2021, - "month": 1 + "process": "Live-Trades" }, { "devTeam": "AAMasters", diff --git a/server.js b/server.js index 6958dcdf1..7126cb938 100644 --- a/server.js +++ b/server.js @@ -25,30 +25,28 @@ global.CUSTOM_FAIL_RESPONSE = { message: "Custom Message" }; -console.log('NEW INSTANCE OF BACKEND SEVER RUNNING') +/* Process Events */ -const EVENT_HANDLER_MODULE = require('../Libraries/SystemEventsClient/SystemEventHandler.js'); -global.systemEventHandler = EVENT_HANDLER_MODULE.newSystemEventHandler() +process.on('uncaughtException', function (err) { + console.log('[INFO] server -> uncaughtException -> err.message = ' + err.message) + console.log('[INFO] server -> uncaughtException -> err.stack = ' + err.stack) + process.exit(1) +}) -global.systemEventHandler.initialize('Clone Executor', bootLoader) +process.on('unhandledRejection', (reason, p) => { + console.log('[INFO] server -> unhandledRejection -> reason = ' + JSON.stringify(reason)) + console.log('[INFO] server -> unhandledRejection -> p = ' + JSON.stringify(p)) + process.exit(1) +}) -function bootLoader() { +process.on('exit', function (code) { + console.log('[INFO] server -> process.on.exit -> About to exit -> code = ' + code) +}) - global.systemEventHandler.createEventHandler('Jason-Multi-Period') +/* Local Variables */ - tryToListenToCockpit() - - function tryToListenToCockpit() { - - global.systemEventHandler.listenToEvent('Cockpit-Restart-Simulation', 'Simulation Started', undefined, 'Clone Executor Boot Loader', onResponse, startSequence) - - function onResponse(message) { - if (message.result !== global.DEFAULT_OK_RESPONSE.result) { - setTimeout(tryToListenToCockpit, 3000) - } - } - } -} +let sequenceList = [] +let heartBeatInterval /* @@ -56,17 +54,71 @@ We read the first string sent as an argument when the process was created by the of this backend process and know exactly what to run within this server instance. */ +global.USER_DEFINITION = process.argv[2] -let backendProcessDefinition = process.argv[2] -if (backendProcessDefinition !== undefined) { - console.log('RECIBI ESTA DEFINICION: ' + backendProcessDefinition) - backendProcessDefinition = JSON.parse(backendProcessDefinition) -} else { - /* Debugging Definition goes here */ - backendProcessDefinition = { +if (global.USER_DEFINITION !== undefined) { + console.log('[INFO] server -> global.USER_DEFINITION = ' + global.USER_DEFINITION) + try { + global.USER_DEFINITION = JSON.parse(global.USER_DEFINITION) + } catch (err) { + console.log('[ERROR] server -> global.USER_DEFINITION -> ' + err.stack) + } +} +/* +else { + let argument = '{"type":"Backend Process","name":"New Backend Process","bot":{"type":"Sensor","botProcesses":[{"type":"Bot Process","name":"New Bot Process","code":{"devTeam":"AAMasters","bot":"AACharly","mode":"noTime","resumeExecution":true,"type":"Sensor","exchangeName":"Poloniex","process":"Live-Trades"}}]}}' + try { + global.USER_DEFINITION = JSON.parse(argument) + } catch (err) { + console.log(err.stack) } } +*/ + +const EVENT_HANDLER_MODULE = require('../Libraries/SystemEventsClient/SystemEventHandler.js'); +global.SYSTEM_EVENT_HANDLER = EVENT_HANDLER_MODULE.newSystemEventHandler() + +global.SYSTEM_EVENT_HANDLER.initialize('Clone Executor', bootLoader) + +function bootLoader() { + + global.SYSTEM_EVENT_HANDLER.createEventHandler(global.USER_DEFINITION.name) + + /* + tryToListenToCockpit() + + function tryToListenToCockpit() { + + global.SYSTEM_EVENT_HANDLER.listenToEvent('Cockpit-Restart-Simulation', 'Simulation Started', undefined, 'Clone Executor Boot Loader', onResponse, startSequence) + + function onResponse(message) { + if (message.result !== global.DEFAULT_OK_RESPONSE.result) { + setTimeout(tryToListenToCockpit, 3000) + } + } + } + */ + + for (let i = 0; i < global.USER_DEFINITION.bot.botProcesses.length; i++) { + let code = global.USER_DEFINITION.bot.botProcesses[i].code + sequenceList.push(code) + } + + /* Heartbeat sent to the UI */ + heartBeatInterval = setInterval(hearBeat, 1000) + + function hearBeat() { + let key = global.USER_DEFINITION.name + '-' + global.USER_DEFINITION.type + let event = { + seconds: (new Date()).getSeconds() + } + global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Heartbeat', event) + } + + startSequence() +} + /* Old Run.js code follows... */ @@ -85,23 +137,10 @@ global.EXCHANGE_NAME = process.env.EXCHANGE_NAME global.MARKET = { assetA: 'USDT', assetB: 'BTC' } global.CLONE_EXECUTOR = { codeName: 'AACloud', version: '1.1' } -process.on('uncaughtException', function (err) { - console.log('[INFO] Run -> uncaughtException -> err.message = ' + err.message) - console.log('[INFO] Run -> uncaughtException -> err.stack = ' + err.stack) - process.exit(1) -}) -process.on('unhandledRejection', (reason, p) => { - console.log('[INFO] Run -> unhandledRejection -> reason = ' + JSON.stringify(reason)) - console.log('[INFO] Run -> unhandledRejection -> p = ' + JSON.stringify(p)) - process.exit(1) -}) -process.on('exit', function (code) { - console.log('[INFO] Run -> process.on.exit -> About to exit -> code = ' + code) -}) -let sequenceList = require('./sequence'); + let isRunSequence = false; let sequenceStep = 0; let processedSteps = new Map(); @@ -117,8 +156,12 @@ function startSequence () { process.on('message', message => { if (message === 'STOP') { runClonExecutor = false; - global.systemEventHandler.finalize() - global.systemEventHandler = undefined + + /* Cleaning Before Exiting. */ + clearInterval(heartBeatInterval) + + global.SYSTEM_EVENT_HANDLER.finalize() + global.SYSTEM_EVENT_HANDLER = undefined console.log("[INFO] CloneExecutor -> Clone Executor Stopped."); } }); @@ -191,7 +234,7 @@ function onExecutionFinish(result, finishStepKey) { setTimeout(function () { if (runClonExecutor) { console.log("[INFO] onExecutionFinish -> New round for sequence execution started."); - sequenceList = require('./sequence'); // We read again the sequence after every loop + sequenceStep = 0; processedSteps = new Map(); notFirstSequence = true; @@ -204,7 +247,7 @@ function onExecutionFinish(result, finishStepKey) { async function readExecutionConfiguration(execution) { try { - console.log("[INFO] Run -> readExecutionConfiguration -> Entering function. "); + console.log("[INFO] server -> readExecutionConfiguration -> Entering function. "); let timePeriodFilter let botProcess @@ -442,7 +485,7 @@ function getTimePeriod(timePeriod) { timePeriodMap.set('01-min', 60000) return timePeriodMap.get(timePeriod) } catch (error) { - console.log('[WARN] Run -> readExecutionConfiguration -> getTimePeriod -> Error: ', error) + console.log('[WARN] server -> readExecutionConfiguration -> getTimePeriod -> Error: ', error) } } else { return undefined @@ -450,7 +493,7 @@ function getTimePeriod(timePeriod) { } function startRoot() { - console.log('[INFO] Run -> startRoot -> Entering function. ') + console.log('[INFO] server -> startRoot -> Entering function. ') const ROOT_DIR = './' const ROOT_MODULE = require(ROOT_DIR + 'Root') @@ -467,7 +510,7 @@ function startRoot() { root.initialize(UI_COMMANDS, onInitialized) function onInitialized() { - console.log('[INFO] Run -> startRoot -> onInitialized -> Entering function. ') + console.log('[INFO] server -> startRoot -> onInitialized -> Entering function. ') root.start(onExecutionFinish) }