secret
Created

  • Download Gist
scheduler.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
* Module dependencies.
*/
 
var scheduler = require('node-schedule')
, debug = require('debug')('evergig:scheduler')
, async = require('async')
, env = process.env.NODE_ENV || 'development';
 
/**
* Evergig Scheduler Object
*
* @class Represents a Scheduler.
*/
 
/**
* Creates a new Scheduler.
*
* @constructor
*/
 
function Scheduler() {
this.jobs = [];
this.settings = {};
this.initialize();
}
 
/**
* Day Mapping.
*/
 
Scheduler.dayMap = { 'sun': 0, 'mon': 1, 'tue': 2, 'wed': 3, 'thu': 4, 'fri': 5, 'sat': 6 };
 
/**
* Initialize the psuedo app environment and schedule jobs.
*
* @api public
*/
 
Scheduler.prototype.initialize = function() {
var self = this;
async.series([
function(done) {
debug('loading ' + env + ' environment');
require(__dirname + '/../config/environments/all').call(self);
require(__dirname + '/../config/environments/' + env).call(self);
done();
}
, require(__dirname + '/../config/initializers/00_generic').bind(self)
, require(__dirname + '/../config/initializers/02_cradle').bind(self)
, require(__dirname + '/../config/initializers/03_redis').bind(self)
, require(__dirname + '/../config/initializers/04_models').bind(self)
, require(__dirname + '/../config/initializers/06_helpers').bind(self)
, self.scheduleJobs.bind(self)
]);
}
 
/**
* Fetch enabld jobs from the database and schedule them to run.
*
* @param {Function} done
* @api public
*/
 
Scheduler.prototype.scheduleJobs = function(done) {
var Job = global.evergig.Job
, self = this;
Job.enabled(function(err, jobs) {
if (err) {
debug('error scheduling jobs: ' + err.message);
return done();
}
jobs = jobs || [];
jobs.forEach(function(job) {
self.schedule(job);
});
});
}
 
/**
* Schedule a recurring job.
*
* @param {Job} job
* @api public
*/
 
Scheduler.prototype.schedule = function(job) {
var self = this
, rule = new scheduler.RecurrenceRule()
, task
, j;
rule.dayOfWeek = [];
rule.hour = parseInt(job.hour, 10);
rule.minute = parseInt(job.minute, 10);
job.days = job.days || [];
job.days.forEach(function(day) {
rule.dayOfWeek.push(Scheduler.dayMap[day]);
});
debug('scheduling ' + job.name + ': ' + JSON.stringify(rule));
task = require(__dirname + '/jobs/' + job.name);
j = scheduler.scheduleJob(rule, function(){
debug('running ' + job.display_name);
task.call(self, job);
});
self.jobs.push(j);
}
 
/**
* Get or define setting from the current environment.
*
* @param {String} key
* @param {Mixed} val [optional]
* @return {Mixed}
* @api public
*/
 
Scheduler.prototype.set = function(key, val) {
if (typeof val === 'undefined') {
return this.settings[key];
}
this.settings[key] = val;
}
 
/**
* Stub methods for express compatibility.
*
* @api public
*/
 
Scheduler.prototype.engine =
Scheduler.prototype.helpers =
Scheduler.prototype.dynamicHelpers =
Scheduler.prototype.locals = function() {}
 
/**
* Expose `Scheduler`.
*/
 
module.exports = Scheduler;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.