Browse Source

added timestamp to shepherd cache-all; better req missing vars handling

all-modes
Petr Balashov 8 years ago
parent
commit
c79e82e5bf
  1. 489
      routes/shepherd.js

489
routes/shepherd.js

@ -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,88 +173,133 @@ 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);
res.end(JSON.stringify({ return Math.floor(secondsElapsed);
'msg': 'success', },
'result': 'call is initiated' internalError = false;
}));
console.log('cache-all call started'); if (!sessionKey) {
var errorObj = {
request({ 'msg': 'error',
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, 'result': 'no session key provided'
method: 'GET' };
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
body = JSON.parse(body);
// basilisk coins
if (body.basilisk && body.basilisk.length) {
// get coin addresses
async.each(body.basilisk, function(coin) {
callStack[coin] = 1;
});
async.each(body.basilisk, function(coin) { res.end(JSON.stringify(errorObj));
outObj.basilisk[coin] = {}; internalError = true;
writeCache(); }
request({ if (!pubkey) {
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', var errorObj = {
method: 'GET' 'msg': 'error',
}, function (error, response, body) { 'result': 'no pubkey provided'
if (response && response.statusCode && response.statusCode === 200) { };
outObj.basilisk[coin].addresses = JSON.parse(body).result;
writeCache(); res.end(JSON.stringify(errorObj));
callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3); internalError = true;
console.log(coin + ' stack len ' + callStack[coin]); }
async.each(outObj.basilisk[coin].addresses, function(address) { if (!internalError) {
var dexUrls = { console.log('cache-all call started');
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, res.end(JSON.stringify({
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, 'msg': 'success',
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, 'result': 'call is initiated'
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address }));
};
if (coin === 'BTC' || coin === 'SYS') { request({
delete dexUrls.refresh; url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey,
delete dexUrls.getbalance; method: 'GET'
} }, function (error, response, body) {
//console.log(JSON.stringify(dexUrls)); if (response && response.statusCode && response.statusCode === 200) {
console.log(coin + ' address ' + address); body = JSON.parse(body);
outObj.basilisk[coin][address] = {}; // basilisk coins
writeCache(); if (body.basilisk && body.basilisk.length) {
// get coin addresses
async.forEachOf(dexUrls, function(dexUrl, key) { async.each(body.basilisk, function(coin) {
request({ callStack[coin] = 1;
url: dexUrl, });
method: 'GET'
}, function (error, response, body) { async.each(body.basilisk, function(coin) {
if (response && response.statusCode && response.statusCode === 200) { outObj.basilisk[coin] = {};
outObj.basilisk[coin][address][key] = JSON.parse(body); writeCache();
console.log(dexUrl);
console.log(body); request({
callStack[coin]--; url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
console.log(coin + ' _stack len ' + callStack[coin]); method: 'GET'
checkCallStack(); }, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
writeCache(); outObj.basilisk[coin].addresses = JSON.parse(body).result;
} writeCache();
}); callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3);
}); console.log(coin + ' stack len ' + callStack[coin]);
});
} else { async.each(outObj.basilisk[coin].addresses, function(address) {
// TODO: error var dexUrls = {
} 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
}); 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
}); 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
} else { 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
// TODO: error };
} if (coin === 'BTC' || coin === 'SYS') {
} else { delete dexUrls.refresh;
// TODO: error delete dexUrls.getbalance;
} }
}); //console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
outObj.basilisk[coin][address] = {};
writeCache();
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({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
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 {
// TODO: error
}
});
});
} else {
// TODO: error
}
} else {
// 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,134 +413,142 @@ shepherd.get('/cache-one', function(req, res, next) {
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
// update all available coin addresses if (!internalError) {
if (!address) { // update all available coin addresses
request({ if (!address) {
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', request({
method: 'GET' url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
}, function (error, response, body) { method: 'GET'
if (response && response.statusCode && response.statusCode === 200) { }, function (error, response, body) {
outObj.basilisk[coin].addresses = JSON.parse(body).result; if (response && response.statusCode && response.statusCode === 200) {
console.log(JSON.parse(body).result); outObj.basilisk[coin].addresses = JSON.parse(body).result;
writeCache(); console.log(JSON.parse(body).result);
callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? callsArray.length - 2 : callsArray.length); writeCache();
console.log(coin + ' stack len ' + callStack[coin]); callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? callsArray.length - 2 : callsArray.length);
console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = { async.each(outObj.basilisk[coin].addresses, function(address) {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, var dexUrls = {
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
}, 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
_dexUrls = {}; },
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; for (var a = 0; a < callsArray.length; a++) {
} _dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
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({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
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();
} }
}); if (coin === 'BTC' || coin === 'SYS') {
}); delete _dexUrls.refresh;
} else { delete _dexUrls.getbalance;
// TODO: error }
} //console.log(JSON.stringify(dexUrls));
}); console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
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({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
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 {
// TODO: error
}
});
} else {
callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2);
console.log(coin + ' stack len ' + callStack[coin]);
var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
console.log(_dexUrls);
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({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
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 {
callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); cacheCallInProgress = false;
console.log(coin + ' stack len ' + callStack[coin]);
var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = {};
for (var a = 0; a < callsArray.length; a++) {
_dexUrls[callsArray[a]] = dexUrls[callsArray[a]];
}
if (coin === 'BTC' || coin === 'SYS') {
delete _dexUrls.refresh;
delete _dexUrls.getbalance;
}
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {};
writeCache();
}
console.log(_dexUrls);
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({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
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 {
res.end(JSON.stringify({ res.end(JSON.stringify({
@ -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);

Loading…
Cancel
Save