-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
redis-queue-driver.ts
80 lines (68 loc) 路 2.41 KB
/
redis-queue-driver.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
import async from 'async';
import { Queue, Worker } from 'bullmq'
import { QueueInterface } from './queue-interface';
import { Server } from '../server';
const Redis = require('ioredis');
interface QueueWithWorker {
queue: Queue;
worker: Worker;
}
export class RedisQueueDriver implements QueueInterface {
/**
* The queues with workers list.
*/
protected queueWithWorker: Map<string, QueueWithWorker> = new Map();
/**
* Initialize the Prometheus exporter.
*/
constructor(protected server: Server) {
//
}
/**
* Add a new event with data to queue.
*/
addToQueue(queueName: string, data: any = {}): Promise<void> {
return new Promise(resolve => {
let queueWithWorker = this.queueWithWorker.get(queueName);
if (!queueWithWorker) {
return resolve();
}
// TODO: Retry policy? https://docs.bullmq.io/guide/retrying-failing-jobs
queueWithWorker.queue.add('webhook', data).then(() => resolve());
});
}
/**
* Register the code to run when handing the queue.
*/
processQueue(queueName: string, callback: CallableFunction): Promise<void> {
return new Promise(resolve => {
if (!this.queueWithWorker.has(queueName)) {
let connection = new Redis({
maxRetriesPerRequest: null,
enableReadyCheck: false,
...this.server.options.database.redis,
});
this.queueWithWorker.set(queueName, {
queue: new Queue(queueName, {
connection,
defaultJobOptions: { attempts: 6, delay: 1000 },
}),
// TODO: Sandbox the worker? https://docs.bullmq.io/guide/workers/sandboxed-processors
worker: new Worker(queueName, callback as any, {
connection,
concurrency: this.server.options.queue.redis.concurrency,
}),
});
}
resolve();
});
}
/**
* Clear the queues for a graceful shutdown.
*/
clear(): Promise<void> {
return async.each([...this.queueWithWorker], ([queueName, { queue, worker }]: [string, QueueWithWorker], callback) => {
worker.close().then(() => callback());
});
}
}