diff --git a/CloudRequire.js b/CloudRequire.js new file mode 100644 index 000000000..882d8a002 --- /dev/null +++ b/CloudRequire.js @@ -0,0 +1,102 @@ +exports.newCloudRequire = function newCloudRequire(BOT, DEBUG_MODULE) { + + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + + const MODULE_NAME = "Cloud Require"; + + let bot = BOT; + + const logger = DEBUG_MODULE.newDebugLog(); + logger.bot = bot; + logger.fileName = MODULE_NAME; + logger.initialize(); + + let thisObject = { + requireBot: requireBot, + requireCommons: requireCommons + }; + + return thisObject; + + function requireBot(pCloudStorage, pProcessConfig, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] requireBot -> Entering function."); } + + let filePath = bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name; + let fileName = "User.Bot.js"; + + pCloudStorage.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write("[ERROR] requireBot -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + try { + + let USER_BOT_MODULE = {}; + USER_BOT_MODULE.newUserBot = eval(text); + + callBackFunction(global.DEFAULT_OK_RESPONSE, USER_BOT_MODULE); + + } catch (err) { + logger.write("[ERROR] requireBot -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write("[ERROR] requireBot -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function requireCommons(pCloudStorage, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] requireCommons -> Entering function."); } + + let filePath = bot.devTeam + "/" + bot.repo; + let fileName = "Commons.js"; + + pCloudStorage.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + // Nothing happens since COMMONS modules are optional. + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + try { + + let COMMONS_MODULE = {}; + COMMONS_MODULE.newCommons = eval(text); + + callBackFunction(global.DEFAULT_OK_RESPONSE, COMMONS_MODULE); + + } catch (err) { + logger.write("[ERROR] requireCommons -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } catch (err) { + logger.write("[ERROR] requireCommons -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + +}; \ No newline at end of file diff --git a/DebugLog.js b/DebugLog.js index 36298f9c9..bbda943a9 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -63,7 +63,7 @@ if (loopCounter === undefined) { loopCounter = 0 }; - filePath = filePath + "/Loop." + loopCounter; + filePath = filePath + "/Loop." + pad(loopCounter, 5); cloudStorage.createTextFile(filePath, thisObject.fileName + ".json", blobContent + '\n' + "]", onFileCreated); diff --git a/Project.njsproj b/Project.njsproj index 01728ebda..e598fe529 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -26,6 +26,9 @@ Code + + Code + Code diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index dadcfc9d4..f8986c38f 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -47,62 +47,40 @@ if (err.result === global.DEFAULT_OK_RESPONSE.result) { - let filePath = bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name; - let fileName = "User.Bot.js"; + /* We needed the cloudStorage initialized for both requesting the bot source code and, later requesting the AACloud condig at ShallWeStop function. */ - cloudStorage.getTextFile(filePath, fileName, onFileReceived); + const CLOUD_REQUIRE = require(ROOT_DIR + 'CloudRequire'); + let cloudRequire = CLOUD_REQUIRE.newCloudRequire(bot, DEBUG_MODULE); - function onFileReceived(err, text) { + cloudRequire.requireBot(cloudStorage, pProcessConfig, onBotRequired); + + function onBotRequired(err, pMODULE) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); + logger.write("[ERROR] initialize -> onInizialized -> onBotRequired -> err.message = " + err.message); callBackFunction(global.DEFAULT_FAIL_RESPONSE); bot.eventHandler.raiseEvent("Loop Finished"); return; } - try { + USER_BOT_MODULE = pMODULE; - USER_BOT_MODULE = {}; - USER_BOT_MODULE.newUserBot = eval(text); + cloudRequire.requireCommons(cloudStorage, onCommonsRequired); - filePath = bot.devTeam + "/" + bot.repo; - fileName = "Commons.js"; + function onCommonsRequired(err, pMODULE) { - cloudStorage.getTextFile(filePath, fileName, onFileReceived); + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - function onFileReceived(err, text) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - // Nothing happens since COMMONS modules are optional. - callBackFunction(global.DEFAULT_OK_RESPONSE); - return; - } - - try { - - COMMONS_MODULE = {}; - COMMONS_MODULE.newCommons = eval(text); - - filePath = bot.devTeam + "/" + bot.repo; - fileName = "Commons.js"; - - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } catch (err) { - logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> onFileReceived -> err.message = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - bot.eventHandler.raiseEvent("Loop Finished"); - return; - } + logger.write("[ERROR] initialize -> onInizialized -> onBotRequired -> onCommonsRequired -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + return; } - } catch (err) { - logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - bot.eventHandler.raiseEvent("Loop Finished"); - return; + + COMMONS_MODULE = pMODULE; + + callBackFunction(global.DEFAULT_OK_RESPONSE); } }