From 8cd3877472d02c3ff74ffebce7bcb417dcced190 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 10 May 2017 14:54:35 +0300 Subject: [PATCH 1/2] shepherd cache bugfix --- routes/cache.js | 73 ++++++++++++++++++++-------------------------- routes/shepherd.js | 2 +- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/routes/cache.js b/routes/cache.js index 68bbfdc..a7be858 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -142,7 +142,7 @@ cache.groomDelete = function(req, res, next) { if (_filename) { if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json')) { inMemCache = {}; - + fs.unlink(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { if (err) { const errorObj = { @@ -183,6 +183,8 @@ cache.groomPost = function(req, res) { _payload = req.body.payload; if (!cacheCallInProgress) { + cacheCallInProgress = true; + if (_filename) { if (!_payload) { const errorObj = { @@ -192,31 +194,29 @@ cache.groomPost = function(req, res) { res.end(JSON.stringify(errorObj)); } else { - if (inMemCache) { - inMemCache = JSON.parse(_payload); + inMemCache = JSON.parse(_payload); + console.log('appending groom post to in mem cache'); + console.log('appending groom post to on disk cache'); - 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) { - const errorObj = { - 'msg': 'error', - 'result': err - }; + fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { + if (err) { + const errorObj = { + 'msg': 'error', + 'result': err + }; - res.end(JSON.stringify(errorObj)); - } else { - const successObj = { - 'msg': 'success', - 'result': 'done' - }; + cacheCallInProgress = false; + res.end(JSON.stringify(errorObj)); + } else { + const successObj = { + 'msg': 'success', + 'result': 'done' + }; - res.end(JSON.stringify(successObj)); - } - }); - } + cacheCallInProgress = false; + res.end(JSON.stringify(successObj)); + } + }); } } else { const errorObj = { @@ -289,15 +289,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) { - });*/ + + let _data = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json', 'utf8'); + if (_data) { + inMemCache = JSON.parse(_data); + _data = _data.replace('waiting', 'failed'); cache.dumpCacheBeforeExit(); - } - }); + } + // TODO: add check to allow only one cache call/sequence in progress cacheCallInProgress = true; @@ -322,16 +321,6 @@ cache.one = function(req, res, next) { } inMemCache = outObj; - /*fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { - if (err) { - return console.log(err); - } - - console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); - if (timeStamp) { - console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); - } - });*/ }, checkTimestamp = function(dateToCheck) { var currentEpochTime = new Date(Date.now()) / 1000, @@ -394,9 +383,11 @@ cache.one = function(req, res, next) { if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json') && coin !== 'all') { if (inMemCache) { + console.log('cache one from mem'); outObj = inMemCache; } else { var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); + console.log('cache one from disk'); //outObj = _file ? JSON.parse(_file) : {}; outObj = fixJSON(_file); } diff --git a/routes/shepherd.js b/routes/shepherd.js index 7b43e41..4cd360f 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -185,7 +185,7 @@ shepherd.post('/appconf', function(req, res, next) { res.end(JSON.stringify(errorObj)); } else { - shepherd.saveLocalAppConf(req.body.payload); + shepherd.saveLocalAppConf(JSON.parse(req.body.payload)); const errorObj = { 'msg': 'success', From 81678ce817f73e9115d286b7d295fa569cc81399 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 10 May 2017 17:46:11 +0300 Subject: [PATCH 2/2] shepherd groom delete bugfix --- routes/cache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/cache.js b/routes/cache.js index a7be858..e14ec22 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -141,7 +141,7 @@ cache.groomDelete = function(req, res, next) { if (_filename) { if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json')) { - inMemCache = {}; + inMemCache = null; fs.unlink(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { if (err) {