-
Notifications
You must be signed in to change notification settings - Fork 56
/
worker.js
303 lines (264 loc) · 6.4 KB
/
worker.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
var
_ = require('lodash'),
Beanstalk = require('./client'),
events = require('events'),
util = require('util')
;
/*
Events emitted:
error: payload is error; execution is halted
close: no payload
warning: payload is object with error information; execution continues
info: payload is object with action info
started: no payload
stopped: no payload
job.reserved: job has been reserved; payload is job id
job.handled: payload is object with job info
job.deleted: payload is jobid
job.buried: payload is jobid
*/
var FiveBeansWorker = function(options)
{
events.EventEmitter.call(this);
this.id = options.id;
this.host = options.host;
this.port = options.port;
this.handlers = options.handlers;
this.ignoreDefault = options.ignoreDefault;
this.stopped = false;
this.timeout = options.timeout || 10;
this.client = null;
};
util.inherits(FiveBeansWorker, events.EventEmitter);
FiveBeansWorker.prototype.start = function(tubes)
{
var self = this;
this.stopped = false;
this.on('next', this.doNext.bind(this));
function finishedStarting()
{
self.emit('started');
self.emit('next');
}
this.client = new Beanstalk(this.host, this.port);
this.client.on('connect', function()
{
self.emitInfo('connected to beanstalkd at ' + self.host + ':' + self.port);
self.watch(tubes, function()
{
if (tubes && tubes.length && self.ignoreDefault)
{
self.ignore(['default'], function()
{
finishedStarting();
});
}
else
{
finishedStarting();
}
});
});
this.client.on('error', function(err)
{
self.emitWarning({message: 'beanstalkd connection error', error: err});
self.emit('error', err);
});
this.client.on('close', function()
{
self.emitInfo('beanstalkd connection closed');
self.emit('close');
});
this.client.connect();
};
FiveBeansWorker.prototype.watch = function(tubes, callback)
{
var self = this;
var tube;
if (tubes && (tube = tubes[0]))
{
self.emitInfo('watching tube ' + tube);
self.client.watch(tube, function(err)
{
if (err) self.emitWarning({ message: 'error watching tube', tube: tube, error: err });
self.watch(tubes.slice(1), callback);
});
}
else
callback();
};
FiveBeansWorker.prototype.ignore = function(tubes, callback)
{
var self = this;
var tube;
if (tubes && (tube = tubes[0]))
{
self.emitInfo('ignoring tube ' + tube);
self.client.ignore(tube, function(err)
{
if (err) self.emitWarning({ message: 'error ignoring tube', tube: tube, error: err });
self.ignore(tubes.slice(1), callback);
});
}
else
callback();
};
FiveBeansWorker.prototype.stop = function()
{
this.emitInfo('stopping...');
this.stopped = true;
};
FiveBeansWorker.prototype.doNext = function()
{
var self = this;
if (self.stopped)
{
self.client.end();
self.emitInfo('stopped');
self.emit('stopped');
return;
}
self.client.reserve_with_timeout(self.timeout, function(err, jobID, payload)
{
if (err)
{
if ('TIMED_OUT' !== err)
self.emitWarning({ message: 'error reserving job', error: err });
self.emit('next');
}
else
{
self.emit('job.reserved', jobID);
var job = null;
try { job = JSON.parse(payload.toString()); }
catch (e) { self.emitWarning({ message: 'parsing job JSON', id: jobID, error: e }); }
if (!job || !_.isObject(job))
self.buryAndMoveOn(jobID);
else if (job instanceof Array)
self.runJob(jobID, job[1]);
else
self.runJob(jobID, job);
}
});
};
FiveBeansWorker.prototype.runJob = function(jobID, job)
{
var self = this;
var handler = this.lookupHandler(job.type);
if (job.type === undefined)
{
self.emitWarning({ message: 'no job type', id: jobID, job: job });
self.deleteAndMoveOn(jobID);
}
else if (!handler)
{
self.emitWarning({ message: 'no handler found', id: jobID, type: job.type });
self.buryAndMoveOn(jobID);
}
else
{
self.callHandler(handler, jobID, job.payload);
}
};
FiveBeansWorker.prototype.lookupHandler = function(type)
{
return this.handlers[type];
};
// issue #25
FiveBeansWorker.prototype.callHandler = function callHandler(handler, jobID, jobdata)
{
if (handler.work.length === 3)
{
var patchedHandler = {
work: function(payload, callback)
{
return handler.work(jobID, payload, callback);
}
};
FiveBeansWorker.prototype.doWork.call(this, patchedHandler, jobID, jobdata);
}
else
{
// pass it right on through
FiveBeansWorker.prototype.doWork.apply(this, arguments);
}
};
FiveBeansWorker.prototype.doWork = function doWork(handler, jobID, jobdata)
{
var self = this;
var start = new Date().getTime();
this.currentJob = jobID;
this.currentHandler = handler;
try
{
handler.work(jobdata, function(action, delay)
{
var elapsed = new Date().getTime() - start;
self.emit('job.handled', { id: jobID, type: handler.type, elapsed: elapsed, action: action });
switch (action)
{
case 'success':
self.deleteAndMoveOn(jobID);
break;
case 'release':
self.releaseAndMoveOn(jobID, delay);
break;
case 'bury':
self.buryAndMoveOn(jobID);
break;
default:
self.buryAndMoveOn(jobID);
break;
}
});
}
catch (e)
{
self.emitWarning({ message: 'exception in job handler', id: jobID, handler: handler.type, error: e });
self.buryAndMoveOn(jobID);
}
};
FiveBeansWorker.prototype.buryAndMoveOn = function(jobID)
{
var self = this;
self.client.bury(jobID, Beanstalk.LOWEST_PRIORITY, function(err)
{
if (err) self.emitWarning({ message: 'error burying', id: jobID, error: err });
self.emit('job.buried', jobID);
self.emit('next');
});
};
FiveBeansWorker.prototype.releaseAndMoveOn = function(jobID, delay)
{
var self = this;
if (delay === undefined) delay = 30;
self.client.release(jobID, Beanstalk.LOWEST_PRIORITY, delay, function(err)
{
if (err) self.emitWarning({ message: 'error releasing', id: jobID, error: err });
self.emit('job.released', jobID);
self.emit('next');
});
};
FiveBeansWorker.prototype.deleteAndMoveOn = function(jobID)
{
var self = this;
self.client.destroy(jobID, function(err)
{
if (err) self.emitWarning({ message: 'error deleting', id: jobID, error: err });
self.emit('job.deleted', jobID);
self.emit('next');
});
};
FiveBeansWorker.prototype.emitInfo = function(message)
{
this.emit('info', {
clientid: this.id,
message: message,
});
};
FiveBeansWorker.prototype.emitWarning = function(data)
{
data.clientid = this.id;
this.emit('warning', data);
};
module.exports = FiveBeansWorker;