-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.js
71 lines (60 loc) · 1.66 KB
/
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
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
/**
* The Scheduler Object
*/
// Scheduler constructor
var Scheduler = function Scheduler(queue, supervisor, logger){
this._logger = logger || console; //default to console if logger is not give
// task queue
this._jobqueue = queue; //here we dependency inject constructor style
this._supervisor = supervisor;
this._errorQueue = []; //TODO turn this into a richer object;
};
Scheduler.prototype = {
// can add multiple tasks but not used now.
schedule: function(task){
if(task instanceof Array){
var jobs = this._jobqueue.queueAll(task);
for(key in jobs){
var job = jobs[key];
if(this._supervisor){
this._supervisor.supervise(
job,
this.preRunCallback.bind(this),
this.postRunCallback.bind(this),
this.errorCallback.bind(this)
);
}
}
} else {
var job = this._jobqueue.queue(task);
if(this._supervisor){
this._supervisor.supervise(
job,
this.preRunCallback.bind(this),
this.postRunCallback.bind(this),
this.errorCallback.bind(this)
);
}
}
},
errorCallback: function(error, job){
this._logger.error('Job: ' + job._id + ' had an error. Error was: ' + error);
this.queueError(job);
},
preRunCallback: function(job){
this._logger.info('Job: ' + job._id + ' checking in for preRunCallback');
//TODO decide if we want to dequeue on
// the job being started or when it's finished.
this.dequeueJob(job);
},
postRunCallback: function(job){
this._logger.info('Job: ' + job._id + ' finished.');
},
queueError: function(job){
this._errorQueue.push(job);
},
dequeueJob: function(job){
this._jobqueue.dequeue(job);
}
};
exports.Scheduler = Scheduler;