-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
54 lines (49 loc) · 1.63 KB
/
handler.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
import { Job, Worker } from 'bullmq';
import { BullMQMessageTranslator } from './message-translators';
import { TranslateBullMQ } from './types';
class BullMQHandler extends Worker {
constructor(
queueName: string,
processor?: any,
options?: any,
Connection?: any
) {
super(queueName, processor, options, Connection);
}
/**
* @description
* This method is called by the BullMQ Worker when a job is received.
* It is called by the BullMQ Worker, not by the application.
* @param {Job} job
* @param {string} token
* @returns {Promise<boolean>}
* @memberof BullMQHandler
*/
protected callProcessJob(job: Job, token: string) {
const translator = new BullMQMessageTranslator();
const { content, metadata } = translator.translateMessage(job, token);
return this.handle({ content, metadata });
}
/**
* Method to be implemented by the application
* @param {TranslateBullMQ["content"]} content
* @param {TranslateBullMQ["content"]} metadata
* @returns {Promise<boolean>} - Return `true` if the message was processed successfully
* @memberof BullMQHandler
* @description
* This method is called by the BullMQ Worker when a job is received.
* It is called by the BullMQ Worker, not by the application.
* @example
* async handle(content: Translate["content"], metadata?: Translate["content"]): Promise<boolean> {
* // messagem processing logic
* return true;
* }
*/
async handle(
_content: TranslateBullMQ['content'],
_metadata?: TranslateBullMQ['metadata']
): Promise<boolean> {
throw new Error('Not implemented');
}
}
export { BullMQHandler };