From 913d7ae5c27b6756b0efbc26e2c66e1327261ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Acun=CC=83a?= Date: Tue, 6 Sep 2016 17:04:18 -0300 Subject: [PATCH 1/5] Add paramethers to response header on action started --- lib/agent/actions.js | 14 +++++++++----- lib/agent/plugins/control-panel/sender.js | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/agent/actions.js b/lib/agent/actions.js index d364c0ef3..0918af688 100644 --- a/lib/agent/actions.js +++ b/lib/agent/actions.js @@ -28,7 +28,7 @@ var action_running = function(type, action, name, emitter) { action_stopped(name); setTimeout(function() { - hooks.trigger(type, 'stopped', name, err, out); + hooks.trigger(type, ['stopped'], name, err, out); }, 1000); if (action.events) @@ -75,8 +75,12 @@ var start = function(type, name, opts, cb){ return hooks.trigger('action', 'failed', name, err); } - if (type == 'action') - hooks.trigger(type, 'started', name); + if (type == 'action') { + if (opts && opts.messageID) + hooks.trigger(type, ['started', opts.messageID], name); + else + hooks.trigger(type, ['started'], name); + } if (emitter) { action_running(type, action, name, emitter); @@ -88,7 +92,7 @@ var start = function(type, name, opts, cb){ // trigger this event after 10 seconds. setTimeout(function() { - hooks.trigger(type, 'stopped', name); + hooks.trigger(type, ['stopped'], name); }, 10000); } @@ -110,7 +114,7 @@ actions.stop = function(name) { if (!action) { logger.warn('Action not running!'); - hooks.trigger('action', 'stopped', name); + hooks.trigger('action', ['stopped'], name); } else if (!action.stop) { logger.warn('Action not stoppable!'); } else { diff --git a/lib/agent/plugins/control-panel/sender.js b/lib/agent/plugins/control-panel/sender.js index 2cb4b5791..57d9a5358 100644 --- a/lib/agent/plugins/control-panel/sender.js +++ b/lib/agent/plugins/control-panel/sender.js @@ -1,5 +1,6 @@ -var api = require('./api'), - bus = require('./bus'); +var api = require('./api'), + bus = require('./bus'), + keys = require('./api/keys'); var logger, providers, @@ -17,6 +18,12 @@ var make_request = function(what, data, opts, cb) { logger.info('Posting ' + what); api.push[what](data, opts, function(err, resp) { + console.log('DATA:', data) + if (data.messageID) { + resp.headers['X-Prey-Correlation-ID'] = data.messageID; + resp.headers['X-Prey-Device-ID'] = keys.get().device; + resp.headers['X-Prey-State'] = "PROCESSED"; + } bus.emit('response', what, err, resp); cb && cb(err, resp); }); @@ -57,8 +64,13 @@ exports.notify_action = function(status, name, err) { var body = { command: 'start', target: name, - status: status + status: status[0] } + + if (status[0] == 'started' && status[1]) { + body.messageID = status[1]; + } + if (err) body.reason = err.message; send('response', body); } From 0fff217079096b1cde4e4215f91278c3bca0b86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Acun=CC=83a?= Date: Mon, 16 Jan 2017 15:28:57 -0300 Subject: [PATCH 2/5] Log response headers with messageID --- lib/agent/plugins/control-panel/sender.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/agent/plugins/control-panel/sender.js b/lib/agent/plugins/control-panel/sender.js index 57d9a5358..211c7b9e8 100644 --- a/lib/agent/plugins/control-panel/sender.js +++ b/lib/agent/plugins/control-panel/sender.js @@ -24,6 +24,7 @@ var make_request = function(what, data, opts, cb) { resp.headers['X-Prey-Device-ID'] = keys.get().device; resp.headers['X-Prey-State'] = "PROCESSED"; } + console.log("RESP:", resp.headers) bus.emit('response', what, err, resp); cb && cb(err, resp); }); From 7e0b27e2e5961c12e2dcef2c83b8b20a7ac8238b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Acun=CC=83a?= Date: Mon, 16 Jan 2017 16:42:59 -0300 Subject: [PATCH 3/5] Delete logs --- lib/agent/plugins/control-panel/sender.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/agent/plugins/control-panel/sender.js b/lib/agent/plugins/control-panel/sender.js index 211c7b9e8..951bb92f0 100644 --- a/lib/agent/plugins/control-panel/sender.js +++ b/lib/agent/plugins/control-panel/sender.js @@ -18,13 +18,11 @@ var make_request = function(what, data, opts, cb) { logger.info('Posting ' + what); api.push[what](data, opts, function(err, resp) { - console.log('DATA:', data) if (data.messageID) { resp.headers['X-Prey-Correlation-ID'] = data.messageID; resp.headers['X-Prey-Device-ID'] = keys.get().device; resp.headers['X-Prey-State'] = "PROCESSED"; } - console.log("RESP:", resp.headers) bus.emit('response', what, err, resp); cb && cb(err, resp); }); @@ -68,9 +66,8 @@ exports.notify_action = function(status, name, err) { status: status[0] } - if (status[0] == 'started' && status[1]) { + if (status[0] == 'started' && status[1]) body.messageID = status[1]; - } if (err) body.reason = err.message; send('response', body); From 049267a7111b5bd48e38f3d16451940f598efc09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Acun=CC=83a?= Date: Tue, 17 Jan 2017 11:12:35 -0300 Subject: [PATCH 4/5] Manage buffer commands from bridge --- .../plugins/control-panel/long-polling/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/agent/plugins/control-panel/long-polling/index.js b/lib/agent/plugins/control-panel/long-polling/index.js index d1afbc0b2..57b140869 100644 --- a/lib/agent/plugins/control-panel/long-polling/index.js +++ b/lib/agent/plugins/control-panel/long-polling/index.js @@ -1,4 +1,5 @@ var needle = require('needle'), + util = require('util'), keys = require('../api/keys'), errors = require('../api/errors'), logger = require('../../../common').logger.prefix('long-polling'), @@ -120,6 +121,19 @@ var request = function(re_schedule) { } function process_commands(arr) { + console.log("TYPE:", typeof arr); + console.log("PROCESS!!", arr) + + if (util.isBuffer(arr)) { + console.log("IS BUFFER"); + console.log("String:", arr.toString()) + arr = JSON.parse("{" + arr.toString() + "}")["instruction"] + } else { + console.log("NOT BUFFER :(") + } + // var arr2 = JSON.parse("{" + arr.toString() + "}"); + // arr = arr2["instruction"]; + if (arr.forEach) { arr.forEach(function(el) { var cmd = el.target ? el : parse_cmd(el); From 7200992cf9dcb02a0c6c308e962fb228396359b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Acun=CC=83a?= Date: Thu, 19 Jan 2017 16:42:13 -0300 Subject: [PATCH 5/5] Cover undefined command case --- .../plugins/control-panel/long-polling/index.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/agent/plugins/control-panel/long-polling/index.js b/lib/agent/plugins/control-panel/long-polling/index.js index 57b140869..9c4395a93 100644 --- a/lib/agent/plugins/control-panel/long-polling/index.js +++ b/lib/agent/plugins/control-panel/long-polling/index.js @@ -121,19 +121,10 @@ var request = function(re_schedule) { } function process_commands(arr) { - console.log("TYPE:", typeof arr); - console.log("PROCESS!!", arr) - if (util.isBuffer(arr)) { - console.log("IS BUFFER"); - console.log("String:", arr.toString()) - arr = JSON.parse("{" + arr.toString() + "}")["instruction"] - } else { - console.log("NOT BUFFER :(") + if (arr.toString() == "\n") return; + arr = JSON.parse("{" + arr.toString() + "}")["instruction"] || arr } - // var arr2 = JSON.parse("{" + arr.toString() + "}"); - // arr = arr2["instruction"]; - if (arr.forEach) { arr.forEach(function(el) { var cmd = el.target ? el : parse_cmd(el);