From 7e842b9956c7adc4108aa804fac81bd38466ec31 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 6 Mar 2018 18:17:13 +0100 Subject: [PATCH] Context Initialize fully reviewed --- .vs/AACloudPlatform/v15/.suo | Bin 69632 -> 70144 bytes Context.js | 243 +++++++++++++++++++++-------------- 2 files changed, 145 insertions(+), 98 deletions(-) diff --git a/.vs/AACloudPlatform/v15/.suo b/.vs/AACloudPlatform/v15/.suo index 4d79fddcb6dc2f39b898644753cdfc28dee78472..2837c14195accb1b727362f80849f27b0b346d0b 100644 GIT binary patch delta 500 zcmZozz|ydUWrD$E0~byS0R{#+1_lOZApZCN|9>FGzzD=@8v~`8SRD>kzscKN$n=|; zm7(d%Dj<`sU4b!i^G~(^<{&DL-IO~~gn>aAsDqPX(PYLMg_Gr{8!;{gvWhm>PBCDb zoN!%Xvcim@$u`Gr7tLV9t$K3pl>3VouucAWI(PE>wStpxO}&h)chd?MHb%Gwn^*6M zU|h6;X|mv1*U9=!9Gf33*vvS2(Gr=>GFx6Sqo@mIWzH%#U$l>9a-fDX)MV|=Nqgoo zg2j(8O%9x*vswSm6~;v;n7A`@QWb(r@{1DFQzuV69ke;&)GH>jWKf`hfy99#Agu?) zE?_SL9c?n1?c8BeA&5Mf3;>c!ASIJGPFXg&<#YYyi=}FlH|%$rZoti`vbpa<5#!`{ z3mqrBTvFVebZMR>)B)10Vl&~UNF3M-WLttvA>N{gAmap=u`zN@K3i%sc|o|)d1vkENZ zP|w+1$kwjFn6mk&+JAEp6~}JMog%`(APm&O$*^=X-AlNDwJO+K^KWb%S(oQr0#;n6(#_tce(7O=6oSXw&PE(4n(vxRT6 z=5!6*CT?26!o~=9#pV_3LKzorU|Kw30n_9~OJp|7YneSccF-JGUHO0$&Qy4H>X~jEs1WA Z;%2^6mdwOju-W&sC?m)m!DVcWTmU*-x10a~ diff --git a/Context.js b/Context.js index 026f08d69..fde1ae0ce 100644 --- a/Context.js +++ b/Context.js @@ -1,4 +1,4 @@ -exports.newContext = function newContext(BOT) { +exports.newContext = function newContext(BOT, DEBUG_MODULE, FILE_STORAGE) { /* @@ -31,7 +31,7 @@ thisObject = { statusReport: undefined, // Here is the information that defines which was the last sucessfull execution and some other details. - executionHistory: undefined, // This is the record of bot execution. + executionHistory: [], // This is the record of bot execution. executionContext: undefined, // Here is the business information of the last execution of this bot process. newHistoryRecord : { date: processDatetime, @@ -52,20 +52,19 @@ */ - const EXCHANGE_NAME = "Poloniex"; let bot = BOT; - const DEBUG_MODULE = require('./Debug Log'); const logger = DEBUG_MODULE.newDebugLog(); logger.fileName = MODULE_NAME; /* Storage account to be used here. */ - const FILE_STORAGE = require('./Azure File Storage'); let cloudStorage = FILE_STORAGE.newAzureFileStorage(bot); let processDatetime; + const EXCHANGE_NAME = "Poloniex"; + return thisObject; function initialize(pProcessDatetime, callBackFunction) { @@ -83,145 +82,193 @@ thisObject.newHistoryRecord.date = pProcessDatetime; processDatetime = pProcessDatetime; - getStatusReport(); + getStatusReport(onDone); - function getStatusReport() { - - /* If the process run and was interrupted, there should be a status report that allows us to resume execution. */ - - let fileName = "Status.Report.json" - let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Processes/" + bot.process; - - cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); - - function onFileReceived(text) { - - try { - - thisObject.statusReport = JSON.parse(text); - - if (thisObject.statusReport.lastExecution === undefined) { - - createConext(); - - } else { - - getExecutionHistory(); + function onDone(err) { + try { + switch (err) { + case null: { + callBackFunction(null); } - - } catch (err) { - - /* - - It might happen that the file content is corrupt or it does not exist. The bot can not run without a Status Report, - since it is risky to ignore its own history, so even for first time execution, a status report with the right format - is needed. - - */ - - logger.write("[ERROR] initialize -> getStatusReport -> Bot cannot execute without the Status report. -> Err = " + err.message); - + break; + case 'Retry Later': { // Something bad happened, but if we retry in a while it might go through the next time. + logger.write("[ERROR] initialize -> onDone -> Retry Later. Requesting Execution Retry."); + callBackFunction(err); + return; + } + break; + case 'Retry Later': { // This is an unexpected exception that we do not know how to handle. + logger.write("[ERROR] initialize -> onDone -> Operation Failed. Aborting the process."); + callBackFunction(err); + return; + } + break; } + + } catch (err) { + logger.write("[ERROR] initialize -> onDone -> err = " + err); + callBackFunction("Operation Failed"); } } - function getExecutionHistory() { + function getStatusReport(callBack) { - let fileName = "Execution.History.json" - let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process; + try { + /* If the process run and was interrupted, there should be a status report that allows us to resume execution. */ - cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); + let fileName = "Status.Report.json" + let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Processes/" + bot.process; - function onFileReceived(text) { + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); - try { + function onFileReceived(text) { - thisObject.executionHistory = JSON.parse(text); - getExecutionContext(); + try { - } catch (err) { + thisObject.statusReport = JSON.parse(text); - /* + if (thisObject.statusReport.lastExecution === undefined) { - It might happen that the file content is corrupt or it does not exist. The bot can not run without a Status Report, - since it is risky to ignore its own history, so even for first time execution, a status report with the right format - is needed. + createConext(callBack); - */ + } else { - logger.write("[ERROR] initialize -> getExecutionHistory -> Bot cannot execute without the Execution History. -> Err = " + err.message); + getExecutionHistory(callBack); + } + + } catch (err) { + + /* + + It might happen that the file content is corrupt or it does not exist. The bot can not run without an Execution Context, + since it is risky to ignore its own context, so even for first time execution, a file with the right format + is needed. + + */ + + logger.write("[ERROR] initialize -> getStatusReport -> Bot cannot execute without the Status report. -> Err = " + err.message); + callBack("Operation Failed"); + } } + } catch (err) { + logger.write("[ERROR] initialize -> getExecutionHistory -> err = " + err); + callBack("Operation Failed"); } } - function getExecutionContext() { + function getExecutionHistory(callBack) { - let date = new Date(thisObject.statusReport.lastExecution); + try { + let fileName = "Execution.History.json" + let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process; - let fileName = "Execution.Context.json" - let dateForPath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2) + '/' + utilities.pad(date.getUTCHours(), 2) + '/' + utilities.pad(date.getUTCMinutes(), 2); - let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath; + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); - cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); + function onFileReceived(text) { - function onFileReceived(text) { + try { - try { + thisObject.executionHistory = JSON.parse(text); + getExecutionContext(callBack); - thisObject.executionContext = JSON.parse(text); + } catch (err) { - thisObject.executionContext.transactions = []; // We record here the transactions that happened duting this execution. + /* - ordersExecutionCheck(); + It might happen that the file content is corrupt or it does not exist. The bot can not run without an Execution Hitory, + since it is risky to ignore its own history, so even for first time execution, a file with the right format + is needed. - } catch (err) { - - /* - - It might happen that the file content is corrupt or it does not exist. The bot can not run without a Status Report, - since it is risky to ignore its own history, so even for first time execution, a status report with the right format - is needed. - - */ - - logger.write("[ERROR] initialize -> getExecutionContext -> Bot cannot execute without the Execution Context. -> Err = " + err.message); + */ + logger.write("[ERROR] initialize -> getExecutionHistory -> Bot cannot execute without the Execution History. -> Err = " + err.message); + callBack("Operation Failed"); + } } + } catch (err) { + logger.write("[ERROR] initialize -> getExecutionHistory -> err = " + err); + callBack("Operation Failed"); } } - function createConext() { + function getExecutionContext(callBack) { - /* + try { + let date = new Date(thisObject.statusReport.lastExecution); + + let fileName = "Execution.Context.json" + let dateForPath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2) + '/' + utilities.pad(date.getUTCHours(), 2) + '/' + utilities.pad(date.getUTCMinutes(), 2); + let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath; + + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); + + function onFileReceived(text) { + + try { + + thisObject.executionContext = JSON.parse(text); + + thisObject.executionContext.transactions = []; // We record here the transactions that happened duting this execution. + + callBack(null); + + } catch (err) { + + /* + + It might happen that the file content is corrupt or it does not exist. The bot can not run without a Status Report, + since it is risky to ignore its own history, so even for first time execution, a status report with the right format + is needed. + + */ + + logger.write("[ERROR] initialize -> getExecutionContext -> Bot cannot execute without the Execution Context. -> Err = " + err.message); + callBack("Operation Failed"); + } + } + + } catch (err) { + logger.write("[ERROR] initialize -> getExecutionContext -> err = " + err); + callBack("Operation Failed"); + } + } + + function createConext(callBack) { + + try { + /* - When the bot is executed for the very first time, there are a few files that do not exist and need to be created, and that - is what we are going to do now. + When the bot is executed for the very first time, there are a few files that do not exist and need to be created, and that + is what we are going to do now. - */ + */ - thisObject.executionHistory = []; + thisObject.executionHistory = []; - thisObject.executionContext = { - investment: { - assetA: 0, - assetB: 0 - }, - availableBalance: { - assetA: 0, - assetB: 0 - }, - positions: [], - transactions: [] - }; + thisObject.executionContext = { + investment: { + assetA: 0, + assetB: 0 + }, + availableBalance: { + assetA: 0, + assetB: 0 + }, + positions: [], + transactions: [] + }; - callBackFunction(); + callBack(null); + } catch (err) { + logger.write("[ERROR] initialize -> createConext -> err = " + err); + callBack("Operation Failed"); + } } } catch (err) { - logger.write("[ERROR] initialize -> err = " + err); callBackFunction("Operation Failed"); }