forked from Real-Serious-Games/task-mule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
task-scheduler.js
44 lines (30 loc) · 953 Bytes
/
task-scheduler.js
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
'use strict';
var cron = require('cron');
var assert = require('chai').assert;
var conf = require('confucious');
var Stopwatch = require('statman-stopwatch');
var TaskScheduler = function (taskRunner, config, log) {
var self = this;
assert.isObject(taskRunner);
assert.isObject(config);
assert.isFunction(log.info);
assert.isFunction(log.error);
assert.isFunction(log.warn);
self.start = function (schedule, callbacks) {
assert.isObject(schedule);
callbacks = callbacks || {};
log.info('Starting task scheduler:');
schedule.jobs.forEach(function (jobSpec) {
assert.isString(jobSpec.name, "Expected 'jobSpec' to have a name field.");
log.info("\t" + jobSpec.name + " - " + jobSpec.cron);
var cronJob = new cron.CronJob({
cronTime: jobSpec.cron,
onTick: function() {
taskRunner.runTask(jobSpec.task, conf);
},
start: true,
});
});
};
};
module.exports = TaskScheduler;