Skip to content

Commit

Permalink
Merge pull request #237 from prey/bridge-node-client
Browse files Browse the repository at this point in the history
Bridge on node client
  • Loading branch information
javo authored Jan 19, 2017
2 parents 8074c7d + 7200992 commit 81f9483
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);

}
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions lib/agent/plugins/control-panel/long-polling/index.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -121,6 +122,10 @@ var request = function(re_schedule) {
}

function process_commands(arr) {
if (util.isBuffer(arr)) {
if (arr.toString() == "\n") return;
arr = JSON.parse("{" + arr.toString() + "}")["instruction"] || arr
}
if (arr.forEach) {
arr.forEach(function(el) {
var cmd = el.target ? el : parse_cmd(el);
Expand Down
16 changes: 13 additions & 3 deletions lib/agent/plugins/control-panel/sender.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,6 +18,11 @@ var make_request = function(what, data, opts, cb) {
logger.info('Posting ' + what);

api.push[what](data, opts, function(err, resp) {
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);
});
Expand Down Expand Up @@ -57,8 +63,12 @@ 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);
}
Expand Down

0 comments on commit 81f9483

Please sign in to comment.