-
Notifications
You must be signed in to change notification settings - Fork 372
/
queue.ts
373 lines (340 loc) · 9.87 KB
/
queue.ts
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
import { get } from 'lodash';
import { v4 } from 'uuid';
import { JobsOptions, QueueOptions, RepeatOptions } from '../interfaces';
import { isRedisInstance, jobIdForGroup } from '../utils';
import { BulkJobOptions, Job } from './job';
import { QueueGetters } from './queue-getters';
import { Repeat } from './repeat';
import { Scripts } from './scripts';
import { RedisConnection } from './redis-connection';
export interface QueueListener<DataType, ResultType, NameType extends string> {
/**
* Listen to 'cleaned' event.
*
* This event is triggered when the queue calls clean method.
*/
cleaned: (jobs: string[], type: string) => void;
/**
* Listen to 'error' event.
*
* This event is triggered when an error is thrown.
*/
error: (err: Error) => void;
/**
* Listen to 'paused' event.
*
* This event is triggered when the queue is paused.
*/
paused: () => void;
/**
* Listen to 'progress' event.
*
* This event is triggered when the job updates its progress.
*/
progress: (
job: Job<DataType, ResultType, NameType>,
progress: number | object,
) => void;
/**
* Listen to 'removed' event.
*
* This event is triggered when a job is removed.
*/
removed: (job: Job<DataType, ResultType, NameType>) => void;
/**
* Listen to 'resumed' event.
*
* This event is triggered when the queue is resumed.
*/
resumed: () => void;
/**
* Listen to 'waiting' event.
*
* This event is triggered when the queue creates a new job.
*/
waiting: (job: Job<DataType, ResultType, NameType>) => void;
}
/**
* Queue
*
* This class provides methods to add jobs to a queue and some othe high-level
* administration such as pausing or deleting queues.
*
*/
export class Queue<
DataType = any,
ResultType = any,
NameType extends string = string,
> extends QueueGetters<DataType, ResultType, NameType> {
token = v4();
jobsOpts: JobsOptions;
limiter: {
groupKey: string;
} = null;
private _repeat: Repeat;
constructor(
name: string,
opts?: QueueOptions,
Connection?: typeof RedisConnection,
) {
super(
name,
{
sharedConnection: isRedisInstance(opts?.connection),
...opts,
},
Connection,
);
this.jobsOpts = get(opts, 'defaultJobOptions');
this.limiter = get(opts, 'limiter');
this.waitUntilReady()
.then(client => {
if (!this.closing) {
client.hset(
this.keys.meta,
'opts.maxLenEvents',
get(opts, 'streams.events.maxLen', 10000),
);
}
})
.catch(err => {
// We ignore this error to avoid warnings. The error can still
// be received by listening to event 'error'
});
}
emit<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
...args: Parameters<QueueListener<DataType, ResultType, NameType>[U]>
): boolean {
return super.emit(event, ...args);
}
off<U extends keyof QueueListener<DataType, ResultType, NameType>>(
eventName: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.off(eventName, listener);
return this;
}
on<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.on(event, listener);
return this;
}
once<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.once(event, listener);
return this;
}
/**
* Returns this instance current default job options.
*/
get defaultJobOptions(): JobsOptions {
return this.jobsOpts;
}
get repeat(): Promise<Repeat> {
return new Promise<Repeat>(async resolve => {
if (!this._repeat) {
this._repeat = new Repeat(this.name, {
...this.opts,
connection: await this.client,
});
this._repeat.on('error', e => this.emit.bind(this, e));
}
resolve(this._repeat);
});
}
/**
* Adds a new job to the queue.
*
* @param name - Name of the job to be added to the queue,.
* @param data - Arbitrary data to append to the job.
* @param opts - Job options that affects how the job is going to be processed.
*/
async add(name: NameType, data: DataType, opts?: JobsOptions) {
if (opts && opts.repeat) {
return (await this.repeat).addNextRepeatableJob<
DataType,
ResultType,
NameType
>(name, data, { ...this.jobsOpts, ...opts }, true);
} else {
const jobId = jobIdForGroup(opts, data, { limiter: this.limiter });
const job = await Job.create<DataType, ResultType, NameType>(
this,
name,
data,
{
...this.jobsOpts,
...opts,
jobId,
},
);
this.emit('waiting', job);
return job;
}
}
/**
* Adds an array of jobs to the queue.
*
* @param jobs - The array of jobs to add to the queue. Each job is defined by 3
* properties, 'name', 'data' and 'opts'. They follow the same signature as 'Queue.add'.
*/
async addBulk(
jobs: { name: NameType; data: DataType; opts?: BulkJobOptions }[],
): Promise<Job<DataType, DataType, NameType>[]> {
return Job.createBulk<DataType, DataType, NameType>(
this,
jobs.map(job => ({
name: job.name,
data: job.data,
opts: {
...this.jobsOpts,
...job.opts,
jobId: jobIdForGroup(job.opts, job.data, { limiter: this.limiter }),
},
})),
);
}
/**
* Pauses the processing of this queue globally.
*
* We use an atomic RENAME operation on the wait queue. Since
* we have blocking calls with BRPOPLPUSH on the wait queue, as long as the queue
* is renamed to 'paused', no new jobs will be processed (the current ones
* will run until finalized).
*
* Adding jobs requires a LUA script to check first if the paused list exist
* and in that case it will add it there instead of the wait list.
*/
async pause(): Promise<void> {
await Scripts.pause(this, true);
this.emit('paused');
}
/**
* Resumes the processing of this queue globally.
*
* The method reverses the pause operation by resuming the processing of the
* queue.
*/
async resume(): Promise<void> {
await Scripts.pause(this, false);
this.emit('resumed');
}
/**
* Returns true if the queue is currently paused.
*/
async isPaused(): Promise<boolean> {
const client = await this.client;
const pausedKeyExists = await client.hexists(this.keys.meta, 'paused');
return pausedKeyExists === 1;
}
/**
* Get all repeatable meta jobs.
*
* @param start - Offset of first job to return.
* @param end - Offset of last job to return.
* @param asc - Determine the order in which jobs are returned based on their
* next execution time.
*/
async getRepeatableJobs(start?: number, end?: number, asc?: boolean) {
return (await this.repeat).getRepeatableJobs(start, end, asc);
}
async removeRepeatable(
name: NameType,
repeatOpts: RepeatOptions,
jobId?: string,
) {
return (await this.repeat).removeRepeatable(name, repeatOpts, jobId);
}
async removeRepeatableByKey(key: string) {
return (await this.repeat).removeRepeatableByKey(key);
}
/**
* Removes the given job from the queue as well as all its
* dependencies.
*
* @param jobId - The id of the job to remove
* @returns 1 if it managed to remove the job or 0 if the job or
* any of its dependencies was locked.
*/
async remove(jobId: string): Promise<number> {
return Scripts.remove(this, jobId);
}
/**
* Drains the queue, i.e., removes all jobs that are waiting
* or delayed, but not active, completed or failed.
*
* @param delayed - Pass true if it should also clean the
* delayed jobs.
*/
drain(delayed = false): Promise<void> {
return Scripts.drain(this, delayed);
}
/**
* Cleans jobs from a queue. Similar to drain but keeps jobs within a certain
* grace period.
*
* @param grace - The grace period
* @param The - Max number of jobs to clean
* @param {string} [type=completed] - The type of job to clean
* Possible values are completed, wait, active, paused, delayed, failed. Defaults to completed.
* @returns Id jobs from the deleted records
*/
async clean(
grace: number,
limit: number,
type:
| 'completed'
| 'wait'
| 'active'
| 'paused'
| 'delayed'
| 'failed' = 'completed',
): Promise<string[]> {
const jobs = await Scripts.cleanJobsInSet(
this,
type,
Date.now() - grace,
limit,
);
this.emit('cleaned', jobs, type);
return jobs;
}
/**
* Completely destroys the queue and all of its contents irreversibly.
* This method will the *pause* the queue and requires that there are no
* active jobs. It is possible to bypass this requirement, i.e. not
* having active jobs using the "force" option.
*
* Note: This operation requires to iterate on all the jobs stored in the queue
* and can be slow for very large queues.
*
* @param { { force: boolean, count: number }} opts. Use force = true to force obliteration even
* with active jobs in the queue. Use count with the maximum number of deleted keys per iteration,
* 1000 is the default.
*/
async obliterate(opts?: { force?: boolean; count?: number }): Promise<void> {
await this.pause();
let cursor = 0;
do {
cursor = await Scripts.obliterate(this, {
force: false,
count: 1000,
...opts,
});
} while (cursor);
}
/**
* Trim the event stream to an approximately maxLength.
*
* @param maxLength -
*/
async trimEvents(maxLength: number) {
const client = await this.client;
return client.xtrim(this.keys.events, 'MAXLEN', '~', maxLength);
}
}