forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
452 lines (399 loc) · 10.7 KB
/
scripts.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
/**
* Includes all the scripts needed by the queue and jobs.
*/
'use strict';
const _ = require('lodash');
const scripts = {
isJobInList(client, listKey, jobId) {
return client.isJobInList([listKey, jobId]).then(result => {
return result === 1;
});
},
addJob(client, queue, job, opts) {
const queueKeys = queue.keys;
let keys = [
queueKeys.wait,
queueKeys.paused,
queueKeys['meta-paused'],
queueKeys.id,
queueKeys.delayed,
queueKeys.priority
];
const args = [
queueKeys[''],
_.isUndefined(opts.customJobId) ? '' : opts.customJobId,
job.name,
job.data,
job.opts,
job.timestamp,
job.delay,
job.delay ? job.timestamp + job.delay : 0,
opts.priority || 0,
opts.lifo ? 'RPUSH' : 'LPUSH',
queue.token
];
keys = keys.concat(args);
return client.addJob(keys);
},
pause(queue, pause) {
let src = 'wait',
dst = 'paused';
if (!pause) {
src = 'paused';
dst = 'wait';
}
const keys = _.map(
[src, dst, 'meta-paused', pause ? 'paused' : 'resumed'],
name => {
return queue.toKey(name);
}
);
return queue.client.pause(keys.concat([pause ? 'paused' : 'resumed']));
},
moveToActive(queue, jobId) {
const queueKeys = queue.keys;
const keys = [queueKeys.wait, queueKeys.active, queueKeys.priority];
keys[3] = keys[1] + '@' + queue.token;
keys[4] = queueKeys.stalled;
keys[5] = queueKeys.limiter;
keys[6] = queueKeys.delayed;
keys[7] = queueKeys.drained;
const args = [
queueKeys[''],
queue.token,
queue.settings.lockDuration,
Date.now(),
jobId
];
if (queue.limiter) {
args.push(
queue.limiter.max,
queue.limiter.duration,
!!queue.limiter.bounceBack
);
}
return queue.client.moveToActive(keys.concat(args)).then(raw2jobData);
},
updateProgress(job, progress, data) {
const queue = job.queue;
const keys = [job.id, 'progress'].map(name => {
return queue.toKey(name);
});
const progressJson = JSON.stringify(progress);
return queue.client
.updateProgress(keys, [progressJson, job.id + ',' + progressJson])
.then(() => {
queue.emit('progress', job, progress, data);
});
},
moveToFinishedArgs(
job,
val,
propVal,
shouldRemove,
target,
ignoreLock,
notFetch
) {
const queue = job.queue;
const queueKeys = queue.keys;
const keys = [
queueKeys.active,
queueKeys[target],
queue.toKey(job.id),
queueKeys.wait,
queueKeys.priority,
queueKeys.active + '@' + queue.token
];
if (typeof shouldRemove === 'boolean') {
shouldRemove = shouldRemove ? '1' : '0';
} else if (typeof shouldRemove === 'number') {
shouldRemove = `${shouldRemove + 1}`;
}
const args = [
job.id,
job.finishedOn,
propVal,
_.isUndefined(val) ? 'null' : val,
ignoreLock ? '0' : queue.token,
shouldRemove,
JSON.stringify({ jobId: job.id, val: val }),
notFetch || queue.paused || queue.closing || queue.limiter ? 0 : 1,
queueKeys[''],
queue.settings.lockDuration,
queue.token
];
return keys.concat(args);
},
moveToFinished(
job,
val,
propVal,
shouldRemove,
target,
ignoreLock,
notFetch = false
) {
const args = scripts.moveToFinishedArgs(
job,
val,
propVal,
shouldRemove,
target,
ignoreLock,
notFetch,
job.queue.toKey('')
);
return job.queue.client.moveToFinished(args).then(result => {
if (result < 0) {
throw scripts.finishedErrors(result, job.id, 'finished');
} else if (result) {
return raw2jobData(result);
}
return 0;
});
},
finishedErrors(code, jobId, command) {
switch (code) {
case -1:
return new Error('Missing key for job ' + jobId + ' ' + command);
case -2:
return new Error('Missing lock for job ' + jobId + ' ' + command);
}
},
// TODO: add a retention argument for completed and finished jobs (in time).
moveToCompleted(
job,
returnvalue,
removeOnComplete,
ignoreLock,
notFetch = false
) {
return scripts.moveToFinished(
job,
returnvalue,
'returnvalue',
removeOnComplete,
'completed',
ignoreLock,
notFetch
);
},
moveToFailedArgs(job, failedReason, removeOnFailed, ignoreLock) {
return scripts.moveToFinishedArgs(
job,
failedReason,
'failedReason',
removeOnFailed,
'failed',
ignoreLock,
true
);
},
moveToFailed(job, failedReason, removeOnFailed, ignoreLock) {
const args = scripts.moveToFailedArgs(
job,
failedReason,
removeOnFailed,
ignoreLock
);
return scripts.moveToFinished(args);
},
isFinished(job) {
const keys = _.map(['completed', 'failed'], key => {
return job.queue.toKey(key);
});
return job.queue.client.isFinished(keys.concat([job.id]));
},
moveToDelayedArgs(queue, jobId, timestamp, ignoreLock) {
//
// Bake in the job id first 12 bits into the timestamp
// to guarantee correct execution order of delayed jobs
// (up to 4096 jobs per given timestamp or 4096 jobs apart per timestamp)
//
// WARNING: Jobs that are so far apart that they wrap around will cause FIFO to fail
//
timestamp = _.isUndefined(timestamp) ? 0 : timestamp;
timestamp = +timestamp || 0;
timestamp = timestamp < 0 ? 0 : timestamp;
if (timestamp > 0) {
timestamp = timestamp * 0x1000 + (jobId & 0xfff);
}
const keys = _.map(['active', 'delayed', jobId], name => {
return queue.toKey(name);
});
return keys.concat([
JSON.stringify(timestamp),
jobId,
ignoreLock ? '0' : queue.token
]);
},
moveToDelayed(queue, jobId, timestamp, ignoreLock) {
const args = scripts.moveToDelayedArgs(queue, jobId, timestamp, ignoreLock);
return queue.client.moveToDelayed(args).then(result => {
switch (result) {
case -1:
throw new Error(
'Missing Job ' +
jobId +
' when trying to move from active to delayed'
);
case -2:
throw new Error(
'Job ' +
jobId +
' was locked when trying to move from active to delayed'
);
}
});
},
remove(queue, jobId) {
const keys = _.map(
[
'active',
'wait',
'delayed',
'paused',
'completed',
'failed',
'priority',
jobId,
jobId + ':logs'
],
name => {
return queue.toKey(name);
}
);
return queue.client.removeJob(keys.concat([jobId, queue.token]));
},
extendLock(queue, jobId) {
return queue.client.extendLock([
queue.toKey(jobId) + ':lock',
queue.keys.stalled,
queue.token,
queue.settings.lockDuration,
jobId
]);
},
releaseLock(queue, jobId) {
return queue.client.releaseLock([
queue.toKey(jobId) + ':lock',
queue.token
]);
},
takeLock(queue, job) {
return queue.client.takeLock([
job.lockKey(),
queue.token,
queue.settings.lockDuration
]);
},
/**
It checks if the job in the top of the delay set should be moved back to the
top of the wait queue (so that it will be processed as soon as possible)
*/
updateDelaySet(queue, delayedTimestamp) {
const keys = [
queue.keys.delayed,
queue.keys.active,
queue.keys.wait,
queue.keys.priority,
queue.keys.paused,
queue.keys['meta-paused']
];
const args = [queue.toKey(''), delayedTimestamp, queue.token];
return queue.client.updateDelaySet(keys.concat(args));
},
promote(queue, jobId) {
const keys = [queue.keys.delayed, queue.keys.wait, queue.keys.priority];
const args = [queue.toKey(''), jobId, queue.token];
return queue.client.promote(keys.concat(args));
},
/**
* Looks for unlocked jobs in the active queue.
*
* The job was being worked on, but the worker process died and it failed to renew the lock.
* We call these jobs 'stalled'. This is the most common case. We resolve these by moving them
* back to wait to be re-processed. To prevent jobs from cycling endlessly between active and wait,
* (e.g. if the job handler keeps crashing), we limit the number stalled job recoveries to settings.maxStalledCount.
*/
moveUnlockedJobsToWait(queue) {
const keys = [
queue.keys.stalled,
queue.keys.wait,
queue.keys.active,
queue.keys.failed,
queue.keys['stalled-check'],
queue.keys['meta-paused'],
queue.keys.paused
];
const args = [
queue.settings.maxStalledCount,
queue.toKey(''),
Date.now(),
queue.settings.stalledInterval
];
return queue.client.moveStalledJobsToWait(keys.concat(args));
},
cleanJobsInSet(queue, set, ts, limit) {
return queue.client.cleanJobsInSet([
queue.toKey(set),
queue.toKey(''),
ts,
limit || 0,
set
]);
},
retryJobArgs(job, ignoreLock) {
const queue = job.queue;
const jobId = job.id;
const keys = _.map(['active', 'wait', jobId], name => {
return queue.toKey(name);
});
const pushCmd = (job.opts.lifo ? 'R' : 'L') + 'PUSH';
return keys.concat([pushCmd, jobId, ignoreLock ? '0' : job.queue.token]);
},
/**
* Attempts to reprocess a job
*
* @param {Job} job
* @param {Object} options
* @param {String} options.state The expected job state. If the job is not found
* on the provided state, then it's not reprocessed. Supported states: 'failed', 'completed'
*
* @return {Promise<Number>} Returns a promise that evaluates to a return code:
* 1 means the operation was a success
* 0 means the job does not exist
* -1 means the job is currently locked and can't be retried.
* -2 means the job was not found in the expected set
*/
reprocessJob(job, options) {
const queue = job.queue;
const keys = [
queue.toKey(job.id),
queue.toKey(job.id) + ':lock',
queue.toKey(options.state),
queue.toKey('wait')
];
const args = [job.id, (job.opts.lifo ? 'R' : 'L') + 'PUSH', queue.token];
return queue.client.reprocessJob(keys.concat(args));
}
};
module.exports = scripts;
function array2obj(arr) {
const obj = {};
for (let i = 0; i < arr.length; i += 2) {
obj[arr[i]] = arr[i + 1];
}
return obj;
}
function raw2jobData(raw) {
if (raw) {
const jobData = raw[0];
if (jobData.length) {
const job = array2obj(jobData);
return [job, raw[1]];
}
}
return [];
}