.createTextFile fixed across the solution

This commit is contained in:
Luis Fernando Molina committed 2018-03-07 19:08:16 +01:00
1 parent a1d3910c11
commit 2225a043d1
4 files changed
+29 -261

No files matched your search

Binary file not shown.
-1
View File
@@ -36,7 +36,6 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Interval Executor.js" />
<Compile Include="Markets.js" />
<Compile Include="Poloniex API Client.js" />
<Compile Include="Run.js" />
<Compile Include="TradingBot.js">
+29 -8
View File
@@ -356,13 +356,20 @@
cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated);
function onFileCreated() {
function onFileCreated(err) {
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write("[ERROR] saveThemAll -> writeExucutionHistory -> onFolderCreated -> onFileCreated -> err = " + err.message);
callBack(err);
return;
}
if (LOG_INFO === true) {
logger.write("[INFO] saveThemAll -> writeExecutionContext -> onFolderCreated -> Content written = " + fileContent);
logger.write("[INFO] saveThemAll -> writeExucutionHistory -> onFolderCreated -> onFileCreated -> Content written = " + fileContent);
}
writeExucutionHistory(callBack);
return;
}
}
catch (err) {
@@ -370,7 +377,6 @@
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write("[ERROR] saveThemAll -> writeExecutionContext -> err = " + err.message);
@@ -415,9 +421,20 @@
cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated);
function onFileCreated() {
function onFileCreated(err) {
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write("[ERROR] saveThemAll -> writeExucutionHistory -> onFolderCreated -> onFileCreated -> err = " + err.message);
callBack(err);
return;
}
if (LOG_INFO === true) {
logger.write("[INFO] saveThemAll -> writeExucutionHistory -> onFolderCreated -> onFileCreated -> Content written = " + fileContent);
}
writeStatusReport(callBack);
return;
}
}
catch (err) {
@@ -425,7 +442,6 @@
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write("[ERROR] saveThemAll -> writeExucutionHistory -> err = " + err.message);
@@ -462,10 +478,16 @@
cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated);
function onFileCreated() {
function onFileCreated(err) {
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write("[ERROR] saveThemAll -> writeStatusReport -> onFolderCreated -> onFileCreated -> err = " + err.message);
callBack(err);
return;
}
if (LOG_INFO === true) {
logger.write("[INFO] saveThemAll -> writeStatusReport -> onFolderCreated -> Content written = " + fileContent);
logger.write("[INFO] saveThemAll -> writeStatusReport -> onFolderCreated -> onFileCreated -> Content written = " + fileContent);
}
callBack(global.DEFAULT_OK_RESPONSE);
@@ -477,7 +499,6 @@
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write("[ERROR] saveThemAll -> writeStatusReport -> err = " + err.message);
-252
View File
@@ -1,252 +0,0 @@
/* This business logic module is responsible for Markets and Assets. */
exports.newMarkets = function newMarkets(BOT) {
let bot = BOT;
const ROOT_DIR = '../';
const MODULE_NAME = "Markets";
const FOLDER_PATHS = 'data';
const MARKETS_FOLDER_NAME = "other-files";
const DEBUG_MODULE = require('./Debug Log');
const logger = DEBUG_MODULE.newDebugLog();
logger.fileName = MODULE_NAME;
logger.bot = bot;
const EXCHANGE_ID = 1;
const fileName = "Markets_" + EXCHANGE_ID + ".json";
const DATA_OWNER_BOT_NAME = "Legacy";
const markets = {
DISABLED: 0,
ENABLED: 1,
getMarketsByExchange: getMarketsByExchange,
getExistingOrNewMarketId: getExistingOrNewMarketId,
createNewAsset: createNewAsset,
createNewMarket: createNewMarket,
disableMarket: disableMarket,
initialize: initialize
};
//const dataLayer = DATA_LAYER_MODULE.newDataLayer();
let marketsArray;
const AZURE_FILE_STORAGE = require('./Azure File Storage');
let azureFileStorage = AZURE_FILE_STORAGE.newAzureFileStorage(bot);
return markets;
function initialize(callBackFunction) {
azureFileStorage.initialize(DATA_OWNER_BOT_NAME);
try {
azureFileStorage.getTextFile(MARKETS_FOLDER_NAME, fileName, onFileRead);
function onFileRead(text) {
try {
var fileString = text;
let cleanString = fileString;
/* Dont know why but sometimes files comes with a first characater that is not supposed to be there and you can not see it at the file opened with notepad */
if (fileString[0] !== "[") {
cleanString = '';
for (let i = 1; i < fileString.length; i++) {
cleanString = cleanString + fileString[i];
}
}
marketsArray = JSON.parse(cleanString);
callBackFunction();
}
catch (err) {
const logText = "[ERROR] initialize - onFileRead ' ERROR : " + err.message + " File: " + fileName;
console.log(logText);
logger.write(logText);
}
}
}
catch (err) {
const logText = "[ERROR] initialize - ' ERROR : " + err.message + " File Not Found: " + fileName;
console.log(logText);
logger.write(logText);
}
}
function getMarketsByExchange(exchangeId, callBackFunction) {
try {
// TODO: Currently the exchange Id is ignored as the class is still working with only one exchange. We must fix this.
callBackFunction(JSON.stringify(marketsArray));
} catch (err) {
const logText = "[ERROR] getMarketsByExchange - ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
function getExistingOrNewMarketId(marketName, market, exchangeId, callBackFunction) {
// TODO: At this function is missing the following: If a market does not exists, it must be greated, and Id generated and returned. We automatically discover new markets when this function is called with a market that didn-t previuously existed.
try {
for (let i = 0; i < marketsArray.length; i++) {
if (marketsArray[i][1] + "_" + marketsArray[i][2] === marketName) {
callBackFunction(marketName, market, marketsArray[i][0]);
return;
}
}
} catch (err) {
const logText = "[ERROR] getExistingOrNewMarketId - ' ERROR : " + err.message + " marketId = " + marketId + " idAssetA = " + idAssetA + " idAssetB = " + idAssetB;
console.log(logText);
logger.write(logText);
}
}
function createNewAsset(assetCode, callBackFunction) {
// TODO: Upgrade this function in order to update the file containing all assets, not the database that will be deprecated.
try {
dataLayer.newAsset(assetCode, onIdReady);
function onIdReady(resultSet) {
try {
callBackFunction(resultSet);
resultSet = [];
} catch (err) {
const logText = "[ERROR] createNewAsset - onIdReady ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
} catch (err) {
const logText = "[ERROR] createNewAsset - ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
function createNewMarket(exchangeId, idAssetA, idAssetB, callBackFunction) {
// TODO: Upgrade this function in order to update the file containing all markets, not the database that will be deprecated.
try {
dataLayer.newMarket(exchangeId, idAssetA, idAssetB, onIdReady);
function onIdReady(resultSet) {
try {
callBackFunction(resultSet);
} catch (err) {
const logText = "[ERROR] createNewMarket - onIdReady ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
} catch (err) {
const logText = "[ERROR] createNewMarket - ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
function disableMarket(exchangeId, marketId, callBackFunction) {
// This function puts the requested market in a DISABLED status.
// TODO: Currently the exchangeId is ignored.
try {
for (let i = 0; i < marketsArray.length; i++) {
if (marketsArray[i][0] === marketId) {
marketsArray[i][3] = markets.DISABLED;
break;
}
}
azureFileStorage.createTextFile(MARKETS_FOLDER_NAME, fileName, JSON.stringify(marketsArray), onFileReplaced);
function onFileReplaced() {
try {
callBackFunction();
} catch (err) {
const logText = "[ERROR] disableMarket - onFileReplaced ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
} catch (err) {
const logText = "[ERROR] disableMarket - ' ERROR : " + err.message;
console.log(logText);
logger.write(logText);
}
}
};