From 6f83547b160d1590489f9d44520aa3827179214f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 22 May 2018 12:37:18 +0200 Subject: [PATCH] AACloud Config is 100% read from the cloud --- BlobStorage.js | 18 +-- FileStorage.js | 2 +- Root.js | 209 +++++++++++++++++++++-------------- Run.js | 11 +- TradingBotProcessMainLoop.js | 2 +- this.config.json | 1 - 6 files changed, 145 insertions(+), 98 deletions(-) diff --git a/BlobStorage.js b/BlobStorage.js index 89fdd1169..6efb1d36b 100644 --- a/BlobStorage.js +++ b/BlobStorage.js @@ -12,9 +12,7 @@ exports.newBlobStorage = function newBlobStorage(BOT) { let storage = require('azure-storage'); const DEBUG_MODULE = require('./DebugLog'); - const logger = DEBUG_MODULE.newDebugLog(); - logger.fileName = MODULE_NAME; - logger.bot = bot; + let logger; let thisObject = { @@ -27,7 +25,7 @@ exports.newBlobStorage = function newBlobStorage(BOT) { let readOnlyBlobService; let writeOnlyBlobService; let containerName; - let environment = global.STORAGE_CONN_STRING_FOLDER; + let environment = global.CURRENT_ENVIRONMENT; return thisObject; @@ -35,24 +33,26 @@ exports.newBlobStorage = function newBlobStorage(BOT) { try { + containerName = pDataOwner.toLowerCase(); + if (disableLogging === true) { FULL_LOG = false; LOG_FILE_CONTENT = false; } else { + logger = DEBUG_MODULE.newDebugLog(); + logger.fileName = MODULE_NAME; + logger.bot = bot; logger.initialize(); + + logger.fileName = MODULE_NAME + '.' + pDataOwner; } - containerName = pDataOwner.toLowerCase(); - - logger.fileName = MODULE_NAME + '.' + pDataOwner.devTeam + '.' + pDataOwner.bot + '.' + containerName; - if (pDataOwner.environment !== undefined) { // This is use for data migration from one environment to the other. environment = pDataOwner.environment; - logger.fileName = MODULE_NAME + '.' + pDataOwner.devTeam + '.' + pDataOwner.bot + '.' + containerName; } if (FULL_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } diff --git a/FileStorage.js b/FileStorage.js index 59c89959c..2157e982c 100644 --- a/FileStorage.js +++ b/FileStorage.js @@ -78,7 +78,7 @@ exports.newFileStorage = function newFileStorage(BOT) { try { let fs = require('fs'); - filePath = '../' + 'Connection-Strings' + '/' + global.STORAGE_CONN_STRING_FOLDER + '/' + dataOwner + '.azure.storage.connstring'; + filePath = '../' + 'Connection-Strings' + '/' + global.CURRENT_ENVIRONMENT + '/' + dataOwner + '.azure.storage.connstring'; let connObj = JSON.parse(fs.readFileSync(filePath, 'utf8')); callBack(global.DEFAULT_OK_RESPONSE, connObj); diff --git a/Root.js b/Root.js index 5cc5e7cbe..ce17ca751 100644 --- a/Root.js +++ b/Root.js @@ -2,6 +2,84 @@ exports.newRoot = function newRoot() { + /* Global constants definitions. */ + + /* Callbacks default responses. */ + + global.DEFAULT_OK_RESPONSE = { + result: "Ok", + message: "Operation Succeeded" + }; + + global.DEFAULT_FAIL_RESPONSE = { + result: "Fail", + message: "Operation Failed" + }; + + global.DEFAULT_RETRY_RESPONSE = { + result: "Retry", + message: "Retry Later" + }; + + global.CUSTOM_OK_RESPONSE = { + result: "Ok, but check Message", + message: "Custom Message" + }; + + global.CUSTOM_FAIL_RESPONSE = { + result: "Fail Because", + message: "Custom Message" + }; + + /* Current state of the art fixed parameters */ + + global.MARKET = { + assetA: "USDT", + assetB: "BTC", + }; + + /* Currently only one exchange is supported. */ + + global.EXCHANGE_NAME = "Poloniex"; + + /* This is the Execution Datetime */ + + global.EXECUTION_DATETIME = new Date(); + + /* Time Periods Definitions. */ + + global.marketFilesPeriods = + '[' + + '[' + 24 * 60 * 60 * 1000 + ',' + '"24-hs"' + ']' + ',' + + '[' + 12 * 60 * 60 * 1000 + ',' + '"12-hs"' + ']' + ',' + + '[' + 8 * 60 * 60 * 1000 + ',' + '"08-hs"' + ']' + ',' + + '[' + 6 * 60 * 60 * 1000 + ',' + '"06-hs"' + ']' + ',' + + '[' + 4 * 60 * 60 * 1000 + ',' + '"04-hs"' + ']' + ',' + + '[' + 3 * 60 * 60 * 1000 + ',' + '"03-hs"' + ']' + ',' + + '[' + 2 * 60 * 60 * 1000 + ',' + '"02-hs"' + ']' + ',' + + '[' + 1 * 60 * 60 * 1000 + ',' + '"01-hs"' + ']' + ']'; + + global.marketFilesPeriods = JSON.parse(global.marketFilesPeriods); + + global.dailyFilePeriods = + '[' + + '[' + 45 * 60 * 1000 + ',' + '"45-min"' + ']' + ',' + + '[' + 40 * 60 * 1000 + ',' + '"40-min"' + ']' + ',' + + '[' + 30 * 60 * 1000 + ',' + '"30-min"' + ']' + ',' + + '[' + 20 * 60 * 1000 + ',' + '"20-min"' + ']' + ',' + + '[' + 15 * 60 * 1000 + ',' + '"15-min"' + ']' + ',' + + '[' + 10 * 60 * 1000 + ',' + '"10-min"' + ']' + ',' + + '[' + 05 * 60 * 1000 + ',' + '"05-min"' + ']' + ',' + + '[' + 04 * 60 * 1000 + ',' + '"04-min"' + ']' + ',' + + '[' + 03 * 60 * 1000 + ',' + '"03-min"' + ']' + ',' + + '[' + 02 * 60 * 1000 + ',' + '"02-min"' + ']' + ',' + + '[' + 01 * 60 * 1000 + ',' + '"01-min"' + ']' + ']'; + + global.dailyFilePeriods = JSON.parse(global.dailyFilePeriods); + + const ROOT_DIR = './'; + const MODULE_NAME = "Root"; + let thisObject = { initialize: initialize, start: start @@ -9,14 +87,56 @@ exports.newRoot = function newRoot() { const FULL_LOG = true; + let cloudStorage; + return thisObject; - function initialize() { + function initialize(callBackFunction) { + try { + console.log("[INFO] Root -> initialize -> Entering function. "); - } + const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage'); + cloudStorage = BLOB_STORAGE.newBlobStorage(); + + cloudStorage.initialize("AAPlatform", onInizialized, true); + + function onInizialized(err) { + + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + + let filePath = "AdvancedAlgos" + "/" + "AACloud"; + let fileName = "this.config.json"; + + cloudStorage.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + console.log("[ERROR] Root -> initialize -> onInizialized -> onFileReceived -> err = " + err.message); + return; + } + + try { + global.PLATFORM_CONFIG = JSON.parse(text); + callBackFunction(); + } catch (err) { + console.log("[ERROR] Root -> initialize -> onInizialized -> onFileReceived -> err = " + err.message); + return; + } + } + + } else { + console.log("[ERROR] Root -> initialize -> onInizialized -> err = " + err.message); + return; + } + } + } + catch (err) { + console.log("[ERROR] Root -> initialize -> err.message = " + err.message); + return; + } - function start() { /* First thing to do is to read the config and guess which bot we will be running. */ @@ -35,93 +155,14 @@ exports.newRoot = function newRoot() { return; } + } - /* The following global variable tells the system if it is running on test mode or production. */ - - global.STORAGE_CONN_STRING_FOLDER = global.PLATFORM_CONFIG.storageConnStringFolder; // 'Testnet', 'Mixed' or 'Production', or whatever folder name the conn strings files are into. + function start() { /* Now we will run according to what we see at the config file. */ - const ROOT_DIR = './'; - const MODULE_NAME = "Root"; - const DEBUG_MODULE = require(ROOT_DIR + 'DebugLog'); - /* Global constants definitions. */ - - /* Callbacks default responses. */ - - global.DEFAULT_OK_RESPONSE = { - result: "Ok", - message: "Operation Succeeded" - }; - - global.DEFAULT_FAIL_RESPONSE = { - result: "Fail", - message: "Operation Failed" - }; - - global.DEFAULT_RETRY_RESPONSE = { - result: "Retry", - message: "Retry Later" - }; - - global.CUSTOM_OK_RESPONSE = { - result: "Ok, but check Message", - message: "Custom Message" - }; - - global.CUSTOM_FAIL_RESPONSE = { - result: "Fail Because", - message: "Custom Message" - }; - - /* Current state of the art fixed parameters */ - - global.MARKET = { - assetA: "USDT", - assetB: "BTC", - }; - - /* Currently only one exchange is supported. */ - - global.EXCHANGE_NAME = "Poloniex"; - - /* This is the Execution Datetime */ - - global.EXECUTION_DATETIME = new Date(); - - /* Time Periods Definitions. */ - - global.marketFilesPeriods = - '[' + - '[' + 24 * 60 * 60 * 1000 + ',' + '"24-hs"' + ']' + ',' + - '[' + 12 * 60 * 60 * 1000 + ',' + '"12-hs"' + ']' + ',' + - '[' + 8 * 60 * 60 * 1000 + ',' + '"08-hs"' + ']' + ',' + - '[' + 6 * 60 * 60 * 1000 + ',' + '"06-hs"' + ']' + ',' + - '[' + 4 * 60 * 60 * 1000 + ',' + '"04-hs"' + ']' + ',' + - '[' + 3 * 60 * 60 * 1000 + ',' + '"03-hs"' + ']' + ',' + - '[' + 2 * 60 * 60 * 1000 + ',' + '"02-hs"' + ']' + ',' + - '[' + 1 * 60 * 60 * 1000 + ',' + '"01-hs"' + ']' + ']'; - - global.marketFilesPeriods = JSON.parse(global.marketFilesPeriods); - - global.dailyFilePeriods = - '[' + - '[' + 45 * 60 * 1000 + ',' + '"45-min"' + ']' + ',' + - '[' + 40 * 60 * 1000 + ',' + '"40-min"' + ']' + ',' + - '[' + 30 * 60 * 1000 + ',' + '"30-min"' + ']' + ',' + - '[' + 20 * 60 * 1000 + ',' + '"20-min"' + ']' + ',' + - '[' + 15 * 60 * 1000 + ',' + '"15-min"' + ']' + ',' + - '[' + 10 * 60 * 1000 + ',' + '"10-min"' + ']' + ',' + - '[' + 05 * 60 * 1000 + ',' + '"05-min"' + ']' + ',' + - '[' + 04 * 60 * 1000 + ',' + '"04-min"' + ']' + ',' + - '[' + 03 * 60 * 1000 + ',' + '"03-min"' + ']' + ',' + - '[' + 02 * 60 * 1000 + ',' + '"02-min"' + ']' + ',' + - '[' + 01 * 60 * 1000 + ',' + '"01-min"' + ']' + ']'; - - global.dailyFilePeriods = JSON.parse(global.dailyFilePeriods); - /* Small Function to fix numbers into strings in a cool way. */ function pad(str, max) { diff --git a/Run.js b/Run.js index cc6b5da4a..0a1bdcc04 100644 --- a/Run.js +++ b/Run.js @@ -1,4 +1,8 @@  + +global.CURRENT_ENVIRONMENT = "Develop"; + + process.on('uncaughtException', function (err) { console.log('[INFO] Run -> uncaughtException -> err.message = ' + err.message); return; @@ -53,8 +57,11 @@ function startRoot() { const ROOT_MODULE = require(ROOT_DIR + 'Root'); let root = ROOT_MODULE.newRoot(); - root.initialize(); - root.start(); + root.initialize(onInitialized); + function onInitialized() { + + root.start(); + } } diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 92ca7332a..b2679a12e 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -39,7 +39,7 @@ const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage'); cloudStorage = BLOB_STORAGE.newBlobStorage(bot); - cloudStorage.initialize("AAPlatform", onInizialized, true); + cloudStorage.initialize("AAPlatform", onInizialized); function onInizialized(err) { diff --git a/this.config.json b/this.config.json index 543740d86..7dce3e9c4 100644 --- a/this.config.json +++ b/this.config.json @@ -15,6 +15,5 @@ } ], "stopGracefully": false, - "storageConnStringFolder": "Develop", "maxLogLoops": 5 } \ No newline at end of file