From c5f6fa530f2a2186a07e0feeb947fdf1ce938d9d Mon Sep 17 00:00:00 2001 From: Joseph Huckaby Date: Sun, 28 Feb 2016 20:53:31 -0800 Subject: [PATCH] Added chainReaction() function for spawning new job at completion of old one. Added process.env into email_data for future use. --- lib/scheduler.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/scheduler.js b/lib/scheduler.js index 2809c4c4..3331326c 100644 --- a/lib/scheduler.js +++ b/lib/scheduler.js @@ -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 + ')'; @@ -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;