Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/**
* 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.