Browse Source

added cache timestamp (wip)

all-modes
pbca26 8 years ago
parent
commit
deeea6495b
  1. 314
      routes/shepherd.js

314
routes/shepherd.js

@ -97,8 +97,8 @@ shepherd.get('/cache', function(req, res, next) {
var pubkey = req.query.pubkey; var pubkey = req.query.pubkey;
if (pubkey) { if (pubkey) {
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) { if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) {
fs.readFile(iguanaDir + '/cache-' + pubkey + '.json', 'utf8', function (err, data) { fs.readFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) {
if (err) { if (err) {
var errorObj = { var errorObj = {
'msg': 'error', 'msg': 'error',
@ -109,7 +109,7 @@ shepherd.get('/cache', function(req, res, next) {
} else { } else {
var successObj = { var successObj = {
'msg': 'success', 'msg': 'success',
'result': JSON.parse(data) 'result': data ? JSON.parse(data) : ''
}; };
res.end(JSON.stringify(successObj)); res.end(JSON.stringify(successObj));
@ -118,22 +118,30 @@ shepherd.get('/cache', function(req, res, next) {
} else { } else {
var errorObj = { var errorObj = {
'msg': 'error', 'msg': 'error',
'result': 'no pubkey provided' 'result': 'no file with handle ' + pubkey
}; };
res.end(JSON.stringify(errorObj)); res.end(JSON.stringify(errorObj));
} }
} else {
var errorObj = {
'msg': 'error',
'result': 'no pubkey provided'
};
res.end(JSON.stringify(errorObj));
} }
}); });
var allcoinsInProgress = false; var cacheCallInProgress = false,
cacheGlobLifetime = 300; // sec
/* /*
* params: userpass, pubkey * params: userpass, pubkey
*/ */
shepherd.get('/cache-all', function(req, res, next) { shepherd.get('/cache-all', function(req, res, next) {
if (!allcoinsInProgress) { if (!cacheCallInProgress) {
allcoinsInProgress = true; cacheCallInProgress = true;
var sessionKey = req.query.userpass, var sessionKey = req.query.userpass,
pubkey = req.query.pubkey, pubkey = req.query.pubkey,
@ -145,12 +153,12 @@ shepherd.get('/cache-all', function(req, res, next) {
basilisk: {} basilisk: {}
}, },
writeCache = function() { writeCache = function() {
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated');
}); });
}, },
callStack = {}, callStack = {},
@ -162,7 +170,7 @@ shepherd.get('/cache-all', function(req, res, next) {
} }
if (total / Object.keys(callStack).length === 1) { if (total / Object.keys(callStack).length === 1) {
allcoinsInProgress = false; cacheCallInProgress = false;
} }
}; };
@ -212,7 +220,7 @@ shepherd.get('/cache-all', function(req, res, next) {
delete dexUrls.getbalance; delete dexUrls.getbalance;
} }
//console.log(JSON.stringify(dexUrls)); //console.log(JSON.stringify(dexUrls));
console.log(coin+' address ' + address); console.log(coin + ' address ' + address);
outObj.basilisk[coin][address] = {}; outObj.basilisk[coin][address] = {};
writeCache(); writeCache();
@ -258,8 +266,9 @@ shepherd.get('/cache-all', function(req, res, next) {
* params: userpass, pubkey, coin, address * params: userpass, pubkey, coin, address
*/ */
shepherd.get('/cache-one', function(req, res, next) { shepherd.get('/cache-one', function(req, res, next) {
if (!allcoinsInProgress) { if (!cacheCallInProgress) {
// TODO: add check to allow only one cache call/sequence in progress // TODO: add check to allow only one cache call/sequence in progress
cacheCallInProgress = true;
var sessionKey = req.query.userpass, var sessionKey = req.query.userpass,
coin = req.query.coin, coin = req.query.coin,
address = req.query.address, address = req.query.address,
@ -269,29 +278,47 @@ shepherd.get('/cache-one', function(req, res, next) {
'msg': 'error', 'msg': 'error',
'result': 'error' 'result': 'error'
}, },
outObj, outObj = {},
pubkey, pubkey,
writeCache = function() { writeCache = function(timeStamp) {
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { if (timeStamp) {
outObj.timestamp = timeStamp;
}
fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated');
if (timeStamp) {
console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped');
}
}); });
}; },
callStack = {},
checkCallStack = function() {
var total = 0;
console.log(callsArray); for (var coin in callStack) {
total =+ callStack[coin];
}
res.end(JSON.stringify({ if (total / Object.keys(callStack).length === 1) {
'msg': 'success', cacheCallInProgress = false;
'result': 'call is initiated' // add timestamp to cache file
})); writeCache(Date.now());
}
};
callStack[coin] = 1;
console.log(callsArray);
console.log('cache-one call started'); console.log('cache-one call started');
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) { if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) {
outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8')); var _file = fs.readFileSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8');
outObj = _file ? JSON.parse(_file) : {};
if (!outObj || !outObj.basilisk) { if (!outObj || !outObj.basilisk) {
console.log('no local basilisk info'); console.log('no local basilisk info');
@ -303,125 +330,136 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} }
} }
} else {
outObj['basilisk'] = {};
outObj['basilisk'][coin] = {};
} }
// update all available coin addresses console.log(Date.now());
if (!address) { // check timestamp
request({ var secondsElapsed = cacheGlobLifetime;
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
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]);
async.each(outObj.basilisk[coin].addresses, function(address) {
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();
}
writeCache();
async.forEachOf(_dexUrls, function(dexUrl, key) { if (outObj && outObj.timestamp) {
request({ var currentEpochTime = new Date(Date.now()) / 1000;
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
/*callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/
writeCache(); secondsElapsed = Number(currentEpochTime) - Number(outObj.timestamp / 1000);
}
});
});
});
} else {
// TODO: error
}
});
} else {
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) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
/*callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/
writeCache(); console.log(secondsElapsed + 's elapsed since last cache update');
} }
}); if (secondsElapsed < cacheGlobLifetime) {
}); res.end(JSON.stringify({
/*var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address 'msg': 'error',
'result': 'too early, wait ' + (cacheGlobLifetime - secondsElapsed) + 's'
request({ }));
url: refreshUrl, cacheCallInProgress = false;
method: 'GET' } else {
}, function (error, response, body) { res.end(JSON.stringify({
if (response && response.statusCode && response.statusCode === 200) { 'msg': 'success',
outObj.basilisk[coin][address].refresh = JSON.parse(body); 'result': 'call is initiated'
console.log(refreshUrl); }));
console.log(body); // update all available coin addresses
if (!address) {
writeCache(); request({
res.end(JSON.stringify({ url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
'msg': 'success', method: 'GET'
'result': iguanaDir + '/cache-' + pubkey + '.json updated' }, function (error, response, body) {
})); if (response && response.statusCode && response.statusCode === 200) {
} outObj.basilisk[coin].addresses = JSON.parse(body).result;
});*/ console.log(JSON.parse(body).result);
writeCache();
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 = {
'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();
}
async.forEachOf(_dexUrls, function(dexUrl, key) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache();
}
});
});
});
} 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) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache();
}
});
});
}
} }
} else { } else {
res.end(JSON.stringify({ res.end(JSON.stringify({

Loading…
Cancel
Save