forked from Automattic/kue
/
job.js
555 lines (476 loc) · 10.9 KB
/
job.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*!
* kue - Job
* Copyright (c) 2011 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter
, events = require('./events')
, pool = require('./pool')
, reds = require('reds')
, noop = function(){};
/**
* Expose `Job`.
*/
exports = module.exports = Job;
/**
* Search instance.
*/
var search = reds.createSearch('q:search');
/**
* Default job priority map.
*/
var priorities = exports.priorities = {
low: 10
, normal: 0
, medium: -5
, high: -10
, critical: -15
};
/**
* Map `jobs` by the given array of `ids`.
*
* @param {Object} jobs
* @param {Array} ids
* @return {Array}
* @api private
*/
function map(jobs, ids){
var ret = [];
ids.forEach(function(id){
if (jobs[id]) ret.push(jobs[id]);
});
return ret;
}
/**
* Return a function that handles fetching
* of jobs by the ids fetched.
*
* @param {Function} fn
* @param {String} order
* @return {Function}
* @api private
*/
function get(fn, order) {
return function(err, ids){
if (err) return fn(err);
var pending = ids.length
, jobs = {};
if (!pending) return fn(null, ids);
ids.forEach(function(id){
exports.get(id, function(err, job){
if (err) return fn(err);
jobs[job.id] = job;
--pending || fn(null, 'desc' == order
? map(jobs, ids).reverse()
: map(jobs, ids));
});
});
}
}
/**
* Get with the range `from`..`to`
* and invoke callback `fn(err, ids)`.
*
* @param {Number} from
* @param {Number} to
* @param {String} order
* @param {Function} fn
* @api public
*/
exports.range = function(from, to, order, fn){
pool.alloc().zrange('q:jobs', from, to, get(fn, order));
};
/**
* Get jobs of `state`, with the range `from`..`to`
* and invoke callback `fn(err, ids)`.
*
* @param {String} state
* @param {Number} from
* @param {Number} to
* @param {String} order
* @param {Function} fn
* @api public
*/
exports.rangeByState = function(state, from, to, order, fn){
pool.alloc().zrange('q:jobs:' + state, from, to, get(fn, order));
};
/**
* Get jobs of `type` and `state`, with the range `from`..`to`
* and invoke callback `fn(err, ids)`.
*
* @param {String} type
* @param {String} state
* @param {Number} from
* @param {Number} to
* @param {String} order
* @param {Function} fn
* @api public
*/
exports.rangeByType = function(type, state, from, to, order, fn){
pool.alloc().zrange('q:jobs:' + type + ':' + state, from, to, get(fn, order));
};
/**
* Get job with `id` and callback `fn(err, job)`.
*
* @param {Number} id
* @param {Function} fn
* @api public
*/
exports.get = function(id, fn){
var client = pool.alloc()
, job = new Job;
job.id = id;
client.hgetall('q:job:' + job.id, function(err, hash){
if (err) return fn(err);
if (!hash.type) return fn();
// TODO: really lame, change some methods so
// we can just merge these
job.type = hash.type;
job._delay = hash.delay;
job.priority(Number(hash.priority));
job._progress = hash.progress;
job._attempts = hash.attempts;
job._max_attempts = hash.max_attempts;
job._state = hash.state;
job._error = hash.error;
job.created_at = hash.created_at;
job.updated_at = hash.updated_at;
job.failed_at = hash.failed_at;
job.duration = hash.duration;
try {
if (hash.data) job.data = JSON.parse(hash.data);
fn(err, job);
} catch (err) {
fn(err);
}
});
};
/**
* Remove job `id` if it exists and invoke callback `fn(err)`.
*
* @param {Number} id
* @param {Function} fn
* @api public
*/
exports.remove = function(id, fn){
exports.get(id, function(err, job){
if (err) return fn(err);
if (!job) return fn(new Error('failed to find job ' + id));
job.removeState(job._state, fn);
});
};
/**
* Get log for job `id` and callback `fn(err, log)`.
*
* @param {Number} id
* @param {Function} fn
* @return {Type}
* @api public
*/
exports.log = function(id, fn){
var client = pool.alloc();
client.lrange('q:job:' + id + ':log', 0, -1, fn);
};
/**
* Initialize a new `Job` with the given `type` and `data`.
*
* @param {String} type
* @param {Object} data
* @api public
*/
function Job(type, data) {
this.type = type;
this.data = data || {};
this.client = pool.alloc();
this.priority('normal');
}
/**
* Inherit from `EventEmitter.prototype`.
*/
Job.prototype.__proto__ = EventEmitter.prototype;
/**
* Return JSON-friendly object.
*
* @return {Object}
* @api public
*/
Job.prototype.toJSON = function(){
return {
id: this.id
, type: this.type
, data: this.data
, priority: this._priority
, progress: this._progress || 0
, state: this._state
, error: this._error
, created_at: this.created_at
, updated_at: this.updated_at
, failed_at: this.failed_at
, duration: this.duration
, delay: this._delay
, attempts: {
made: this._attempts
, remaining: this._max_attempts - this._attempts
, max: this._max_attempts
}
};
};
/**
* Log `str` with sprintf-style variable args.
*
* Examples:
*
* job.log('preparing attachments');
* job.log('sending email to %s at %s', user.name, user.email);
*
* Specifiers:
*
* - %s : string
* - %d : integer
*
* @param {String} str
* @param {Mixed} ...
* @return {Job} for chaining
* @api public
*/
Job.prototype.log = function(str){
var args = arguments
, i = 1;
str = str.replace(/%([sd])/g, function(_, type){
var arg = args[i++];
switch (type) {
case 'd': return arg | 0;
case 's': return arg;
}
});
this.client.rpush('q:job:' + this.id + ':log', str);
return this;
};
/**
* Set job `key` to `val`.
*
* @param {String} key
* @param {String} val
* @param {String} fn
* @return {Job} for chaining
* @api public
*/
Job.prototype.set = function(key, val, fn){
this.client.hset('q:job:' + this.id, key, val, fn || noop);
return this;
};
/**
* Set the job progress by telling the job
* how `complete` it is relative to `total`.
*
* @param {Number} complete
* @param {Number} total
* @return {Job} for chaining
* @api public
*/
Job.prototype.progress = function(complete, total){
if (0 == arguments.length) return this._progress;
var n = Math.min(100, complete / total * 100 | 0);
this.set('progress', n);
events.emit(this.id, 'progress', n);
return this;
};
/**
* Set the job delay in `ms`.
*
* @param {Number} ms
* @return {Job|Number}
* @api public
*/
Job.prototype.delay = function(ms){
if (0 == arguments.length) return this._delay;
this._delay = ms;
this.state = 'delayed';
return this;
};
/**
* Set or get the priority `level`, which is one
* of "low", "normal", "medium", and "high", or
* a number in the range of -10..10.
*
* @param {String|Number} level
* @return {Job|Number} for chaining
* @api public
*/
Job.prototype.priority = function(level){
if (0 == arguments.length) return this._priority;
this._priority = null == priorities[level]
? level
: priorities[level];
return this;
};
/**
* Increment attemps, invoking callback `fn(remaining, attempts, max)`.
*
* @param {Function} fn
* @return {Job} for chaining
* @api public
*/
Job.prototype.attempt = function(fn){
var self = this
, client = this.client
, id = this.id
, key = 'q:job:' + id;
client.hsetnx(key, 'max_attempts', 1, function(){
client.hget(key, 'max_attempts', function(err, max){
client.hincrby(key, 'attempts', 1, function(err, attempts){
fn(err, Math.max(0, max - attempts), attempts, max);
});
});
});
return this;
};
/**
* Set max attempts to `n`.
*
* @param {Number} n
* @return {Job} for chaining
* @api public
*/
Job.prototype.attempts = function(n){
this._max_attempts = n;
return this;
};
/**
* Remove `state` with optional callback `fn`.
*
* @param {String} state
* @param {Function} fn
* @return {Job} for chaining
* @api public
*/
Job.prototype.removeState = function(state, fn){
this.client.zrem('q:jobs', this.id);
this.client.zrem('q:jobs:' + state, this.id);
this.client.zrem('q:jobs:' + this.type + ':' + state, this.id, fn || noop);
return this;
};
/**
* Set the job's failure `err`.
*
* @param {Error} err
* @return {Job} for chaining
* @api public
*/
Job.prototype.error = function(err){
if (0 == arguments.length) return this._error;
var str = err.stack || err.message
, summary = str.split('\n')[0];
this.set('failed_at', Date.now());
this.set('error', str);
this.log('%s', summary);
return this;
};
/**
* Set state to `state`.
*
* @param {String} state
* @return {Job} for chaining
* @api public
*/
Job.prototype.state = function(state){
this._state = state;
this.removeState('complete');
this.removeState('failed');
this.removeState('inactive');
this.removeState('active');
this.removeState('delayed');
this.set('state', state);
this.client.zadd('q:jobs', this._priority, this.id);
this.client.zadd('q:jobs:' + state, this._priority, this.id);
this.client.zadd('q:jobs:' + this.type + ':' + state, this._priority, this.id);
return this;
};
/**
* Set state to "complete", and progress to 100%.
*/
Job.prototype.complete = function(){
return this.set('progress', 100).state('complete');
};
/**
* Set state to "failed".
*/
Job.prototype.failed = function(){
return this.state('failed');
};
/**
* Set state to "inactive".
*/
Job.prototype.inactive = function(){
return this.state('inactive');
};
/**
* Set state to "active".
*/
Job.prototype.active = function(){
return this.state('active');
};
/**
* Save the job, optionally invoking the callback `fn(err)`.
*
* @param {Function} fn
* @return {Job} for chaining
* @api public
*/
Job.prototype.save = function(fn){
var client = this.client
, fn = fn || noop
, max = this._max_attempts
, self = this;
// update
if (this.id) return this.update(fn);
// incr id
client.incr('q:ids', function(err, id){
if (err) return fn(err);
var key = 'q:job:' + id;
self.id = id;
self._state = self._state || 'inactive';
if (max) client.hset(key, 'max_attempts', max);
client.sadd('q:job:types', self.type);
self.set('type', self.type);
self.set('created_at', Date.now());
self.update(fn);
// add the job for event mapping
if (self.listeners('complete').length
+ self.listeners('failed').length
+ self.listeners('progress').length) {
events.add(self);
}
});
return this;
};
/**
* Update the job and callback `fn(err)`.
*
* @param {Function} fn
* @api public
*/
Job.prototype.update = function(fn){
var json;
// serialize json data
try {
json = JSON.stringify(this.data);
} catch (err) {
return fn(err);
}
// delay
if (this._delay) this.set('delay', this._delay);
// updated timestamp
this.set('updated_at', Date.now());
// priority
this.set('priority', this._priority);
// state
this.state(this._state);
// data
this.set('data', json, fn);
// search data
search.index(json, this.id);
};