diff --git a/ExtractionBotProcessMainLoop.js b/ExtractionBotProcessMainLoop.js index 30f370b90..78515510b 100644 --- a/ExtractionBotProcessMainLoop.js +++ b/ExtractionBotProcessMainLoop.js @@ -28,22 +28,89 @@ return thisObject; - function initialize(pBotPath, pProcessConfig, callBackFunction) { + function initialize(pProcessConfig, callBackFunction) { try { if (FULL_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } + processConfig = pProcessConfig; - USER_BOT_MODULE = require(pBotPath + "/" + pProcessConfig.name + "/" + 'User.Bot'); + const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage'); + cloudStorage = BLOB_STORAGE.newBlobStorage(bot); - try { - COMMONS_MODULE = require(pBotPath + "/" + 'Commons'); - } catch (err) { - // Nothing happens since COMMONS modules are optional. + cloudStorage.initialize("AAPlatform", onInizialized); + + function onInizialized(err) { + + if (FULL_LOG === true) { logger.write("[INFO] initialize -> onInizialized -> Entering function."); } + + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + + let filePath = bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name; + let fileName = "User.Bot.js"; + + cloudStorage.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } + + try { + + USER_BOT_MODULE = {}; + USER_BOT_MODULE.newUserBot = eval(text); + + filePath = bot.devTeam + "/" + bot.repo; + fileName = "Commons.js"; + + cloudStorage.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 { + + 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; + } + } + } catch (err) { + logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } + } + + } else { + logger.write("[ERROR] Root -> start -> getBotConfig -> onInizialized -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + } } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } catch (err) { logger.write("[ERROR] initialize -> err = " + err.message); bot.eventHandler.raiseEvent("Loop Finished"); @@ -330,7 +397,9 @@ /* Here we check if we must stop the loop gracefully. */ - if (shallWeStop() === true) { + shallWeStop(onStop, onContinue); + + function onStop() { if (bot.runAtFixedInterval === true) { intervalHandle.clearInterval();} @@ -341,67 +410,99 @@ } - /* Extraction bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + function onContinue() { - switch (nextWaitTime) { - case 'Normal': { - if (bot.runAtFixedInterval === true) { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Normal exit point reached."); } - bot.eventHandler.raiseEvent("Loop Finished"); - return; - } else { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.normalWaitTime); + /* Extraction bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + + switch (nextWaitTime) { + case 'Normal': { + if (bot.runAtFixedInterval === true) { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Normal exit point reached."); } + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } else { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.normalWaitTime); + } } - } - break; - case 'Retry': { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.retryWaitTime); - } - break; - case 'Sleep': { - if (bot.runAtFixedInterval === true) { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Sleep exit point reached."); } + break; + case 'Retry': { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } bot.eventHandler.raiseEvent("Loop Finished"); - return; - } else { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.sleepWaitTime); + setTimeout(loop, processConfig.retryWaitTime); } - } - break; - case 'Coma': { - if (bot.runAtFixedInterval === true) { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Coma exit point reached."); } - bot.eventHandler.raiseEvent("Loop Finished"); - return; - } else { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.comaWaitTime); + break; + case 'Sleep': { + if (bot.runAtFixedInterval === true) { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Sleep exit point reached."); } + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } else { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.sleepWaitTime); + } } + break; + case 'Coma': { + if (bot.runAtFixedInterval === true) { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Fixed Interval Coma exit point reached."); } + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } else { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.comaWaitTime); + } + } + break; } - break; } } - function shallWeStop() { - - var fs = require('fs'); + function shallWeStop(stopCallBack, continueCallBack) { try { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> shallWeStop -> Entering function. "); } - global.PLATFORM_CONFIG = JSON.parse(fs.readFileSync('this.config.json', 'utf8')); - return JSON.parse(global.PLATFORM_CONFIG.stopGracefully); + 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) { + logger.write("[ERROR] run -> loopControl -> shallWeStop -> onFileReceived -> err.message = " + err.message); + bot.eventHandler.raiseEvent("Loop Finished"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + try { + + let configRead = JSON.parse(text); + + if (configRead.stopGracefully === false) { + continueCallBack(); + } else { + stopCallBack(); + } + + } catch (err) { + logger.write("[ERROR] run -> loopControl -> shallWeStop -> onFileReceived -> err.message = " + err.message); + bot.eventHandler.raiseEvent("Loop Finished"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } } catch (err) { - logger.write("[ERROR] run -> shallWeStop -> err = " + err.message); + logger.write("[ERROR] run -> loopControl -> shallWeStop -> err.message = " + err.message); bot.eventHandler.raiseEvent("Loop Finished"); callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; } } } diff --git a/FileStorage.js b/FileStorage.js deleted file mode 100644 index 2157e982c..000000000 --- a/FileStorage.js +++ /dev/null @@ -1,434 +0,0 @@ - -exports.newFileStorage = function newFileStorage(BOT) { - - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - - let bot = BOT; - const ROOT_DIR = './'; - - const MODULE_NAME = "File Storage"; - - var util = require('util'); - var guid = require('node-uuid'); - var crypto = require('crypto'); - var storage = require('azure-storage'); - - const DEBUG_MODULE = require('./DebugLog'); - const logger = DEBUG_MODULE.newDebugLog(); - logger.fileName = MODULE_NAME; - logger.bot = bot; - logger.initialize(); - - const shareName = 'data'; - let dataOwner; - - let thisObject = { - initialize: initialize, - createFolder: createFolder, - createTextFile: createTextFile, - getTextFile: getTextFile, - listFilesAndFolders: listFilesAndFolders - }; - - let fileService; - - return thisObject; - - - function initialize(pDataOwnerBotCodeName, callBackFunction) { - - try { - - if (pDataOwnerBotCodeName === undefined) { - dataOwner = bot.codeName; - } else { - dataOwner = pDataOwnerBotCodeName; - } - - logger.fileName = MODULE_NAME + '.' + dataOwner; - - if (FULL_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } - - readConnectionStringConfigFile(onConnectionStringReady); - - function onConnectionStringReady(err, pConnObj) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] initialize -> onConnectionStringReady -> err = " + err.message); - callBackFunction(err); - return; - } - - try { - fileService = storage.createFileService(pConnObj.connectionString); - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } catch (err) { - - logger.write("[ERROR] initialize -> onConnectionStringReady -> createFileService -> err = " + err.message); - callBackFunction(err); - return; - } - } - - function readConnectionStringConfigFile(callBack) { - - let filePath; - - try { - let fs = require('fs'); - filePath = '../' + 'Connection-Strings' + '/' + global.CURRENT_ENVIRONMENT + '/' + dataOwner + '.azure.storage.connstring'; - let connObj = JSON.parse(fs.readFileSync(filePath, 'utf8')); - - callBack(global.DEFAULT_OK_RESPONSE, connObj); - } - catch (err) { - logger.write("[ERROR] readConnectionStringConfigFile -> err = " + err.message); - logger.write("[HINT] readConnectionStringConfigFile -> You need to have a file at this path -> " + filePath); - logger.write("[HINT] readConnectionStringConfigFile -> The file must have the connection string to the Azure Storage Account. Request the file to an AA Team Member if you dont have it."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - logger.write("[ERROR] initialize -> err = " + err.message); - callBack(global.DEFAULT_FAIL_RESPONSE); - } - } - - function createFolder(pFolderPath, callBackFunction) { - - if (FULL_LOG === true) { logger.write("[INFO] createFolder -> Entering function."); } - - if (fileService === undefined) { - - logger.write("[ERROR] createFolder -> initialize function not executed or failed. Can not process this request. Sorry."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - try { - - fileService.createDirectoryIfNotExists(shareName, pFolderPath, onFolderCreated); - - function onFolderCreated(err, result, response) { - - if (err) { - - logger.write("[ERROR] createFolder -> onFolderCreated -> err = " + JSON.stringify(err)); - logger.write("[ERROR] createFolder -> onFolderCreated -> result = " + JSON.stringify(result)); - logger.write("[ERROR] createFolder -> onFolderCreated -> response = " + JSON.stringify(response)); - - if (err.code === 'ServerBusy' - || err.code === 'ECONNRESET' - || err.code === 'ENOTFOUND' - || err.code === 'ESOCKETTIMEDOUT' - || err.code === 'ETIMEDOUT' - || err.code === 'ECONNREFUSED') { - - setTimeout(secondTry, 1000); - return; - - function secondTry() { - - logger.write("[INFO] createFolder -> onFolderCreated -> secondTry -> Retrying to create the folder."); - - fileService.createDirectoryIfNotExists(shareName, pFolderPath, onSecondTry); - - function onSecondTry(err, result, response) { - - if (err) { - logger.write("[ERROR] createFolder -> onFolderCreated -> secondTry -> Folder not created. Giving Up."); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write("[INFO] createFolder -> onFolderCreated -> secondTry -> Folder succesfully created on second try."); - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } - } - - logger.write("[ERROR] createFolder -> onFolderCreated -> Dont know what to do here. Cancelling operation. "); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - - } else { - if (FULL_LOG === true) { logger.write("[INFO] createFolder -> onFolderCreated -> File Created."); } - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write("[ERROR] createFolder -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function createTextFile(pFolderPath, pFileName, pFileContent, callBackFunction) { - - if (fileService === undefined) { - - logger.write("[ERROR] createTextFile -> initialize function not executed or failed. Can not process this request. Sorry."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - try { - - if (FULL_LOG === true) { - logger.write("[INFO] createTextFile -> About to create a text file."); - logger.write("[INFO] createTextFile -> shareName = " + shareName); - logger.write("[INFO] createTextFile -> pFolderPath = " + pFolderPath); - logger.write("[INFO] createTextFile -> pFileName = " + pFileName); - } - - fileService.createFileFromText(shareName, pFolderPath, pFileName, pFileContent, onFileCreated); - - function onFileCreated(err, result, response) { - - if (FULL_LOG === true) { - logger.write("[INFO] createTextFile -> onFileCreated -> Response from Azure received."); - logger.write("[INFO] createTextFile -> onFileCreated -> err = " + JSON.stringify(err)); - logger.write("[INFO] createTextFile -> onFileCreated -> result = " + JSON.stringify(result)); - logger.write("[INFO] createTextFile -> onFileCreated -> response = " + JSON.stringify(response)); - } - - if (err) { - - logger.write("[ERROR] createTextFile -> onFileCreated -> err = " + JSON.stringify(err)); - logger.write("[ERROR] createTextFile -> onFileCreated -> result = " + JSON.stringify(result)); - logger.write("[ERROR] createTextFile -> onFileCreated -> response = " + JSON.stringify(response)); - - if (err.code === 'ServerBusy' - || err.code === 'ECONNRESET' - || err.code === 'ENOTFOUND' - || err.code === 'ESOCKETTIMEDOUT' - || err.code === 'ETIMEDOUT' - || err.code === 'ECONNREFUSED') { - - setTimeout(secondTry, 1000); - return; - - function secondTry() { - - logger.write("[INFO] createTextFile -> onFileCreated -> secondTry -> Retrying to create the file."); - - fileService.createFileFromText(shareName, pFolderPath, pFileName, pFileContent, onSecondTry); - - function onSecondTry(err, result, response) { - - if (err) { - logger.write("[ERROR] createTextFile -> onFileCreated -> secondTry -> File not created. Giving Up."); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write("[INFO] createTextFile -> onFileCreated -> secondTry -> File succesfully created on second try."); - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } - } - - logger.write("[ERROR] createTextFile -> onFileCreated -> Dont know what to do here. Cancelling operation. "); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - - } else { - if (FULL_LOG === true) { logger.write("[INFO] createTextFile -> onFileCreated -> File Created."); } - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - } - catch (err) { - logger.write("[ERROR] 'createTextFile' -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function getTextFile(pFolderPath, pFileName, callBackFunction) { - - if (fileService === undefined) { - - logger.write("[ERROR] getTextFile -> initialize function not executed or failed. Can not process this request. Sorry."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - try { - - if (FULL_LOG === true) { - logger.write("[INFO] getTextFile -> About to get a text file."); - logger.write("[INFO] getTextFile -> shareName = " + shareName); - logger.write("[INFO] getTextFile -> pFolderPath = " + pFolderPath); - logger.write("[INFO] getTextFile -> pFileName = " + pFileName); - } - - fileService.getFileToText(shareName, pFolderPath, pFileName, undefined, onFileReceived); - - function onFileReceived(err, text, response) { - - if (FULL_LOG === true) { - logger.write("[INFO] getTextFile -> onFileReceived -> Response from Azure received."); - logger.write("[INFO] getTextFile -> onFileReceived -> err = " + JSON.stringify(err)); - logger.write("[INFO] getTextFile -> onFileReceived -> response = " + JSON.stringify(response)); - } - - if (LOG_FILE_CONTENT === true) { - logger.write("[INFO] getTextFile -> onFileReceived -> text = " + text); - } - - if (err) { - - if (err.code === 'ServerBusy' - || err.code === 'ECONNRESET' - || err.code === 'ENOTFOUND' - || err.code === 'ESOCKETTIMEDOUT' - || err.code === 'ETIMEDOUT' - || err.code === 'ECONNREFUSED') { - - setTimeout(secondTry, 1000); - return; - - function secondTry() { - - logger.write("[INFO] getTextFile -> onFileReceived -> secondTry -> Retrying to get the file."); - - fileService.getFileToText(shareName, pFolderPath, pFileName, undefined, onSecondTry); - - function onSecondTry(err, text, response) { - - if (err) { - logger.write("[ERROR] getTextFile -> onFileReceived -> secondTry -> File not retrieved. Giving Up."); - callBackFunction(global.DEFAULT_RETRY_RESPONSE); - } else { - logger.write("[INFO] getTextFile -> onFileReceived -> secondTry -> File succesfully retrieved on second try."); - callBackFunction(global.DEFAULT_OK_RESPONSE, text); - } - } - } - } - - if (err.code === "ResourceNotFound") { - - /* This is how Azure tell us the file does not exist. */ - - let customErr = { - result: global.CUSTOM_FAIL_RESPONSE.result, - message: "File does not exist." - }; - - logger.write("[ERROR] getTextFile -> onFileReceived -> Custom Response -> message = " + customErr.message); - - callBackFunction(customErr); - return; - - } - - if (err.code === "ParentNotFound") { - - /* This is how Azure tell us the folder where the file was supposed to be does not exist. We map this to our own response. */ - - let customErr = { - result: global.CUSTOM_FAIL_RESPONSE.result, - message: "Folder does not exist." - }; - - logger.write("[ERROR] getTextFile -> onFileReceived -> Custom Response -> message = " + customErr.message); - - callBackFunction(customErr); - return; - - } - - logger.write("[ERROR] getTextFile -> onFileReceived -> Dont know what to do here. Cancelling operation. "); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - - } else { - if (FULL_LOG === true) { logger.write("[INFO] getTextFile -> onFileReceived -> File retrieved."); } - callBackFunction(global.DEFAULT_OK_RESPONSE, text); - } - } - } - catch (err) { - logger.write("[ERROR] 'getTextFile' -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function listFilesAndFolders(pFolderPath, callBackFunction) { - - if (fileService === undefined) { - - logger.write("[ERROR] listFilesAndFolders -> initialize function not executed or failed. Can not process this request. Sorry."); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - try { - - let items = { - files: [], - folders: [] - }; - - /* - * AZURE HELP: - * - * @param { object } [options] The request options. - * @param { int } [options.maxResults] Specifies the maximum number of folders to return per call to Azure ServiceClient. - * This does NOT affect list size returned by this function. (maximum: 5000) - * @param { LocationMode } [options.locationMode] Specifies the location mode used to decide which location the request should be sent to. - * Please see StorageUtilities.LocationMode for the possible values. - * @param { int } [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request. - * @param { int } [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request. - * The maximum execution time interval begins at the time that the client begins building the request.The maximum - * execution time is checked intermittently while performing requests, and before executing retries. - * @param { string } [options.clientRequestId] A string that represents the client request ID with a 1KB character limit. - * @param { bool } [options.useNagleAlgorithm] Determines whether the Nagle al - */ - - let options = { - maxResults: 500 - }; - - if (FULL_LOG === true) { - logger.write("[INFO] listFilesAndFolders -> About to get the list of files and folders."); - logger.write("[INFO] listFilesAndFolders -> shareName = " + shareName); - logger.write("[INFO] listFilesAndFolders -> pFolderPath = " + pFolderPath); - } - - fileService.listFilesAndDirectoriesSegmented(shareName, pFolderPath, null, options, onFilesAndFoldersReceived); - - function onFilesAndFoldersReceived(err, result) { - - try { - if (FULL_LOG === true) { - logger.write("[INFO] listFilesAndFolders -> onFileReceived -> Response from Azure received."); - logger.write("[INFO] listFilesAndFolders -> onFileReceived -> shareName = " + shareName); - logger.write("[INFO] listFilesAndFolders -> onFileReceived -> pFolderPath = " + pFolderPath); - } - - if (err) { - - logger.write("[ERROR] 'listFilesAndFolders' -> onFilesAndFoldersReceived -> err = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - - } else { - - items.files.push.apply(items.files, result.entries.files); - items.folders.push.apply(items.folders, result.entries.directories); - - callBackFunction(global.DEFAULT_OK_RESPONSE, items); - } - } - catch (err) { - const logText = "[ERROR] 'listFilesAndFolders - onFilesAndFoldersReceived' - ERROR : " + err.message; - logger.write(logText); - } - } - } - catch (err) { - logger.write("[ERROR] 'listFilesAndFolders' -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } -}; \ No newline at end of file diff --git a/IndicatorBotProcessMainLoop.js b/IndicatorBotProcessMainLoop.js index 5b3502059..a423ddac2 100644 --- a/IndicatorBotProcessMainLoop.js +++ b/IndicatorBotProcessMainLoop.js @@ -28,22 +28,89 @@ return thisObject; - function initialize(pBotPath, pProcessConfig, callBackFunction) { + function initialize(pProcessConfig, callBackFunction) { try { if (FULL_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } + processConfig = pProcessConfig; - USER_BOT_MODULE = require(pBotPath + "/" + pProcessConfig.name + "/" + 'User.Bot'); + const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage'); + cloudStorage = BLOB_STORAGE.newBlobStorage(bot); - try { - COMMONS_MODULE = require(pBotPath + "/" + 'Commons'); - } catch (err) { - // Nothing happens since COMMONS modules are optional. + cloudStorage.initialize("AAPlatform", onInizialized); + + function onInizialized(err) { + + if (FULL_LOG === true) { logger.write("[INFO] initialize -> onInizialized -> Entering function."); } + + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + + let filePath = bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name; + let fileName = "User.Bot.js"; + + cloudStorage.getTextFile(filePath, fileName, onFileReceived); + + function onFileReceived(err, text) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } + + try { + + USER_BOT_MODULE = {}; + USER_BOT_MODULE.newUserBot = eval(text); + + filePath = bot.devTeam + "/" + bot.repo; + fileName = "Commons.js"; + + cloudStorage.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 { + + 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; + } + } + } catch (err) { + logger.write("[ERROR] initialize -> onInizialized -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + return; + } + } + + } else { + logger.write("[ERROR] Root -> start -> getBotConfig -> onInizialized -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + bot.eventHandler.raiseEvent("Loop Finished"); + } } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } catch (err) { logger.write("[ERROR] initialize -> err = " + err.message); bot.eventHandler.raiseEvent("Loop Finished"); @@ -321,7 +388,9 @@ /* Here we check if we must stop the loop gracefully. */ - if (shallWeStop() === true) { + shallWeStop(onStop, onContinue); + + function onStop() { if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Stopping the Loop Gracefully. See you next time! :-)"); } @@ -331,49 +400,81 @@ } - /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + function onContinue() { - switch (nextWaitTime) { - case 'Normal': { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.normalWaitTime); + /* Indicator bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */ + + switch (nextWaitTime) { + case 'Normal': { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.normalWaitTime / 1000) + " seconds."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.normalWaitTime); + } + break; + case 'Retry': { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.retryWaitTime); + } + break; + case 'Sleep': { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.sleepWaitTime); + } + break; + case 'Coma': { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } + bot.eventHandler.raiseEvent("Loop Finished"); + setTimeout(loop, processConfig.comaWaitTime); + } + break; } - break; - case 'Retry': { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.retryWaitTime / 1000) + " seconds."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.retryWaitTime); - } - break; - case 'Sleep': { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.sleepWaitTime / 60000) + " minutes."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.sleepWaitTime); - } - break; - case 'Coma': { - if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> Restarting Loop in " + (processConfig.comaWaitTime / 3600000) + " hours."); } - bot.eventHandler.raiseEvent("Loop Finished"); - setTimeout(loop, processConfig.comaWaitTime); - } - break; } } - function shallWeStop() { - - var fs = require('fs'); + function shallWeStop(stopCallBack, continueCallBack) { try { + if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> shallWeStop -> Entering function. "); } - global.PLATFORM_CONFIG = JSON.parse(fs.readFileSync('this.config.json', 'utf8')); - return JSON.parse(global.PLATFORM_CONFIG.stopGracefully); + 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) { + logger.write("[ERROR] run -> loopControl -> shallWeStop -> onFileReceived -> err.message = " + err.message); + bot.eventHandler.raiseEvent("Loop Finished"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + try { + + let configRead = JSON.parse(text); + + if (configRead.stopGracefully === false) { + continueCallBack(); + } else { + stopCallBack(); + } + + } catch (err) { + logger.write("[ERROR] run -> loopControl -> shallWeStop -> onFileReceived -> err.message = " + err.message); + bot.eventHandler.raiseEvent("Loop Finished"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } } catch (err) { - logger.write("[ERROR] run -> shallWeStop -> err = " + err.message); + logger.write("[ERROR] run -> loopControl -> shallWeStop -> err.message = " + err.message); bot.eventHandler.raiseEvent("Loop Finished"); callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; } } } diff --git a/Project.njsproj b/Project.njsproj index 266478bdf..01728ebda 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -45,7 +45,6 @@ Code - Code