Skip to content

Commit 5f68f37

Browse files
Merge pull request #3075 from clawrence121/fix-bullmq-event-listener-documentation
docs(queues): correct event listener example for BullMQ
2 parents 172280d + 139a8a3 commit 5f68f37

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

content/techniques/queues.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,27 +294,50 @@ constructor(@Inject(JOB_REF) jobRef: Job) {
294294
295295
#### Event listeners
296296

297-
Bull generates a set of useful events when queue and/or job state changes occur. Nest provides the `@OnQueueEvent(event)` decorator that allows subscribing to a core set of standard events.
297+
BullMQ generates a set of useful events when queue and/or job state changes occur. These events can be subscribed to at the Worker level using the `@OnWorkerEvent(event)` decorator, or at the Queue level with a dedicated listener class and the `@OnQueueEvent(event)` decorator.
298298

299-
Event listeners must be declared within a <a href="techniques/queues#consumers">consumer</a> class (i.e., within a class decorated with the `@Processor()` decorator). To listen for an event, use the `@OnQueueEvent(event)` decorator with the event you want to be handled. For example, to listen to the event emitted when a job enters the active state in the `audio` queue, use the following construct:
299+
Worker events must be declared within a <a href="techniques/queues#consumers">consumer</a> class (i.e., within a class decorated with the `@Processor()` decorator). To listen for an event, use the `@OnWorkerEvent(event)` decorator with the event you want to be handled. For example, to listen to the event emitted when a job enters the active state in the `audio` queue, use the following construct:
300300

301301
```typescript
302-
import { Processor, Process, OnQueueEvent } from '@nestjs/bullmq';
302+
import { Processor, Process, OnWorkerEvent } from '@nestjs/bullmq';
303303
import { Job } from 'bullmq';
304304

305305
@Processor('audio')
306306
export class AudioConsumer {
307-
308-
@OnQueueEvent('active')
307+
@OnWorkerEvent('active')
309308
onActive(job: Job) {
310309
console.log(
311310
`Processing job ${job.id} of type ${job.name} with data ${job.data}...`,
312311
);
313312
}
313+
314+
// ...
315+
}
316+
```
317+
318+
You can see the complete list of events and their arguments as properties of WorkerListener [here](https://api.docs.bullmq.io/interfaces/v4.WorkerListener.html).
319+
320+
QueueEvent listeners must use the `@QueueEventsListener(queue)` decorator and extend the `QueueEventsHost` class provided by `@nestjs/bullmq`. To listen for an event, use the `@OnQueueEvent(event)` decorator with the event you want to be handled. For example, to listen to the event emitted when a job enters the active state in the `audio` queue, use the following construct:
321+
322+
```typescript
323+
import { QueueEventsHost, QueueEventsListener, OnQueueEvent } from '@nestjs/bullmq';
324+
325+
@QueueEventsListener('audio')
326+
export class AudioEventsListener extends QueueEventsHost {
327+
@OnQueueEvent('active')
328+
onActive(job: { jobId: string; prev?: string; }) {
329+
console.log(
330+
`Processing job ${job.jobId}...`,
331+
);
332+
}
333+
314334
// ...
335+
}
315336
```
316337

317-
You can see the complete list of events as properties of QueueEventsListener [here](https://api.docs.bullmq.io/interfaces/v4.QueueEventsListener.html).
338+
> info **Hint** QueueEvent Listeners must be registered as `providers` so the `@nestjs/bullmq` package can pick them up.
339+
340+
You can see the complete list of events and their arguments as properties of QueueEventsListener [here](https://api.docs.bullmq.io/interfaces/v4.QueueEventsListener.html).
318341

319342
#### Queue management
320343

0 commit comments

Comments
 (0)