Merge pull request #118 from AAVikings/master

Support for local lite execution
This commit is contained in:
benme authored and GitHub committed 2019-07-29 13:59:45 -03:00
commit 4788814630
3 files changed
+165 -90

No files matched your search

+37 -21
View File
@@ -1,13 +1,23 @@
const axios = require('axios')
let fs = require('fs')
const { promisify } = require('util')
const readFileAsync = promisify(fs.readFile);
exports.getEcosystem = async function () {
try {
if (!global.ECOSYSTEM) {
const ecosystemResponse = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
try {
if (!global.ECOSYSTEM) {
let executionOnCloud = false
if (process.env.ON_CLOUD !== undefined) {
executionOnCloud = JSON.parse(process.env.ON_CLOUD)
}
if (executionOnCloud) {
const ecosystemResponse = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
query {
web_GetEcosystem {
id
@@ -52,21 +62,27 @@ exports.getEcosystem = async function () {
}
}
`
},
headers: {
authorization: process.env.AUTHORIZATION
}
})
//TODO Competition hosts not retrieved currently
},
headers: {
authorization: process.env.AUTHORIZATION
}
})
//TODO Competition hosts not retrieved currently
if (ecosystemResponse.data.errors)
throw new Error(ecosystemResponse.data.errors[0].message)
if (ecosystemResponse.data.errors)
throw new Error(ecosystemResponse.data.errors[0].message)
global.ECOSYSTEM = ecosystemResponse.data.data.web_GetEcosystem;
}
return global.ECOSYSTEM
} catch (error) {
throw new Error('There has been an error getting the Ecosystem: ', error)
global.ECOSYSTEM = ecosystemResponse.data.data.web_GetEcosystem
} else {
let fileLocation = process.env.CONFIG_PATH + 'ecosystem.json'
let ecosystem = await readFileAsync(fileLocation, { encoding: 'utf8' })
global.ECOSYSTEM = JSON.parse(ecosystem)
}
}
return global.ECOSYSTEM
} catch (error) {
throw new Error('There has been an error getting the Ecosystem: ', error)
}
}
+100 -51
View File
@@ -1,5 +1,10 @@
const axios = require('axios')
const Ecosystem = require('./Ecosystem')
let fs = require('fs')
const { promisify } = require('util')
const readFileAsync = promisify(fs.readFile)
const writeFileAsync = promisify(fs.writeFile)
const path = require('path')
exports.newFileStorage = function newFileStorage() {
const MODULE_NAME = 'FileStorage'
@@ -40,47 +45,54 @@ exports.newFileStorage = function newFileStorage() {
}
async function getTextFile(container, filePath, callBackFunction) {
try {
logInfo('getTextFile: ' + container.toLowerCase() + '/...' + filePath.substring(filePath.length - 110, filePath.length))
logInfo('getTextFile: ' + container.toLowerCase() + '/...' + filePath.substring(filePath.length - 110, filePath.length))
try {
let host = await getDevTeamHost(container)
let response = await axios({
url: host.url + 'graphql',
method: 'post',
data: {
query: `
query web_FileContent($file: web_FileInput){
web_FileContent(file: $file)
}
`,
variables: {
file: {
container: container.toLowerCase(),
filePath,
storage: host.storage,
accessKey: host.accessKey
if (host.url.indexOf('localhost') !== -1) {
let fileLocation = process.env.STORAGE_PATH + '/' + container + '/' + filePath
let fileContent = await readFileAsync(fileLocation)
callBackFunction(global.DEFAULT_OK_RESPONSE, fileContent.toString())
} else {
let response = await axios({
url: host.url + 'graphql',
method: 'post',
data: {
query: `
query web_FileContent($file: web_FileInput){
web_FileContent(file: $file)
}
`,
variables: {
file: {
container: container.toLowerCase(),
filePath,
storage: host.storage,
accessKey: host.accessKey
}
}
}
})
if (response.data.errors) {
callBackFunction({ code: response.data.errors[0] })
return
}
})
if (response.data.errors) {
callBackFunction({ code: response.data.errors[0] })
return
if (response.data.data.web_FileContent) {
callBackFunction(global.DEFAULT_OK_RESPONSE, response.data.data.web_FileContent)
} else {
callBackFunction({ code: 'The specified key does not exist.' })
}
}
if (response.data.data.web_FileContent) {
callBackFunction(global.DEFAULT_OK_RESPONSE, response.data.data.web_FileContent)
} else {
callBackFunction({ code: 'The specified key does not exist.' })
}
} catch (err) {
if (verifyRetry(err.code) && currentRetryGetTextFile < MAX_RETRY) {
currentRetryGetTextFile++
logInfo('getTextFile -> Retrying connection to the server because received error: ' + err.code + '. Retry #: ' + currentRetryGetTextFile)
getTextFile(container, filePath, callBackFunction)
} else if (err.code === 'ENOENT') {
callBackFunction({ code: 'The specified key does not exist.' })
} else {
currentRetryGetTextFile = 0
logError('getTextFile -> error = ' + err.message)
@@ -90,40 +102,48 @@ exports.newFileStorage = function newFileStorage() {
}
async function createTextFile(container, filePath, fileContent, callBackFunction) {
try {
logInfo('createTextFile -> Entering function: ' + container.toLowerCase() + '/...' + filePath.substring(filePath.length - 110, filePath.length))
logInfo('createTextFile -> Entering function: ' + container.toLowerCase() + '/...' + filePath.substring(filePath.length - 110, filePath.length))
try {
let host = await getDevTeamHost(container)
let response = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
if (host.url.indexOf('localhost') !== -1) {
let fileLocation = process.env.STORAGE_PATH + '/' + container + '/' + filePath
let directoryPath = fileLocation.substring(0, fileLocation.lastIndexOf('/') + 1);
mkDirByPathSync(directoryPath)
await writeFileAsync(fileLocation, fileContent)
callBackFunction(global.DEFAULT_OK_RESPONSE)
} else {
let response = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
mutation web_CreateFile($file: web_FileInput){
web_CreateFile(file: $file)
}
`,
variables: {
file: {
container: container.toLowerCase(),
filePath,
storage: 'localStorage',
accessKey: host.ownerKey,
fileContent
variables: {
file: {
container: container.toLowerCase(),
filePath,
storage: 'localStorage',
accessKey: host.ownerKey,
fileContent
}
}
}
}
})
})
if (!response || response.data.errors) {
let customErr = {
result: global.CUSTOM_FAIL_RESPONSE.result,
message: response.data.errors[0]
if (!response || response.data.errors) {
let customErr = {
result: global.CUSTOM_FAIL_RESPONSE.result,
message: response.data.errors[0]
}
callBackFunction(customErr)
} else {
callBackFunction(global.DEFAULT_OK_RESPONSE)
}
callBackFunction(customErr)
} else {
callBackFunction(global.DEFAULT_OK_RESPONSE)
}
} catch (err) {
@@ -158,4 +178,33 @@ exports.newFileStorage = function newFileStorage() {
}
return false
}
function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : '';
const baseDir = isRelativeToScript ? __dirname : '.';
return targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code === 'EEXIST') { // curDir already exists!
return curDir;
}
// To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
if (err.code === 'ENOENT') { // Throw the original parentDir error on curDir `ENOENT` failure.
throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`);
}
const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1;
if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) {
throw err; // Throw if it's just the last created dir.
}
}
return curDir;
}, initDir);
}
}
+28 -18
View File
@@ -1,38 +1,48 @@
const axios = require('axios')
let fs = require('fs')
const { promisify } = require('util')
const readFileAsync = promisify(fs.readFile);
exports.getStrategy = async function () {
try {
if (!global.STRATEGY) {
let fbSlug = process.env.BOT
if (process.env.TEST_FB !== undefined) {
fbSlug = process.env.TEST_FB
let executionOnCloud = false
if (process.env.ON_CLOUD !== undefined) {
executionOnCloud = JSON.parse(process.env.ON_CLOUD)
}
const strategizerResponse = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
if (executionOnCloud) {
const strategizerResponse = await axios({
url: process.env.GATEWAY_ENDPOINT_K8S,
method: 'post',
data: {
query: `
query($fbSlug: String!){
strategizer_TradingSystemByFb(fbSlug: $fbSlug){
data
}
}
`,
variables: {
fbSlug: fbSlug
variables: {
fbSlug: process.env.BOT
},
},
},
headers: {
authorization: process.env.AUTHORIZATION
}
})
headers: {
authorization: process.env.AUTHORIZATION
}
})
if (strategizerResponse.data.errors)
throw new Error(strategizerResponse.data.errors[0].message)
if (strategizerResponse.data.errors)
throw new Error(strategizerResponse.data.errors[0].message)
global.STRATEGY = strategizerResponse.data.data.strategizer_TradingSystemByFb.data;
global.STRATEGY = strategizerResponse.data.data.strategizer_TradingSystemByFb.data;
} else {
let fileLocation = process.env.CONFIG_PATH + 'userConfig.json'
let strategy = await readFileAsync(fileLocation, { encoding: 'utf8' })
global.STRATEGY = JSON.parse(strategy)
}
}
return global.STRATEGY
} catch (error) {