From d2e251e082bc67efbd03f672e69910acf92d238d Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Mon, 27 Feb 2017 15:51:08 +0100 Subject: [PATCH 1/4] added missing SYS condition in shepherd cache api --- routes/shepherd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/shepherd.js b/routes/shepherd.js index 94f0b0e..9265cba 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -233,7 +233,7 @@ shepherd.get('/cache-all', function(req, res, next) { if (response && response.statusCode && response.statusCode === 200) { outObj.basilisk[coin].addresses = JSON.parse(body).result; writeCache(); - callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3); + callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' || coin === 'SYS' ? 2 : 4); console.log(coin + ' stack len ' + callStack[coin]); async.each(outObj.basilisk[coin].addresses, function(address) { @@ -424,7 +424,7 @@ shepherd.get('/cache-one', function(req, res, next) { outObj.basilisk[coin].addresses = JSON.parse(body).result; console.log(JSON.parse(body).result); writeCache(); - callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? callsArray.length - 2 : callsArray.length); + callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); console.log(coin + ' stack len ' + callStack[coin]); async.each(outObj.basilisk[coin].addresses, function(address) { From 88dc21395a6ae9be92628e2fba3e10c4e21a4964 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Tue, 28 Feb 2017 12:41:13 +0100 Subject: [PATCH 2/4] added shepherd cache call status --- gui/loading.js | 27 ++++++++++++++------------- routes/shepherd.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/gui/loading.js b/gui/loading.js index 8fef18a..a683e9c 100644 --- a/gui/loading.js +++ b/gui/loading.js @@ -18,18 +18,18 @@ function Iguana_activehandle(callback) { 'method': 'activehandle' }, AjaxOutputData = IguanaAJAX('http://127.0.0.1:7778', ajax_data).done(function(data) { - //console.log(AjaxOutputData.responseText); - AjaxOutputData = JSON.parse(AjaxOutputData.responseText) - //console.log(AjaxOutputData); - resolve(AjaxOutputData); - }).fail(function(xhr, textStatus, error) { - // handle request failures - console.log(xhr.statusText); - if ( xhr.readyState == 0 ) { - } - console.log(textStatus); - console.log(error); - }); + //console.log(AjaxOutputData.responseText); + AjaxOutputData = JSON.parse(AjaxOutputData.responseText) + //console.log(AjaxOutputData); + resolve(AjaxOutputData); + }).fail(function(xhr, textStatus, error) { + // handle request failures + console.log(xhr.statusText); + if ( xhr.readyState == 0 ) { + } + console.log(textStatus); + console.log(error); + }); }); } //Iguana_activehandle().then(function(result){ @@ -70,7 +70,8 @@ function GetAppConf() { // get iguana app conf async: false, type: 'GET', url: 'http://127.0.0.1:17777/shepherd/appconf' - }).done(function(_data) { + }) + .done(function(_data) { console.log('== App Conf Data OutPut =='); console.log(_data); data = _data; diff --git a/routes/shepherd.js b/routes/shepherd.js index 9265cba..e47535b 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -252,14 +252,27 @@ shepherd.get('/cache-all', function(req, res, next) { outObj.basilisk[coin][address] = {}; writeCache(); + // set current call status + async.forEachOf(dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + async.forEachOf(dexUrls, function(dexUrl, key) { var tooEarly = false; if (outObj.basilisk[coin][address][key] && outObj.basilisk[coin][address][key].timestamp && checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; } if (!tooEarly) { + outObj.basilisk[coin][address][key].status = 'in progress'; request({ url: dexUrl, method: 'GET' @@ -268,6 +281,7 @@ shepherd.get('/cache-all', function(req, res, next) { outObj.basilisk[coin][address][key] = {}; outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; console.log(dexUrl); console.log(body); callStack[coin]--; @@ -450,14 +464,27 @@ shepherd.get('/cache-one', function(req, res, next) { writeCache(); } + // set current call status + async.forEachOf(_dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + async.forEachOf(_dexUrls, function(dexUrl, key) { var tooEarly = false; if (outObj.basilisk[coin][address][key] && outObj.basilisk[coin][address][key].timestamp && checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; } if (!tooEarly) { + outObj.basilisk[coin][address][key].status = 'in progress'; request({ url: dexUrl, method: 'GET' @@ -466,6 +493,7 @@ shepherd.get('/cache-one', function(req, res, next) { outObj.basilisk[coin][address][key] = {}; outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; console.log(dexUrl); console.log(body); callStack[coin]--; @@ -514,14 +542,27 @@ shepherd.get('/cache-one', function(req, res, next) { } console.log(_dexUrls); + // set current call status + async.forEachOf(_dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + async.forEachOf(_dexUrls, function(dexUrl, key) { var tooEarly = false; if (outObj.basilisk[coin][address][key] && outObj.basilisk[coin][address][key].timestamp && checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; } if (!tooEarly) { + outObj.basilisk[coin][address][key].status = 'in progress'; request({ url: dexUrl, method: 'GET' @@ -530,6 +571,7 @@ shepherd.get('/cache-one', function(req, res, next) { outObj.basilisk[coin][address][key] = {}; outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; console.log(dexUrl); console.log(body); callStack[coin]--; From 08337f4196703c7beb562c5f9dbdd1d018ca9366 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Tue, 28 Feb 2017 15:17:27 +0100 Subject: [PATCH 3/4] experimental sockets code --- main.js | 38 +++++++++++++++++++++++++++++++++++++- package.json | 3 ++- routes/shepherd.js | 10 ++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 973e9f4..76cb512 100644 --- a/main.js +++ b/main.js @@ -33,6 +33,15 @@ if (os.platform() === 'linux') { var shepherd = require('./routes/shepherd'), guiapp = express(); +guiapp.use(function(req, res, next) { + res.header("Access-Control-Allow-Origin", "http://127.0.0.1:17777"); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + res.header("Access-Control-Allow-Credentials", "true"); + res.header("Access-Control-Allow-Headers", "Content-Type"); + res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS"); + next(); + }); + var appConfig = shepherd.loadLocalConfig(); // load app config // preload.js @@ -71,10 +80,37 @@ guiapp.use('/shepherd', shepherd); console.log(cluster) */ -var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { +/*var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!'); +});*/ + +var server = require('http').createServer(guiapp); +var io = require('socket.io').listen(server); +server.listen(appConfig.iguanaAppPort, function() { + console.log('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!'); }); +io.set('origins', 'http://127.0.0.1:17777'); + +io.on('connection', function(client) { + console.log('EDEX GUI is connected...'); + + client.on('event', function(data) { + console.log(data); + }); + client.on('disconnect', function(data) { + console.log('EDEX GUI is disconnected'); + }); + client.on('join', function(data) { + console.log(data); + client.emit('messages', 'Sockets server is listening'); + }); +}); + +shepherd.setIO(io); + +//io.emit('an event sent to all connected clients'); + module.exports = guiapp; // END GUI App Settings diff --git a/package.json b/package.json index 72f95dd..744cc7f 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "mkdirp": "^0.5.1", "pm2": "^2.3.0", "ps-node": "^0.1.4", - "read-last-lines": "^1.1.0" + "read-last-lines": "^1.1.0", + "socket.io": "^1.7.3" } } diff --git a/routes/shepherd.js b/routes/shepherd.js index e47535b..08fe046 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -91,6 +91,16 @@ shepherd.get('/appconf', function(req, res, next) { res.send(obj); }); +shepherd.get('/socket-test', function(req, res, next) { + res.send('Sockets test'); + shepherd.io.emit('messages', { 'message': 'legacy of grewal' }); +}); + +// expose sockets obj +shepherd.setIO = function(io) { + shepherd.io = io; +}; + /* * params: pubkey */ From 579bd3667e9209afabc188950e48cc7118296472 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Tue, 28 Feb 2017 16:28:29 +0100 Subject: [PATCH 4/4] refactored debug.log reader func --- package.json | 2 +- routes/shepherd.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 744cc7f..f6cc812 100644 --- a/package.json +++ b/package.json @@ -29,11 +29,11 @@ "express": "^4.14.0", "fix-path": "^2.1.0", "fs-extra": "^1.0.0", + "graceful-fs": "^4.1.11", "md5": "^2.2.1", "mkdirp": "^0.5.1", "pm2": "^2.3.0", "ps-node": "^0.1.4", - "read-last-lines": "^1.1.0", "socket.io": "^1.7.3" } } diff --git a/routes/shepherd.js b/routes/shepherd.js index 08fe046..a86b618 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -12,7 +12,6 @@ const electron = require('electron'), exec = require('child_process').exec, md5 = require('md5'), pm2 = require('pm2'), - readLastLines = require('read-last-lines'), request = require('request'), async = require('async'); @@ -783,9 +782,14 @@ shepherd.readDebugLog = function(fileLocation, lastNLines) { reject('readDebugLog error: ' + err); } else { console.log('reading ' + fileLocation); - readLastLines - .read(fileLocation, lastNLines) - .then((lines) => resolve(lines)); + _fs.readFile(fileLocation, 'utf-8', function(err, data) { + if (err) throw err; + + // TODO: truncate komodod debug.log on app start + var lines = data.trim().split('\n'); + var lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n'); + resolve(lastLine); + }); } }); } else {