-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
273 lines (230 loc) · 6.02 KB
/
index.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Requires.
*/
var redis = require('./deps/node-redis'),
Filter = require('filter'),
uuid = require('node-uuid'),
util = require('util');
/**
* Handle an error appropiatly.
*
* @param {Error} error: The error object in question.
* @param {Function} callback: Optional callback to pass error to.
*/
var handleError = function (error, callback) {
if (callback) callback(error);
else {
throw error;
}
};
/**
* The Queue prototype used by the server to add jobs to a queue.
*
* @constructor
* @param {Object} options: A hash that can contain name, host, port, auth, prefix
*/
var Queue = function (options) {
var self = this;
Filter.call(this);
this.name = options.name;
this.client = redis.createClient(options.port, options.host, options.auth);
this.prefix = options.prefix || '';
this.client.on('error', function (error) {
self.emit('error', error);
});
};
// Inherits from Filter.
util.inherits(Queue, Filter);
/**
* Creates a new Queue object.
*
* @param {Object} options: A hash that can contain name, host, port, auth, prefix
* @returns {Queue}
*/
exports.createQueue = function (options) {
return new Queue(options);
};
exports.Queue = Queue;
/**
* Adds a new job to the queue.
*
* @param {Object} payload: The data payload to enqueue.
* @param {Function} callback
*/
Queue.prototype.write = function (payload, callback) {
var self = this;
var id = uuid();
// Push the job.
self.client.rpush(self.prefix + 'queue:' + self.name, JSON.stringify({
id: id,
payload: payload,
error_count: 0,
errors: [],
modified: Date.now()
}), function (error, length) {
if (error) {
return handleError(error, callback);
}
if (callback) callback(null, id);
});
return id;
};
/**
* Worker prototype used by the workers to listen for jobs.
* Inherits from Filter.
*
* @constructor
* @extends {Filter}
* @param {Object} options: A hash that can contain name, host, port, auth, prefix
*/
var Worker = function (options) {
var self = this;
// Call parent
Filter.call(this);
this.host = options.host;
this.port = options.port;
this.auth = options.auth;
this.prefix = options.prefix || '';
this.name = options.name;
this.queues = {};
this._current_id = null;
// TODO: Rename?
this.continual = false;
// Client for use with child jobs.
this._child_client = redis.createClient(this.port, this.host, this.auth);
this._onError = function (error) {
if (error) {
self.emit('error', error);
}
}
this._child_client.on('error', this._onError);
/**
* Callback for blpop responses.
*
* @private
* @param {Error} error: Possible error from Redis
* @param {Object} data: The data from redis.
*/
this._onPop = function (error, data) {
if (error) {
return self.emit('error', error);
}
try {
data = JSON.parse(Buffer.isBuffer(data[1]) ? data[1].toString() : data[1]);
var job = new Job(self, data);
if (!self.continual) {
self._current_id = job.id;
try {
self.client.hset(self.prefix + 'pending:' + self.name, job.id, JSON.stringify(job), self._onError);
} catch (error) {
self._onError(error);
}
}
self.emit('data', job);
} catch (json_error) {
self._onError(json_error);
}
if (!self.client.quitting && self.continual) {
// Listen for more jobs.
self.client.blpop(self.prefix + 'queue:' + self.name, 0, self._onPop);
}
};
};
// Inherits from Filter.
util.inherits(Worker, Filter);
/**
* Creates a new Worker object.
*
* @param {Object} options: A hash that can contain name, host, port, auth, prefix
* @returns {Worker}
*/
exports.createWorker = function (options) {
return new Worker(options);
};
exports.Worker = Worker;
/**
* Listen for the next job. Only has to be called by user if `continual` is false.
*/
Worker.prototype.next = function () {
if (this._current_id) {
this.client.hdel(this.prefix + 'pending:' + this.name, this._current_id, this._onError);
}
this.client.blpop(this.prefix + 'queue:' + this.name, 0, this._onPop);
};
/**
* Start the worker
*/
Worker.prototype.start = function () {
var self = this;
this.client = redis.createClient(this.port, this.host, this.auth);
this.client.on('error', function (error) {
self.emit('error', error);
});
this.next();
};
/**
* Stop the worker
*/
Worker.prototype.stop = function () {
this.client.destroy();
};
/**
* Job prototype used by the workers.
*
* @constructor
* @param {Worker} worker: Parent prototype
* @param {Object} payload: The data to set as the payload.
*/
var Job = function (worker, data) {
this.id = data.id;
this.payload = data.payload;
this.error_count = data.error_count;
this.errors = data.errors;
this.modified = data.modified;
this.queue = worker.name;
this.prefix = worker.prefix;
// Associate with a redis client.
this.parent = worker;
};
exports.Job = Job;
/**
* Add an error the the job.
*
* @param {Error} error: The error object to add.
*/
Job.prototype.reportError = function (error) {
++this.error_count;
this.errors.push(error.message ? error.message : error.toString());
};
/**
* Re-process the job by adding back to the queue.
*
* @param {Function} callback: The optional callback
*/
Job.prototype.retry = function (callback) {
var self = this;
this.parent._child_client.rpush(this.prefix + 'queue:' + this.queue, JSON.stringify({
id: this.id,
payload: this.payload,
error_count: this.error_count,
errors: this.errors,
modified: Date.now()
}), function (error) {
if (error) return handleError(error, callback);
if (callback) callback(null, self.id);
});
};
/**
* For JSON.stringify
*
* @return {Object}
*/
Job.prototype.toJSON = function () {
return {
id: this.id,
payload: this.payload,
error_count: this.error_count,
errors: this.errors,
modified: this.modified
};
};