-
Notifications
You must be signed in to change notification settings - Fork 5
/
Quartz.js
75 lines (58 loc) · 1.75 KB
/
Quartz.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
72
73
74
75
'use strict';
var kue = require('kue');
var _ = require('underscore');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Server = require('./lib/Server.js');
var Scheduler = require('./lib/Scheduler.js');
function Quartz(config) {
EventEmitter.call(this);
config = _.extend({
queue: {
redis: {
port: 6379,
host: '127.0.0.1'
}
},
listen: 8001,
callbackURL: 'http://127.0.0.1:8001/api/job',
quartzURL: 'http://127.0.0.1:8080/api',
concurrency: 5,
monitor: false
}, config || {});
var self = this;
this._queue = kue.createQueue(config.queue);
var app;
if(config.monitor) {
app = kue.app
}
this._server = new Server({port: config.listen, queue: this._queue, app: kue.app});
this._scheduler = new Scheduler({queue: this._queue, callbackURL: config.callbackURL, quartzURL: config.quartzURL});
this._queue.process('job', config.concurrency || 5, function(job, done) {
self.emit(job.data.name, job.data.key, job.data.data, done);
})
}
util.inherits(Quartz, EventEmitter);
Quartz.prototype.schedule = function(jobName, date, data, callback) {
if(_.isNumber(date)) {
date = new Date(date);
}
this._scheduler.schedule(jobName, date, data, callback);
};
// jobId example: 'group::name'
Quartz.prototype.update = function(jobName, date, data, jobId, callback) {
if(_.isNumber(date)) {
date = new Date(date);
}
this._scheduler.update(jobName, date, data, jobId, callback);
};
Quartz.prototype.cancel = function(jobId, callback) {
this._scheduler.cancel(jobId, callback);
};
Quartz.prototype.start = function(callback) {
this._server.start(callback);
};
Quartz.prototype.shutdown = function() {
this._server.shutdown();
};
module.exports = Quartz;