|
@ -6,6 +6,7 @@ const electron = require('electron'), |
|
|
os = require('os'), |
|
|
os = require('os'), |
|
|
fsnode = require('fs'), |
|
|
fsnode = require('fs'), |
|
|
fs = require('fs-extra'), |
|
|
fs = require('fs-extra'), |
|
|
|
|
|
_fs = require('graceful-fs'), |
|
|
mkdirp = require('mkdirp'), |
|
|
mkdirp = require('mkdirp'), |
|
|
express = require('express'), |
|
|
express = require('express'), |
|
|
exec = require('child_process').exec, |
|
|
exec = require('child_process').exec, |
|
@ -172,15 +173,42 @@ shepherd.get('/cache-all', function(req, res, next) { |
|
|
if (total / Object.keys(callStack).length === 1) { |
|
|
if (total / Object.keys(callStack).length === 1) { |
|
|
cacheCallInProgress = false; |
|
|
cacheCallInProgress = false; |
|
|
} |
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
checkTimestamp = function(dateToCheck) { |
|
|
|
|
|
var currentEpochTime = new Date(Date.now()) / 1000, |
|
|
|
|
|
secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); |
|
|
|
|
|
|
|
|
|
|
|
return Math.floor(secondsElapsed); |
|
|
|
|
|
}, |
|
|
|
|
|
internalError = false; |
|
|
|
|
|
|
|
|
|
|
|
if (!sessionKey) { |
|
|
|
|
|
var errorObj = { |
|
|
|
|
|
'msg': 'error', |
|
|
|
|
|
'result': 'no session key provided' |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
|
|
internalError = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!pubkey) { |
|
|
|
|
|
var errorObj = { |
|
|
|
|
|
'msg': 'error', |
|
|
|
|
|
'result': 'no pubkey provided' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
|
|
internalError = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!internalError) { |
|
|
|
|
|
console.log('cache-all call started'); |
|
|
res.end(JSON.stringify({ |
|
|
res.end(JSON.stringify({ |
|
|
'msg': 'success', |
|
|
'msg': 'success', |
|
|
'result': 'call is initiated' |
|
|
'result': 'call is initiated' |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
console.log('cache-all call started'); |
|
|
|
|
|
|
|
|
|
|
|
request({ |
|
|
request({ |
|
|
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, |
|
|
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, |
|
|
method: 'GET' |
|
|
method: 'GET' |
|
@ -225,12 +253,21 @@ shepherd.get('/cache-all', function(req, res, next) { |
|
|
writeCache(); |
|
|
writeCache(); |
|
|
|
|
|
|
|
|
async.forEachOf(dexUrls, function(dexUrl, key) { |
|
|
async.forEachOf(dexUrls, function(dexUrl, key) { |
|
|
|
|
|
var tooEarly = false; |
|
|
|
|
|
if (outObj.basilisk[coin][address][key] && |
|
|
|
|
|
outObj.basilisk[coin][address][key].timestamp && |
|
|
|
|
|
checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { |
|
|
|
|
|
tooEarly = true; |
|
|
|
|
|
} |
|
|
|
|
|
if (!tooEarly) { |
|
|
request({ |
|
|
request({ |
|
|
url: dexUrl, |
|
|
url: dexUrl, |
|
|
method: 'GET' |
|
|
method: 'GET' |
|
|
}, function (error, response, body) { |
|
|
}, function (error, response, body) { |
|
|
if (response && response.statusCode && response.statusCode === 200) { |
|
|
if (response && response.statusCode && response.statusCode === 200) { |
|
|
outObj.basilisk[coin][address][key] = JSON.parse(body); |
|
|
outObj.basilisk[coin][address][key] = {}; |
|
|
|
|
|
outObj.basilisk[coin][address][key].data = JSON.parse(body); |
|
|
|
|
|
outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp
|
|
|
console.log(dexUrl); |
|
|
console.log(dexUrl); |
|
|
console.log(body); |
|
|
console.log(body); |
|
|
callStack[coin]--; |
|
|
callStack[coin]--; |
|
@ -240,6 +277,12 @@ shepherd.get('/cache-all', function(req, res, next) { |
|
|
writeCache(); |
|
|
writeCache(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); |
|
|
|
|
|
callStack[coin]--; |
|
|
|
|
|
console.log(coin + ' _stack len ' + callStack[coin]); |
|
|
|
|
|
checkCallStack(); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
} else { |
|
@ -254,6 +297,9 @@ shepherd.get('/cache-all', function(req, res, next) { |
|
|
// TODO: error
|
|
|
// TODO: error
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
cacheCallInProgress = false; |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
res.end(JSON.stringify({ |
|
|
res.end(JSON.stringify({ |
|
|
'msg': 'error', |
|
|
'msg': 'error', |
|
@ -315,11 +361,32 @@ shepherd.get('/cache-one', function(req, res, next) { |
|
|
secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); |
|
|
secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); |
|
|
|
|
|
|
|
|
return Math.floor(secondsElapsed); |
|
|
return Math.floor(secondsElapsed); |
|
|
}; |
|
|
}, |
|
|
|
|
|
internalError = false; |
|
|
|
|
|
|
|
|
callStack[coin] = 1; |
|
|
callStack[coin] = 1; |
|
|
console.log(callsArray); |
|
|
console.log(callsArray); |
|
|
|
|
|
|
|
|
|
|
|
if (!sessionKey) { |
|
|
|
|
|
var errorObj = { |
|
|
|
|
|
'msg': 'error', |
|
|
|
|
|
'result': 'no session key provided' |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
|
|
internalError = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!pubkey) { |
|
|
|
|
|
var errorObj = { |
|
|
|
|
|
'msg': 'error', |
|
|
|
|
|
'result': 'no pubkey provided' |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
|
|
internalError = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
console.log('cache-one call started'); |
|
|
console.log('cache-one call started'); |
|
|
|
|
|
|
|
|
if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { |
|
|
if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { |
|
@ -346,6 +413,7 @@ shepherd.get('/cache-one', function(req, res, next) { |
|
|
'result': 'call is initiated' |
|
|
'result': 'call is initiated' |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
if (!internalError) { |
|
|
// update all available coin addresses
|
|
|
// update all available coin addresses
|
|
|
if (!address) { |
|
|
if (!address) { |
|
|
request({ |
|
|
request({ |
|
@ -384,7 +452,9 @@ shepherd.get('/cache-one', function(req, res, next) { |
|
|
|
|
|
|
|
|
async.forEachOf(_dexUrls, function(dexUrl, key) { |
|
|
async.forEachOf(_dexUrls, function(dexUrl, key) { |
|
|
var tooEarly = false; |
|
|
var tooEarly = false; |
|
|
if (outObj.basilisk[coin][address][key] && outObj.basilisk[coin][address][key].timestamp && checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { |
|
|
if (outObj.basilisk[coin][address][key] && |
|
|
|
|
|
outObj.basilisk[coin][address][key].timestamp && |
|
|
|
|
|
checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { |
|
|
tooEarly = true; |
|
|
tooEarly = true; |
|
|
} |
|
|
} |
|
|
if (!tooEarly) { |
|
|
if (!tooEarly) { |
|
@ -446,7 +516,9 @@ shepherd.get('/cache-one', function(req, res, next) { |
|
|
|
|
|
|
|
|
async.forEachOf(_dexUrls, function(dexUrl, key) { |
|
|
async.forEachOf(_dexUrls, function(dexUrl, key) { |
|
|
var tooEarly = false; |
|
|
var tooEarly = false; |
|
|
if (outObj.basilisk[coin][address][key] && outObj.basilisk[coin][address][key].timestamp && checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { |
|
|
if (outObj.basilisk[coin][address][key] && |
|
|
|
|
|
outObj.basilisk[coin][address][key].timestamp && |
|
|
|
|
|
checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { |
|
|
tooEarly = true; |
|
|
tooEarly = true; |
|
|
} |
|
|
} |
|
|
if (!tooEarly) { |
|
|
if (!tooEarly) { |
|
@ -475,6 +547,9 @@ shepherd.get('/cache-one', function(req, res, next) { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
cacheCallInProgress = false; |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
res.end(JSON.stringify({ |
|
|
res.end(JSON.stringify({ |
|
|
'msg': 'error', |
|
|
'msg': 'error', |
|
@ -650,7 +725,7 @@ shepherd.readDebugLog = function(fileLocation, lastNLines) { |
|
|
return new Promise( |
|
|
return new Promise( |
|
|
function(resolve, reject) { |
|
|
function(resolve, reject) { |
|
|
if (lastNLines) { |
|
|
if (lastNLines) { |
|
|
fs.access(fileLocation, fs.constants.R_OK, function(err) { |
|
|
_fs.access(fileLocation, fs.constants.R_OK, function(err) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
console.log('error reading ' + fileLocation); |
|
|
console.log('error reading ' + fileLocation); |
|
|
reject('readDebugLog error: ' + err); |
|
|
reject('readDebugLog error: ' + err); |
|
|