Browse Source

added cache timestamp (wip)

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

130
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;
} }
}; };
@ -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;
for (var coin in callStack) {
total =+ callStack[coin];
}
if (total / Object.keys(callStack).length === 1) {
cacheCallInProgress = false;
// add timestamp to cache file
writeCache(Date.now());
}
}; };
callStack[coin] = 1;
console.log(callsArray); console.log(callsArray);
res.end(JSON.stringify({
'msg': 'success',
'result': 'call is initiated'
}));
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,8 +330,33 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} }
} }
} else {
outObj['basilisk'] = {};
outObj['basilisk'][coin] = {};
} }
console.log(Date.now());
// check timestamp
var secondsElapsed = cacheGlobLifetime;
if (outObj && outObj.timestamp) {
var currentEpochTime = new Date(Date.now()) / 1000;
secondsElapsed = Number(currentEpochTime) - Number(outObj.timestamp / 1000);
console.log(secondsElapsed + 's elapsed since last cache update');
}
if (secondsElapsed < cacheGlobLifetime) {
res.end(JSON.stringify({
'msg': 'error',
'result': 'too early, wait ' + (cacheGlobLifetime - secondsElapsed) + 's'
}));
cacheCallInProgress = false;
} else {
res.end(JSON.stringify({
'msg': 'success',
'result': 'call is initiated'
}));
// update all available coin addresses // update all available coin addresses
if (!address) { if (!address) {
request({ request({
@ -313,9 +365,10 @@ shepherd.get('/cache-one', function(req, res, next) {
}, 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].addresses = JSON.parse(body).result; outObj.basilisk[coin].addresses = JSON.parse(body).result;
console.log(JSON.parse(body).result);
writeCache(); writeCache();
//callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3); callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? callsArray.length - 2 : callsArray.length);
//console.log(coin + ' stack len ' + callStack[coin]); console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) { async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = { var dexUrls = {
@ -339,7 +392,6 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj.basilisk[coin][address] = {}; outObj.basilisk[coin][address] = {};
writeCache(); writeCache();
} }
writeCache();
async.forEachOf(_dexUrls, function(dexUrl, key) { async.forEachOf(_dexUrls, function(dexUrl, key) {
request({ request({
@ -350,9 +402,9 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj.basilisk[coin][address][key] = JSON.parse(body); outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl); console.log(dexUrl);
console.log(body); console.log(body);
/*callStack[coin]--; callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]); console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/ checkCallStack();
writeCache(); writeCache();
} }
@ -364,6 +416,9 @@ shepherd.get('/cache-one', function(req, res, next) {
} }
}); });
} else { } else {
callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2);
console.log(coin + ' stack len ' + callStack[coin]);
var dexUrls = { var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&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,
'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, '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,
@ -396,32 +451,15 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj.basilisk[coin][address][key] = JSON.parse(body); outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl); console.log(dexUrl);
console.log(body); console.log(body);
/*callStack[coin]--; callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]); console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();*/ checkCallStack();
writeCache(); writeCache();
} }
}); });
}); });
/*var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
request({
url: refreshUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].refresh = JSON.parse(body);
console.log(refreshUrl);
console.log(body);
writeCache();
res.end(JSON.stringify({
'msg': 'success',
'result': iguanaDir + '/cache-' + pubkey + '.json updated'
}));
} }
});*/
} }
} else { } else {
res.end(JSON.stringify({ res.end(JSON.stringify({

Loading…
Cancel
Save