Browse Source

Merge pull request #186 from SuperNETorg/bin-exec-fix

in mem app runtime log
all-modes
pbca26 7 years ago
committed by GitHub
parent
commit
7c15f8dcfe
  1. 1
      main.js
  2. 60
      routes/cache.js
  3. 13
      routes/shepherd.js

1
main.js

@ -371,6 +371,7 @@ function createWindow(status) {
mainWindow.iguanaIcon = iguanaIcon; mainWindow.iguanaIcon = iguanaIcon;
mainWindow.testLocation = shepherd.testLocation; mainWindow.testLocation = shepherd.testLocation;
mainWindow.kmdMainPassiveMode = shepherd.kmdMainPassiveMode; mainWindow.kmdMainPassiveMode = shepherd.kmdMainPassiveMode;
mainWindow.getAppRuntimeLog = shepherd.getAppRuntimeLog;
if (appConfig.dev) { if (appConfig.dev) {
mainWindow.loadURL('http://127.0.0.1:3000'); mainWindow.loadURL('http://127.0.0.1:3000');

60
routes/cache.js

@ -15,7 +15,7 @@ cache.setVar = function(variable, value) {
*/ */
cache.dumpCacheBeforeExit = function() { cache.dumpCacheBeforeExit = function() {
if (inMemCache) { if (inMemCache) {
shepherd.log('dumping cache before exit'); cache.shepherd.log('dumping cache before exit');
fs.writeFileSync(`${cache.iguanaDir}/shepherd/cache-${inMemPubkey}.json`, JSON.stringify(inMemCache), 'utf8'); fs.writeFileSync(`${cache.iguanaDir}/shepherd/cache-${inMemPubkey}.json`, JSON.stringify(inMemCache), 'utf8');
} }
} }
@ -27,7 +27,7 @@ cache.get = function(req, res, next) {
inMemPubkey = pubkey; inMemPubkey = pubkey;
if (!inMemCache) { if (!inMemCache) {
shepherd.log('serving cache from disk'); cache.shepherd.log('serving cache from disk');
if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`)) { if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`)) {
fs.readFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8', function(err, data) { fs.readFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8', function(err, data) {
@ -49,13 +49,13 @@ cache.get = function(req, res, next) {
inMemCache = parsedJSON; inMemCache = parsedJSON;
res.end(JSON.stringify(successObj)); res.end(JSON.stringify(successObj));
} catch (e) { } catch (e) {
shepherd.log('JSON parse error while reading cache data from disk:'); cache.shepherd.log('JSON parse error while reading cache data from disk:');
shepherd.log(e); cache.shepherd.log(e);
if (e.toString().indexOf('at position') > -1) { if (e.toString().indexOf('at position') > -1) {
const errorPos = e.toString().split(' '); const errorPos = e.toString().split(' ');
shepherd.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)}`); cache.shepherd.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)}`);
shepherd.log('attempting to recover JSON data'); cache.shepherd.log('attempting to recover JSON data');
fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, data.substring(0, errorPos[errorPos.length - 1]), function(err) { fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, data.substring(0, errorPos[errorPos.length - 1]), function(err) {
const successObj = { const successObj = {
@ -195,8 +195,8 @@ cache.groomPost = function(req, res) {
res.end(JSON.stringify(errorObj)); res.end(JSON.stringify(errorObj));
} else { } else {
inMemCache = JSON.parse(_payload); inMemCache = JSON.parse(_payload);
shepherd.log('appending groom post to in mem cache'); cache.shepherd.log('appending groom post to in mem cache');
shepherd.log('appending groom post to on disk cache'); cache.shepherd.log('appending groom post to on disk cache');
fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${_filename}.json`, _payload, function(err) { fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${_filename}.json`, _payload, function(err) {
if (err) { if (err) {
@ -326,8 +326,8 @@ cache.one = function(req, res, next) {
inMemPubkey = pubkey; inMemPubkey = pubkey;
callStack[coin] = 1; callStack[coin] = 1;
shepherd.log(callsArray); cache.shepherd.log(callsArray);
shepherd.log(`iguana core port ${iguanaCorePort}`); cache.shepherd.log(`iguana core port ${iguanaCorePort}`);
if (!sessionKey) { if (!sessionKey) {
const errorObj = { const errorObj = {
@ -349,7 +349,7 @@ cache.one = function(req, res, next) {
internalError = true; internalError = true;
} }
shepherd.log('cache-one call started'); cache.shepherd.log('cache-one call started');
function fixJSON(data) { function fixJSON(data) {
if (data && if (data &&
@ -359,12 +359,12 @@ cache.one = function(req, res, next) {
return parsedJSON; return parsedJSON;
} catch (e) { } catch (e) {
shepherd.log(e); cache.shepherd.log(e);
if (e.toString().indexOf('at position') > -1) { if (e.toString().indexOf('at position') > -1) {
const errorPos = e.toString().split(' '); const errorPos = e.toString().split(' ');
shepherd.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)}`); cache.shepherd.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)}`);
shepherd.log('attempting to recover JSON data'); cache.shepherd.log('attempting to recover JSON data');
return JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); return JSON.parse(data.substring(0, errorPos[errorPos.length - 1]));
} }
if (e.toString().indexOf('Unexpected end of JSON input')) { if (e.toString().indexOf('Unexpected end of JSON input')) {
@ -379,23 +379,23 @@ cache.one = function(req, res, next) {
if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`) && if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`) &&
coin !== 'all') { coin !== 'all') {
if (inMemCache) { if (inMemCache) {
shepherd.log('cache one from mem'); cache.shepherd.log('cache one from mem');
outObj = inMemCache; outObj = inMemCache;
} else { } else {
const _file = fs.readFileSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8'); const _file = fs.readFileSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8');
shepherd.log('cache one from disk'); cache.shepherd.log('cache one from disk');
outObj = fixJSON(_file); outObj = fixJSON(_file);
} }
if (!outObj || if (!outObj ||
!outObj.basilisk) { !outObj.basilisk) {
shepherd.log('no local basilisk info'); cache.shepherd.log('no local basilisk info');
outObj['basilisk'] = {}; outObj['basilisk'] = {};
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} else { } else {
if (!outObj['basilisk'][coin]) { if (!outObj['basilisk'][coin]) {
shepherd.log('no local coin info'); cache.shepherd.log('no local coin info');
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} }
} }
@ -438,7 +438,7 @@ cache.one = function(req, res, next) {
delete _dexUrls.getbalance; delete _dexUrls.getbalance;
} }
shepherd.log(`${coin} address ${address}`); cache.shepherd.log(`${coin} address ${address}`);
if (!outObj.basilisk[coin][address]) { if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {}; outObj.basilisk[coin][address] = {};
@ -524,7 +524,7 @@ cache.one = function(req, res, next) {
if (key === 'getbalance' && if (key === 'getbalance' &&
coin === 'KMD'/* && coin === 'KMD'/* &&
((_parsedJSON && _parsedJSON.balance === 0) || body === [])*/) { ((_parsedJSON && _parsedJSON.balance === 0) || body === [])*/) {
shepherd.log('fallback to kmd explorer ======>'); cache.shepherd.log('fallback to kmd explorer ======>');
request({ request({
url: `http://kmd.explorer.supernet.org/api/addr/${address}/?noTxList=1`, url: `http://kmd.explorer.supernet.org/api/addr/${address}/?noTxList=1`,
method: 'GET' method: 'GET'
@ -558,10 +558,10 @@ cache.one = function(req, res, next) {
outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].data = JSON.parse(body);
outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp
outObj.basilisk[coin][address][key].status = 'done'; outObj.basilisk[coin][address][key].status = 'done';
shepherd.log(dexUrl); cache.shepherd.log(dexUrl);
shepherd.log(body); cache.shepherd.log(body);
callStack[coin]--; callStack[coin]--;
shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -585,7 +585,7 @@ cache.one = function(req, res, next) {
outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp
outObj.basilisk[coin][address][key].status = 'done'; outObj.basilisk[coin][address][key].status = 'done';
callStack[coin]--; callStack[coin]--;
shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -602,9 +602,9 @@ cache.one = function(req, res, next) {
} }
}); });
} else { } else {
shepherd.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`); cache.shepherd.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`);
callStack[coin]--; callStack[coin]--;
shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -637,7 +637,7 @@ cache.one = function(req, res, next) {
}, },
}); });
outObj.basilisk[coin].addresses = addrArray; outObj.basilisk[coin].addresses = addrArray;
shepherd.log(addrArray); cache.shepherd.log(addrArray);
writeCache(); writeCache();
const addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; const addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
@ -650,7 +650,7 @@ cache.one = function(req, res, next) {
callsArrayBTC--; callsArrayBTC--;
} }
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length);
shepherd.log(`${coin} stack len ${callStack[coin]}`); cache.shepherd.log(`${coin} stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
@ -713,7 +713,7 @@ cache.one = function(req, res, next) {
if (response && if (response &&
response.statusCode && response.statusCode &&
response.statusCode === 200) { response.statusCode === 200) {
shepherd.log(JSON.parse(body).basilisk); cache.shepherd.log(JSON.parse(body).basilisk);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -777,7 +777,7 @@ cache.one = function(req, res, next) {
} }
callStack[coin] = callStack[coin] + (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); callStack[coin] = callStack[coin] + (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length);
shepherd.log(`${coin} stack len ${callStack[coin]}`); cache.shepherd.log(`${coin} stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {

13
routes/shepherd.js

@ -36,6 +36,7 @@ var ps = require('ps-node'),
syncOnlyInstanceInterval = -1, syncOnlyInstanceInterval = -1,
guiLog = {}, guiLog = {},
rpcConf = {}, rpcConf = {},
appRuntimeLog = [],
lockDownAddCoin = false; lockDownAddCoin = false;
shepherd.appConfig = _appConfig.config; shepherd.appConfig = _appConfig.config;
@ -99,10 +100,21 @@ shepherd.kmdMainPassiveMode = false;
shepherd.coindInstanceRegistry = coindInstanceRegistry; shepherd.coindInstanceRegistry = coindInstanceRegistry;
shepherd.getAppRuntimeLog = function() {
return new Promise((resolve, reject) => {
resolve(appRuntimeLog);
});
};
shepherd.log = function(msg) { shepherd.log = function(msg) {
if (shepherd.appConfig.dev) { if (shepherd.appConfig.dev) {
console.log(msg); console.log(msg);
} }
appRuntimeLog.push({
time: Date.now(),
msg: msg,
});
}; };
shepherd.startKMDNative = function(selection, isManual) { shepherd.startKMDNative = function(selection, isManual) {
@ -1937,6 +1949,7 @@ var mock = require('./mock');
shepherd.setIO = function(io) { shepherd.setIO = function(io) {
shepherd.io = io; shepherd.io = io;
cache.setVar('io', io); cache.setVar('io', io);
cache.setVar('shepherd', shepherd);
}; };
shepherd.setVar = function(_name, _body) { shepherd.setVar = function(_name, _body) {

Loading…
Cancel
Save