From 5d040baa3dac3396d1471932b316fc019cc2602b Mon Sep 17 00:00:00 2001 From: pbca26 Date: Fri, 28 Apr 2017 19:34:34 +0300 Subject: [PATCH 1/2] basilisk instance startup --- gui/init.js | 10 +++++----- gui/loading.js | 37 +++++++++++++++---------------------- main.js | 37 +++++++++++++++++++++++++++++-------- routes/shepherd.js | 2 +- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/gui/init.js b/gui/init.js index 251c7f4..84932e4 100644 --- a/gui/init.js +++ b/gui/init.js @@ -15,11 +15,6 @@ $(document).ready(function() { if (appConf && !appConf.manualIguanaStart) { StartIguana(); } - /*if (appConf && appConf.forks && appConf.forks.basilisk) { - setTimeout(function() { - StartBasiliskInstance(); - }, 4000); - }*/ var portcheck; @@ -30,6 +25,11 @@ $(document).ready(function() { if (result !== 'error') { stopcheck(); + + if (appConf && appConf.forks && appConf.forks.basilisk) { + StartIguana_Cache(); + } + $('#loading_status_text').text('Connecting to Basilisk Network...'); EDEX_DEXgetinfoAll(appConf.skipBasiliskNetworkCheck, appConf.minNotaries); } diff --git a/gui/loading.js b/gui/loading.js index 90701db..96c571b 100644 --- a/gui/loading.js +++ b/gui/loading.js @@ -65,28 +65,21 @@ function StartIguana() { }); } -function StartBasiliskInstance() { - var ajax_data = { 'name': 'basilisk'}; - - console.log(ajax_data); - $.ajax({ - type: 'POST', - data: JSON.stringify(ajax_data), - url: 'http://127.0.0.1:17777/shepherd/forks', - dataType: 'xml/html/script/json', // expected format for response - contentType: 'application/json', // send as JSON - success: function(data, textStatus, jqXHR) { - var AjaxOutputData = JSON.parse(data); - console.log('== Shepherd Forks Data OutPut =='); - console.log(AjaxOutputData); - }, - error: function(xhr, textStatus, error) { - console.log(xhr.statusText); - if ( xhr.readyState == 0 ) { - } - console.log(textStatus); - console.log(error); - } +function StartIguana_Cache() { + var ajax_data = { + 'mode': 'basilisk', + 'coin': 'all' + }; + var start_iguana_cache= $.ajax({ + type: 'POST', + data: JSON.stringify(ajax_data), + url: 'http://127.0.0.1:17777/shepherd/forks', + contentType: 'application/json', // send as JSON + }) + start_iguana_cache.done(function(data) { + _data = JSON.parse(data); + console.log(_data.result); + sessionStorage.setItem('IguanaCachePort', _data.result); }); } diff --git a/main.js b/main.js index 68b30d7..ce3cdc5 100644 --- a/main.js +++ b/main.js @@ -21,7 +21,7 @@ var express = require('express'), pm2 = require('pm2'), cluster = require('cluster'), numCPUs = require('os').cpus().length, - kmdcli = require('./private/kmdcli.js'), + coincli = require('./private/coincli.js'), ipc = require('electron').ipcMain; Promise = require('bluebird'); @@ -240,14 +240,35 @@ function createLoadingWindow() { } }); -ipc.on('invokeAction', function(event, data){ - //console.log(data); - kmdcli.command(data, function(err, command) { - //console.log(command); - var result = command; - event.sender.send('kmdcliReply', result); + /* + * var ipc = require('electron').ipcRenderer; + * ipc.once('coincliReply', function(event, response){ + * console.log(response); + * }); + * ipc.send('InvokeCoinCliAction', '{"cli":"kmd","command":"getinfo"}'); + */ + + ipc.on('InvokeCoinCliAction', function(event, data){ + console.log(JSON.stringify(data)); + console.log(data.cli) + console.log(data.command) + + if (data.cli == 'kmd') { + coincli.kmdcommand(data.command, function(err, command) { + //console.log(command); + var result = command; + event.sender.send('coincliReply', result); + }); + } + if (data.cli == 'zec') { + coincli.zeccommand(data.command, function(err, command) { + //console.log(command); + var result = command; + event.sender.send('coincliReply', result); + }); + } }); -}); + //ca333 todo - add os detector to use correct binary - so we can use the same bundle on ALL OS platforms /*if (os.platform() === 'win32') { process.chdir(iguanaDir); diff --git a/routes/shepherd.js b/routes/shepherd.js index db4080b..6c6f42a 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -23,7 +23,7 @@ Promise = require('bluebird'); const fixPath = require('fix-path'); var ps = require('ps-node'), setconf = require('../private/setconf.js'), - kmdcli = require('../private/kmdcli.js'), + coincli = require('../private/coincli.js'), assetChainPorts = require('./ports.js') shepherd = express.Router(), iguanaInstanceRegistry = {}; From fba20b5b6c99035324bc238be099544206e64bd5 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sat, 29 Apr 2017 18:12:34 +0300 Subject: [PATCH 2/2] increased cache lifetime to 600s; prevent cache write in groom api --- routes/cache.js | 81 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/routes/cache.js b/routes/cache.js index 5b04c45..384bbb6 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -152,37 +152,46 @@ cache.groomPost = function(req, res) { var _filename = req.body.filename, _payload = req.body.payload; - if (_filename) { - if (!_payload) { + if (!cacheCallInProgress) { + if (_filename) { + if (!_payload) { + var errorObj = { + 'msg': 'error', + 'result': 'no payload provided' + }; + + res.end(JSON.stringify(errorObj)); + } else { + 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 { var errorObj = { 'msg': 'error', - 'result': 'no payload provided' + 'result': 'no file name provided' }; res.end(JSON.stringify(errorObj)); - } else { - 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 { var errorObj = { 'msg': 'error', - 'result': 'no file name provided' + 'result': 'another job is in progress' }; res.end(JSON.stringify(errorObj)); @@ -303,9 +312,35 @@ 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); + + return parsedJSON; + } catch (e) { + console.log(e); + if (e.toString().indexOf('at position') > -1) { + const errorPos = e.toString().split(' '); + + 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'); + return JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); + } + if (e.toString().indexOf('Unexpected end of JSON input')) { + return {}; + } + } + } else { + return {}; + } + } + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json') && coin !== 'all') { var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); - outObj = _file ? JSON.parse(_file) : {}; + //outObj = _file ? JSON.parse(_file) : {}; + outObj = fixJSON(_file); if (!outObj || !outObj.basilisk) { console.log('no local basilisk info');