forked from dfoody/kue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
181 lines (156 loc) · 3.71 KB
/
Copy pathworker.js
File metadata and controls
181 lines (156 loc) · 3.71 KB
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
/*!
* kue - Worker
* Copyright (c) 2011 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter
, redis = require('../redis')
, events = require('./events')
, Job = require('./job');
/**
* Expose `Worker`.
*/
module.exports = Worker;
/**
* Redis connections used by `getJob()` when blocking.
*/
var clients = {};
/**
* Initialize a new `Worker` with the given Queue
* targetting jobs of `type`.
*
* @param {Queue} queue
* @param {String} type
* @api private
*/
function Worker(queue, type) {
this.queue = queue;
this.type = type;
this.client = Worker.client || (Worker.client = redis.createClient());
this.interval = 1000;
}
/**
* Inherit from `EventEmitter.prototype`.
*/
Worker.prototype.__proto__ = EventEmitter.prototype;
/**
* Start processing jobs with the given `fn`,
* checking for jobs every second (by default).
*
* @param {Function} fn
* @return {Worker} for chaining
* @api private
*/
Worker.prototype.start = function(fn){
var self = this;
self.getJob(function(err, job){
if (err) self.error(err, job);
if (!job || err) return process.nextTick(function(){ self.start(fn); });
self.process(job, fn);
});
return this;
};
/**
* Error handler, currently does nothing.
*
* @param {Error} err
* @param {Job} job
* @return {Worker} for chaining
* @api private
*/
Worker.prototype.error = function(err, job){
// TODO: emit non "error"
console.error(err.stack || err.message);
return this;
};
/**
* Process a failed `job`. Set's the job's state
* to "failed" unless more attempts remain, in which
* case the job is marked as "inactive" and remains
* in the queue.
*
* @param {Function} fn
* @return {Worker} for chaining
* @api private
*/
Worker.prototype.failed = function(job, err, fn){
var self = this;
events.emit(job.id, 'failed');
job.failed().error(err);
self.error(err, job);
job.attempt(function(error, remaining, attempts, max){
if (error) return self.error(error, job);
if (remaining) job.inactive()
else {
self.emit('job max_attempts', job);
events.emit(job.id, 'max_attempts');
}
self.start(fn);
});
};
/**
* Process `job`, marking it as active,
* invoking the given callback `fn(job)`,
* if the job fails `Worker#failed()` is invoked,
* otherwise the job is marked as "complete".
*
* @param {Job} job
* @param {Function} fn
* @return {Worker} for chaining
* @api public
*/
Worker.prototype.process = function(job, fn){
var self = this
, start = new Date;
job.active();
fn(job, function(err){
if (err) return self.failed(job, err, fn);
job.complete();
job.set('duration', job.duration = new Date - start);
self.emit('job complete', job);
events.emit(job.id, 'complete');
self.start(fn);
});
return this;
};
/**
* Atomic ZPOP implementation.
*
* @param {String} key
* @param {Function} fn
* @api private
*/
Worker.prototype.zpop = function(key, fn){
this.client
.multi()
.zrange(key, 0, 0)
.zremrangebyrank(key, 0, 0)
.exec(function(err, res){
if (err) return fn(err);
var id = res[0][0];
fn(null, id);
});
};
/**
* Attempt to fetch the next job.
*
* @param {Function} fn
* @api private
*/
Worker.prototype.getJob = function(fn){
var self = this;
// alloc a client for this job type
var client = clients[self.type]
|| (clients[self.type] = redis.createClient());
// BLPOP indicates we have a new inactive job to process
client.blpop('q:' + self.type + ':jobs', 0, function(err) {
self.zpop('q:jobs:' + self.type + ':inactive', function(err, id){
if (err) return fn(err);
if (!id) return fn();
Job.get(id, fn);
});
});
};