From 65b0dae3666e044fe2b4eecb897eae67af1344bb Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 14 Mar 2018 11:53:52 +0100 Subject: [PATCH] Context and Status Report Implemented --- .vs/AACloudPlatform/v15/.suo | Bin 75776 -> 76288 bytes Context.js | 118 +++++++++-------------------------- Run.js | 5 ++ Status Report.js | 16 +++-- Trading Bot Main Loop.js | 3 +- 5 files changed, 47 insertions(+), 95 deletions(-) diff --git a/.vs/AACloudPlatform/v15/.suo b/.vs/AACloudPlatform/v15/.suo index 8080165c5dab490d827bf2fb6ba414d5f2be79c4..ec981ab4dd838e452ab2b8fdfd1777851952da94 100644 GIT binary patch delta 704 zcmZp;z|wGqWrBes69W_oFfi}{nan`^@Bjb*AW;xoVq@TM#>qZx3akcGAAfF~T*!2p zgAXXK2gH*XoKTo-z{In;g82&*t3%KIuZ^1v*)DNTcIGkMTq5Al$jJaS=|3+*DaYh5 zf>$=55V2s~B;vxwAqbQKIRI)9$K(>R^PG$zJ?u!Ln@i+dSQ!g8|5W+U45BpHE4d5A z7#M_sYPc8{O?F&gIN9zz*W?2mH5eC6K6of|vdkfY$=mm8OkT5L#^kGFnZ(dx~fBzV#ZLb54b_iuJ>lavWFyWXl4v%Tl0W>WeNgO;40$ z^qTzP67S~FOROxDqkf5OF1V(_g6fyeR<{covH9SR7o!+?ZY@J{<0QsKkJu*LpYfco zAjYULIpGTLWWPNkn>)^wF)n(=wE5rJ!;H8+QMB3f(pKh0pE#Iu^(Otw+g$K#g6=Xk GMkWAa{^vFT delta 757 zcmbVJOK4M35PkRMrTMnzL(?pZO)$_%v5g86FpCtsC=qF*C{%PIqM;&*u>~tArW+R` z#&U#;t8$}TllCe_j6n*fC6qQKF`EjCxG3%nS@^>^&r(c9H(nTK?wPsgo|#Ejm1Ie| z>x1uv!D*4C9g-wz$bK}NO>z<=+cy{a#+VLnxknr1Pea#{kSlsBX!cVUv&$~{WV4vm zG-Pe*d*8}6k!UL$46L29^6v`0n6Qj61G5sOw7_UstBSb=5Di<{^ozfv1;vD81c$m# z!`1a}-t}2wG4aLI)Tqv@yK0I#B*{+YMrjt2^cZ$7TJY?J7kB0da3g&Xf!6_q)At#t zY$tzc#HZ({5L`3jCih|^?L;voCb9KeY|LeiZ2PZ)^#ac; z*qiGnAt`H=gl!(r%P9&jiu{`)hQ$xwg;G1~U^wuq*zJ@UO>20olKe3;6WLG}^q)u- zJj`lq$?sBQtXB7vU=v@KFc4()cS5-k}*{o7NN3D zu^R|hSD{W{-8vma>UNQ=IhE}liNJb-{k4w}bNsL7Dzr{pm getStatusReport -> Entering function."); } - let fileName = "Status.Report.json" - let filePath = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + bot.dataSetVersion + "/Processes/" + bot.process + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor; + statusReportFile = STATUS_REPORT.newStatusReport(BOT, DEBUG_MODULE, FILE_STORAGE, UTILITIES); + statusReportFile.initialize(bot, onInitilized); - if (FULL_LOG === true) { logger.write("[INFO] initialize -> getStatusReport -> fileName = " + fileName); } - if (FULL_LOG === true) { logger.write("[INFO] initialize -> getStatusReport -> filePath = " + filePath); } + function onInitilized(err) { - cloudStorage.getTextFile(filePath, fileName, onFileReceived); + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write("[INFO] initialize -> getStatusReport -> Execution finished well. :-)"); + thisObject.statusReport = statusReportFile.getFile(); + getExecutionHistory(callBack); + return; + } + case global.CUSTOM_FAIL_RESPONSE.result: { // We need to see if we can handle this. + logger.write("[ERROR] initialize -> getStatusReport -> err.message = " + err.message); - function onFileReceived(err, text) { - - if (err.result === global.DEFAULT_FAIL_RESPONSE.result && err.message === 'Folder does not exist.') { - logger.write("[INFO] initialize -> getStatusReport -> onFileReceived -> err = " + err.message); - - /* In this case we can assume that this is the first execution ever of this bot.*/ - - createConext(callBack); - return; - } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] initialize -> getStatusReport -> onFileReceived -> err = " + err.message); - callBack(err); - return; - } - - if (FULL_LOG === true) { - logger.write("[INFO] initialize -> getStatusReport -> onFileReceived -> Content received = " + text); - } - - try { - - thisObject.statusReport = JSON.parse(text); - getExecutionHistory(callBack); - - } catch (err) { - - /* - It might happen that the file content is corrupt. We will consider this as a temporary situation, since sometimes the file - is being updated at the moment of the read. The bot can not run without a valid Status Report but we can request the platform to retry later. - */ - - logger.write("[ERROR] initialize -> getStatusReport -> onFileReceived -> Bot cannot execute without a valid Status report. -> Err = " + err.message); - callBack(global.DEFAULT_RETRY_RESPONSE); + if (err.message === "Status Report was never created.") { + createConext(callBack); + } else { + callBackFunction(err); // we cant handle this here. + } + return; + } + default: + { + logger.write("[ERROR] initialize -> getStatusReport -> Operation Failed."); + callBackFunction(err); + return; + } } } @@ -561,55 +547,9 @@ if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeStatusReport -> Entering function."); } - let fileName = "Status.Report.json" - let filePath = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + bot.dataSetVersion + "/Processes/" + bot.process + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor; + thisObject.statusReport.lastExecution = global.processDatetime; + statusReportFile.save(callBack); - if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeStatusReport -> fileName = " + fileName); } - if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeStatusReport -> filePath = " + filePath); } - - utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated); - - function onFolderCreated(err) { - - try { - - if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeStatusReport -> onFolderCreated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] saveThemAll -> writeStatusReport -> onFolderCreated -> err = " + err.message); - callBack(err); - return; - } - - thisObject.statusReport.lastExecution = global.processDatetime; - - let fileContent = JSON.stringify(thisObject.statusReport); - - cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); - - function onFileCreated(err) { - - if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeStatusReport -> onFolderCreated -> onFileCreated -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] saveThemAll -> writeStatusReport -> onFolderCreated -> onFileCreated -> err = " + err.message); - callBack(err); - return; - } - - if (FULL_LOG === true) { - logger.write("[INFO] saveThemAll -> writeStatusReport -> onFolderCreated -> onFileCreated -> Content written = " + fileContent); - } - - callBack(global.DEFAULT_OK_RESPONSE); - return; - } - } - catch (err) { - logger.write("[ERROR] saveThemAll -> writeStatusReport -> onFolderCreated -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } } catch (err) { logger.write("[ERROR] saveThemAll -> writeStatusReport -> err = " + err.message); diff --git a/Run.js b/Run.js index 4f8d1c7db..355834c8a 100644 --- a/Run.js +++ b/Run.js @@ -84,6 +84,11 @@ global.DEFAULT_RETRY_RESPONSE = { message: "Retry Later" }; +global.CUSTOM_FAIL_RESPONSE = { + result: "Fail Because", + message: "Custom Message" +}; + /* Current state of the art fixed parameters */ global.MARKET = { diff --git a/Status Report.js b/Status Report.js index 962032f57..888a91ee5 100644 --- a/Status Report.js +++ b/Status Report.js @@ -129,7 +129,12 @@ /* In this case we can assume that this is the first execution ever of this bot.*/ - createConext(callBack); + let customErr = { + result: global.CUSTOM_FAIL_RESPONSE.result, + message: "Status Report was never created." + } + logger.write("[ERROR] save -> err = " + err.message); + callBackFunction(customErr); return; } @@ -146,7 +151,7 @@ try { thisObject.statusReport = JSON.parse(text); - getExecutionHistory(callBack); + callBack(global.DEFAULT_OK_RESPONSE); } catch (err) { @@ -183,12 +188,13 @@ if (ownerId !== botId) { - let err = { - result: global.DEFAULT_FAIL_RESPONSE.result, + let customErr = { + result: global.CUSTOM_FAIL_RESPONSE.result, message: "Only bots owners of a Status Report can save them." } logger.write("[ERROR] save -> err = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + callBackFunction(customErr); + return; } let fileName = "Status.Report.json" diff --git a/Trading Bot Main Loop.js b/Trading Bot Main Loop.js index e4da9c327..72309ce4b 100644 --- a/Trading Bot Main Loop.js +++ b/Trading Bot Main Loop.js @@ -60,6 +60,7 @@ const CONTEXT = require(ROOT_DIR + 'Context'); const DATASOURCE = require(ROOT_DIR + 'Datasource'); const ASSISTANT = require(ROOT_DIR + 'Assistant'); + const STATUS_REPORT = require(ROOT_DIR + 'Status Report'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ @@ -82,7 +83,7 @@ if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeContext -> Entering function."); } - context = CONTEXT.newContext(bot, DEBUG_MODULE, AZURE_FILE_STORAGE, UTILITIES); + context = CONTEXT.newContext(bot, DEBUG_MODULE, AZURE_FILE_STORAGE, UTILITIES, STATUS_REPORT); context.initialize(onInizialized); function onInizialized(err) {