Skip to content

Commit 9513bd4

Browse files
committed
docs(queues): correct event listener example for BullMQ
1 parent 172280d commit 9513bd4

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

content/techniques/queues.md

Lines changed: 28 additions & 5 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 {
307307

308-
@OnQueueEvent('active')
308+
@OnWorkerEvent('active')
309309
onActive(job: Job) {
310310
console.log(
311311
`Processing job ${job.id} of type ${job.name} with data ${job.data}...`,
312312
);
313313
}
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+
328+
@OnQueueEvent('active')
329+
onActive(job: { jobId: string; prev?: string; }) {
330+
console.log(
331+
`Processing job ${job.jobId}...`,
332+
);
333+
}
334+
314335
// ...
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)