|
|
@ -3,15 +3,29 @@ const fs = require('fs-extra'), |
|
|
|
async = require('async'); |
|
|
|
|
|
|
|
var cache = {}; |
|
|
|
var inMemCache; |
|
|
|
var inMemPubkey; |
|
|
|
|
|
|
|
cache.setVar = function(variable, value) { |
|
|
|
cache[variable] = value; |
|
|
|
} |
|
|
|
|
|
|
|
cache.dumpCacheBeforeExit = function() { |
|
|
|
if (inMemCache) { |
|
|
|
console.log('dumping cache before exit'); |
|
|
|
fs.writeFileSync(cache.iguanaDir + '/shepherd/cache-' + inMemPubkey + '.json', JSON.stringify(inMemCache), 'utf8'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cache.get = function(req, res, next) { |
|
|
|
var pubkey = req.query.pubkey; |
|
|
|
|
|
|
|
if (pubkey) { |
|
|
|
inMemPubkey = pubkey; |
|
|
|
|
|
|
|
if (!inMemCache) { |
|
|
|
console.log('serving cache from disk'); |
|
|
|
|
|
|
|
if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { |
|
|
|
fs.readFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) { |
|
|
|
if (err) { |
|
|
@ -29,6 +43,7 @@ cache.get = function(req, res, next) { |
|
|
|
'result': parsedJSON |
|
|
|
}; |
|
|
|
|
|
|
|
inMemCache = parsedJSON; |
|
|
|
res.end(JSON.stringify(successObj)); |
|
|
|
} catch (e) { |
|
|
|
console.log(e); |
|
|
@ -44,6 +59,7 @@ cache.get = function(req, res, next) { |
|
|
|
'result': data.substring(0, errorPos[errorPos.length - 1]) |
|
|
|
}; |
|
|
|
|
|
|
|
inMemCache = JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); |
|
|
|
res.end(JSON.stringify(successObj)); |
|
|
|
}); |
|
|
|
} |
|
|
@ -58,6 +74,14 @@ cache.get = function(req, res, next) { |
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
} |
|
|
|
} else { |
|
|
|
const successObj = { |
|
|
|
'msg': 'success', |
|
|
|
'result': inMemCache |
|
|
|
}; |
|
|
|
|
|
|
|
res.end(JSON.stringify(successObj)); |
|
|
|
} |
|
|
|
} else { |
|
|
|
var errorObj = { |
|
|
|
'msg': 'error', |
|
|
@ -162,6 +186,13 @@ cache.groomPost = function(req, res) { |
|
|
|
|
|
|
|
res.end(JSON.stringify(errorObj)); |
|
|
|
} else { |
|
|
|
if (inMemCache) { |
|
|
|
inMemCache = JSON.parse(_payload); |
|
|
|
|
|
|
|
console.log('appending groom post to in mem cache'); |
|
|
|
} else { |
|
|
|
console.log('appending groom post to on disk cache'); |
|
|
|
|
|
|
|
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { |
|
|
|
if (err) { |
|
|
|
var errorObj = { |
|
|
@ -180,6 +211,7 @@ cache.groomPost = function(req, res) { |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
var errorObj = { |
|
|
|
'msg': 'error', |
|
|
@ -199,7 +231,7 @@ cache.groomPost = function(req, res) { |
|
|
|
} |
|
|
|
|
|
|
|
var cacheCallInProgress = false, |
|
|
|
cacheGlobLifetime = 300; // sec
|
|
|
|
cacheGlobLifetime = 600; // sec
|
|
|
|
|
|
|
|
// TODO: reset calls' states on new /cache call start
|
|
|
|
var mock = require('./mock'); |
|
|
@ -214,11 +246,14 @@ cache.one = function(req, res, next) { |
|
|
|
} |
|
|
|
|
|
|
|
if (!cacheCallInProgress) { |
|
|
|
cache.dumpCacheBeforeExit(); |
|
|
|
fs.readFile(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', 'utf8', function (err, data) { |
|
|
|
if (data) { |
|
|
|
inMemCache = JSON.parse(data); |
|
|
|
data = data.replace('waiting', 'failed'); |
|
|
|
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', data, function(err) { |
|
|
|
}); |
|
|
|
/*fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', data, function(err) { |
|
|
|
});*/ |
|
|
|
cache.dumpCacheBeforeExit(); |
|
|
|
} |
|
|
|
}); |
|
|
|
// TODO: add check to allow only one cache call/sequence in progress
|
|
|
@ -244,7 +279,8 @@ cache.one = function(req, res, next) { |
|
|
|
outObj.timestamp = timeStamp; |
|
|
|
} |
|
|
|
|
|
|
|
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { |
|
|
|
inMemCache = outObj; |
|
|
|
/*fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { |
|
|
|
if (err) { |
|
|
|
return console.log(err); |
|
|
|
} |
|
|
@ -253,7 +289,7 @@ cache.one = function(req, res, next) { |
|
|
|
if (timeStamp) { |
|
|
|
console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); |
|
|
|
} |
|
|
|
}); |
|
|
|
});*/ |
|
|
|
}, |
|
|
|
callStack = {}, |
|
|
|
checkCallStack = function() { |
|
|
@ -264,6 +300,14 @@ cache.one = function(req, res, next) { |
|
|
|
} |
|
|
|
|
|
|
|
if (total / Object.keys(callStack).length === 1) { |
|
|
|
cache.dumpCacheBeforeExit(); |
|
|
|
/*fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(inMemCache), function(err) { |
|
|
|
if (err) { |
|
|
|
return console.log(err); |
|
|
|
} |
|
|
|
|
|
|
|
console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); |
|
|
|
});*/ |
|
|
|
cacheCallInProgress = false; |
|
|
|
cache.io.emit('messages', { |
|
|
|
'message': { |
|
|
@ -286,6 +330,7 @@ cache.one = function(req, res, next) { |
|
|
|
}, |
|
|
|
internalError = false; |
|
|
|
|
|
|
|
inMemPubkey = pubkey; |
|
|
|
callStack[coin] = 1; |
|
|
|
console.log(callsArray); |
|
|
|
console.log('iguana core port ' + iguanaCorePort); |
|
|
@ -313,7 +358,6 @@ cache.one = function(req, res, next) { |
|
|
|
console.log('cache-one call started'); |
|
|
|
|
|
|
|
function fixJSON(data) { |
|
|
|
console.log(data); |
|
|
|
if (data && data.length) { |
|
|
|
try { |
|
|
|
var parsedJSON = JSON.parse(data); |
|
|
@ -338,9 +382,13 @@ cache.one = function(req, res, next) { |
|
|
|
} |
|
|
|
|
|
|
|
if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json') && coin !== 'all') { |
|
|
|
if (inMemCache) { |
|
|
|
outObj = inMemCache; |
|
|
|
} else { |
|
|
|
var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); |
|
|
|
//outObj = _file ? JSON.parse(_file) : {};
|
|
|
|
outObj = fixJSON(_file); |
|
|
|
} |
|
|
|
|
|
|
|
if (!outObj || !outObj.basilisk) { |
|
|
|
console.log('no local basilisk info'); |
|
|
|