Browse Source

in mem cache

all-modes
pbca26 8 years ago
parent
commit
87a6176519
  1. 1
      main.js
  2. 176
      routes/cache.js
  3. 4
      routes/shepherd.js

1
main.js

@ -354,6 +354,7 @@ function createWindow (status) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
console.log('Closing Main Window...'); console.log('Closing Main Window...');
shepherd.dumpCacheBeforeExit();
shepherd.quitKomodod(); shepherd.quitKomodod();
// if komodod is under heavy load it may not respond to cli stop the first time // if komodod is under heavy load it may not respond to cli stop the first time
setInterval(function() { setInterval(function() {

176
routes/cache.js

@ -3,60 +3,84 @@ const fs = require('fs-extra'),
async = require('async'); async = require('async');
var cache = {}; var cache = {};
var inMemCache;
var inMemPubkey;
cache.setVar = function(variable, value) { cache.setVar = function(variable, value) {
cache[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) { cache.get = function(req, res, next) {
var pubkey = req.query.pubkey; var pubkey = req.query.pubkey;
if (pubkey) { if (pubkey) {
if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { inMemPubkey = pubkey;
fs.readFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) {
if (err) {
var errorObj = {
'msg': 'error',
'result': err
};
res.end(JSON.stringify(errorObj)); if (!inMemCache) {
} else { console.log('serving cache from disk');
try {
var parsedJSON = JSON.parse(data), if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) {
successObj = { fs.readFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) {
'msg': 'success', if (err) {
'result': parsedJSON var errorObj = {
}; 'msg': 'error',
'result': err
res.end(JSON.stringify(successObj)); };
} catch (e) {
console.log(e); res.end(JSON.stringify(errorObj));
if (e.toString().indexOf('at position') > -1) { } else {
const errorPos = e.toString().split(' '); try {
//console.log(errorPos[errorPos.length - 1]); var parsedJSON = JSON.parse(data),
//JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); successObj = {
console.log('JSON error ---> ' + data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20) + ' | error sequence: ' + data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)); 'msg': 'success',
console.log('attempting to recover JSON data'); 'result': parsedJSON
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', data.substring(0, errorPos[errorPos.length - 1]), function(err) { };
var successObj = {
'msg': 'success', inMemCache = parsedJSON;
'result': data.substring(0, errorPos[errorPos.length - 1]) res.end(JSON.stringify(successObj));
}; } catch (e) {
console.log(e);
res.end(JSON.stringify(successObj)); if (e.toString().indexOf('at position') > -1) {
}); const errorPos = e.toString().split(' ');
//console.log(errorPos[errorPos.length - 1]);
//JSON.parse(data.substring(0, errorPos[errorPos.length - 1]));
console.log('JSON error ---> ' + data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20) + ' | error sequence: ' + data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1));
console.log('attempting to recover JSON data');
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', data.substring(0, errorPos[errorPos.length - 1]), function(err) {
var successObj = {
'msg': 'success',
'result': data.substring(0, errorPos[errorPos.length - 1])
};
inMemCache = JSON.parse(data.substring(0, errorPos[errorPos.length - 1]));
res.end(JSON.stringify(successObj));
});
}
} }
} }
} });
}); } else {
var errorObj = {
'msg': 'error',
'result': 'no file with handle ' + pubkey
};
res.end(JSON.stringify(errorObj));
}
} else { } else {
var errorObj = { const successObj = {
'msg': 'error', 'msg': 'success',
'result': 'no file with handle ' + pubkey 'result': inMemCache
}; };
res.end(JSON.stringify(errorObj)); res.end(JSON.stringify(successObj));
} }
} else { } else {
var errorObj = { var errorObj = {
@ -162,23 +186,31 @@ cache.groomPost = function(req, res) {
res.end(JSON.stringify(errorObj)); res.end(JSON.stringify(errorObj));
} else { } else {
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { if (inMemCache) {
if (err) { inMemCache = JSON.parse(_payload);
var errorObj = {
'msg': 'error',
'result': err
};
res.end(JSON.stringify(errorObj)); console.log('appending groom post to in mem cache');
} else { } else {
var successObj = { console.log('appending groom post to on disk cache');
'msg': 'success',
'result': 'done'
};
res.end(JSON.stringify(successObj)); fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) {
} if (err) {
}); var errorObj = {
'msg': 'error',
'result': err
};
res.end(JSON.stringify(errorObj));
} else {
var successObj = {
'msg': 'success',
'result': 'done'
};
res.end(JSON.stringify(successObj));
}
});
}
} }
} else { } else {
var errorObj = { var errorObj = {
@ -199,7 +231,7 @@ cache.groomPost = function(req, res) {
} }
var cacheCallInProgress = false, var cacheCallInProgress = false,
cacheGlobLifetime = 300; // sec cacheGlobLifetime = 600; // sec
// TODO: reset calls' states on new /cache call start // TODO: reset calls' states on new /cache call start
var mock = require('./mock'); var mock = require('./mock');
@ -214,11 +246,14 @@ cache.one = function(req, res, next) {
} }
if (!cacheCallInProgress) { if (!cacheCallInProgress) {
cache.dumpCacheBeforeExit();
fs.readFile(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', 'utf8', function (err, data) { fs.readFile(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', 'utf8', function (err, data) {
if (data) { if (data) {
inMemCache = JSON.parse(data);
data = data.replace('waiting', 'failed'); 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 // 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; 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) { if (err) {
return console.log(err); return console.log(err);
} }
@ -253,7 +289,7 @@ cache.one = function(req, res, next) {
if (timeStamp) { if (timeStamp) {
console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped');
} }
}); });*/
}, },
callStack = {}, callStack = {},
checkCallStack = function() { checkCallStack = function() {
@ -264,6 +300,14 @@ cache.one = function(req, res, next) {
} }
if (total / Object.keys(callStack).length === 1) { 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; cacheCallInProgress = false;
cache.io.emit('messages', { cache.io.emit('messages', {
'message': { 'message': {
@ -286,6 +330,7 @@ cache.one = function(req, res, next) {
}, },
internalError = false; internalError = false;
inMemPubkey = pubkey;
callStack[coin] = 1; callStack[coin] = 1;
console.log(callsArray); console.log(callsArray);
console.log('iguana core port ' + iguanaCorePort); console.log('iguana core port ' + iguanaCorePort);
@ -313,7 +358,6 @@ cache.one = function(req, res, next) {
console.log('cache-one call started'); console.log('cache-one call started');
function fixJSON(data) { function fixJSON(data) {
console.log(data);
if (data && data.length) { if (data && data.length) {
try { try {
var parsedJSON = JSON.parse(data); 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 (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json') && coin !== 'all') {
var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); if (inMemCache) {
//outObj = _file ? JSON.parse(_file) : {}; outObj = inMemCache;
outObj = fixJSON(_file); } else {
var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8');
//outObj = _file ? JSON.parse(_file) : {};
outObj = fixJSON(_file);
}
if (!outObj || !outObj.basilisk) { if (!outObj || !outObj.basilisk) {
console.log('no local basilisk info'); console.log('no local basilisk info');

4
routes/shepherd.js

@ -220,6 +220,10 @@ shepherd.get('/sysinfo', function(req, res, next) {
res.send(obj); res.send(obj);
}); });
shepherd.dumpCacheBeforeExit = function() {
cache.dumpCacheBeforeExit();
}
var cache = require('./cache'); var cache = require('./cache');
var mock = require('./mock'); var mock = require('./mock');

Loading…
Cancel
Save