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) {
cacheCallInProgress = false;
// 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;
@ -335,28 +341,11 @@ shepherd.get('/cache-one', function(req, res, next) {
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
if (!address) {
request({
@ -394,12 +383,19 @@ shepherd.get('/cache-one', function(req, res, next) {
}
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] = 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(body);
callStack[coin]--;
@ -409,6 +405,12 @@ shepherd.get('/cache-one', function(req, res, next) {
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 {
@ -443,12 +445,19 @@ shepherd.get('/cache-one', function(req, res, next) {
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] = 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(body);
callStack[coin]--;
@ -458,8 +467,13 @@ shepherd.get('/cache-one', function(req, res, next) {
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 {
res.end(JSON.stringify({

Loading…
Cancel
Save