Browse Source

added cache timestamp to each call

all-modes
pbca26 8 years ago
parent
commit
69a93667fb
  1. 54
      routes/shepherd.js

54
routes/shepherd.js

@ -307,8 +307,14 @@ shepherd.get('/cache-one', function(req, res, next) {
if (total / Object.keys(callStack).length === 1) { if (total / Object.keys(callStack).length === 1) {
cacheCallInProgress = false; cacheCallInProgress = false;
// add timestamp to cache file // add timestamp to cache file
writeCache(Date.now()); // writeCache(Date.now());
} }
},
checkTimestamp = function(dateToCheck) {
var currentEpochTime = new Date(Date.now()) / 1000,
secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000);
return Math.floor(secondsElapsed);
}; };
callStack[coin] = 1; callStack[coin] = 1;
@ -335,28 +341,11 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj['basilisk'][coin] = {}; 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({ res.end(JSON.stringify({
'msg': 'success', 'msg': 'success',
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
// update all available coin addresses // update all available coin addresses
if (!address) { if (!address) {
request({ request({
@ -394,12 +383,19 @@ shepherd.get('/cache-one', function(req, res, next) {
} }
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] = JSON.parse(body);
if (outObj.basilisk[coin][address][key]) outObj.basilisk[coin][address][key] = {};
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]--;
@ -409,6 +405,12 @@ shepherd.get('/cache-one', 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 {
@ -443,12 +445,19 @@ shepherd.get('/cache-one', function(req, res, next) {
console.log(_dexUrls); console.log(_dexUrls);
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] = JSON.parse(body);
if (outObj.basilisk[coin][address][key]) outObj.basilisk[coin][address][key] = {};
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]--;
@ -458,8 +467,13 @@ shepherd.get('/cache-one', 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 {
res.end(JSON.stringify({ res.end(JSON.stringify({

Loading…
Cancel
Save