Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add job-id response for multiple actions #289

Merged
merged 5 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var load_action = function(type, name) {
}

// type can be 'action' or 'trigger'
var action_running = function(type, action, name, emitter) {
var action_running = function(type, action, name, opts, emitter) {
logger.info('Running: ' + name)
running[name] = action;

Expand All @@ -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, opts, err, out);
}, 1000);

if (action.events)
Expand Down Expand Up @@ -75,15 +75,13 @@ var start = function(type, name, opts, cb){
return hooks.trigger('action', 'failed', name, err);
}

if (type == 'action') {
if (opts && opts.messageID)
hooks.trigger(type, ['started', opts.messageID], name);
else
hooks.trigger(type, ['started'], name);
}
if (!opts) opts = {};

if (type == 'action')
hooks.trigger(type, 'started', name, opts);

if (emitter) {
action_running(type, action, name, emitter);
action_running(type, action, name, opts, emitter);
} else {

// no emitter was returned, so we have no way of knowing when
Expand All @@ -92,7 +90,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, opts);
}, 10000);

}
Expand All @@ -114,7 +112,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
8 changes: 4 additions & 4 deletions lib/agent/plugins/control-panel/long-polling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ var request = function(re_schedule) {
function attach_listeners(request) {

request.on('data', function(data) {
if (util.isBuffer(data)) {
if (data.toString() == "\n") return;
data = JSON.parse("{" + data.toString() + "}")["instruction"] || data
}
var len = data.length;

if (len && len > 0) {
Expand Down Expand Up @@ -130,10 +134,6 @@ 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: 8 additions & 8 deletions lib/agent/plugins/control-panel/sender.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var api = require('./api'),
bus = require('./bus'),
keys = require('./api/keys');
bus = require('./bus');

var logger,
providers,
Expand All @@ -20,7 +19,7 @@ var make_request = function(what, data, opts, cb) {
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-Device-ID'] = api.keys.get().device;
resp.headers['X-Prey-State'] = "PROCESSED";
}
bus.emit('response', what, err, resp);
Expand Down Expand Up @@ -57,18 +56,19 @@ exports.init = function(common) {
send_status_info = common.config.get('send_status_info');
}

exports.notify_action = function(status, name, err) {
exports.notify_action = function(status, name, opts, err) {
if (name === 'geofencing') return; // geofencing needs to send custom notification

var body = {
command: 'start',
target: name,
status: status[0]
status: status
}

if (status[0] == 'started' && status[1])
body.messageID = status[1];

if (opts) {
if (opts.messageID) body.messageID = opts.messageID;
if (opts.device_job_id) body.reason = { device_job_id: opts.device_job_id };
}
if (err) body.reason = err.message;
send('response', body);
}
Expand Down