Skip to content

Commit

Permalink
Added chainReaction() function for spawning new job at completion of …
Browse files Browse the repository at this point in the history
…old one.

Added process.env into email_data for future use.
  • Loading branch information
jhuckaby committed Feb 29, 2016
1 parent 70bdbe3 commit c5f6fa5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module.exports = Class.create({
var dargs = Tools.getDateArgs( Tools.timeNow() );
var email_data = Tools.mergeHashes(event, overrides || {});

email_data.env = process.env;
email_data.config = this.server.config.get();
email_data.edit_event_url = this.server.config.get('base_app_url') + '/#Schedule?sub=edit_event&id=' + event.id;
email_data.nice_date_time = dargs.yyyy_mm_dd + ' ' + dargs.hh_mi_ss + ' (' + dargs.tz + ')';
Expand All @@ -203,6 +204,54 @@ module.exports = Class.create({
} );
},

chainReaction: function(old_job) {
// launch custom new job from completed one
var self = this;

this.storage.listFind( 'global/schedule', { id: old_job.chain }, function(err, event) {
if (err) {
var err_msg = "Failed to launch chain reaction: Event ID not found: " + old_job.chain;
self.logError('scheduler', err_msg);
self.logActivity( 'warning', { description: err_msg } );
if (old_job.notify_fail) {
self.sendEventErrorEmail( old_job, { description: err_msg } );
}
return;
}

var job = Tools.mergeHashes( Tools.copyHash(event, true), {
chain_data: old_job.chain_data || {},
source: "Chain Reaction (" + old_job.event_title + ")",
source_event: old_job.event
} );

self.logDebug(6, "Running event via chain reaction: " + job.title, job);

self.launchJob( job, function(err, jobs_launched) {
if (err) {
var err_msg = "Failed to launch chain reaction: " + job.title + ": " + err.message;
self.logError('scheduler', err_msg);
self.logActivity( 'warning', { description: err_msg } );
if (job.notify_fail) {
self.sendEventErrorEmail( job, { description: err_msg } );
}
else if (old_job.notify_fail) {
self.sendEventErrorEmail( old_job, { description: err_msg } );
}
return;
}

// multiple jobs may have been launched (multiplex)
for (var idx = 0, len = jobs_launched.length; idx < len; idx++) {
var job = jobs_launched[idx];
var stub = { id: job.id, event: job.event, chain_reaction: 1, source_event: old_job.event };
self.logTransaction('job_run', job.event_title, stub);
}

} ); // launch job
} ); // find event
},

shutdownScheduler: function(callback) {
// persist state to storage
var self = this;
Expand Down

0 comments on commit c5f6fa5

Please sign in to comment.