From 9513bd4bdd02474434e47e245fafa55ed97c2360 Mon Sep 17 00:00:00 2001 From: Chris Lawrence Date: Thu, 8 Aug 2024 16:03:14 +0100 Subject: [PATCH 1/4] docs(queues): correct event listener example for BullMQ --- content/techniques/queues.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/content/techniques/queues.md b/content/techniques/queues.md index 422192a08e..e3ae546aa3 100644 --- a/content/techniques/queues.md +++ b/content/techniques/queues.md @@ -294,27 +294,50 @@ constructor(@Inject(JOB_REF) jobRef: Job) { #### Event listeners -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. +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. -Event listeners must be declared within a consumer 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: +Worker events must be declared within a consumer 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: ```typescript -import { Processor, Process, OnQueueEvent } from '@nestjs/bullmq'; +import { Processor, Process, OnWorkerEvent } from '@nestjs/bullmq'; import { Job } from 'bullmq'; @Processor('audio') export class AudioConsumer { - @OnQueueEvent('active') + @OnWorkerEvent('active') onActive(job: Job) { console.log( `Processing job ${job.id} of type ${job.name} with data ${job.data}...`, ); } + + // ... +``` + +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). + +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: + +```typescript +import { QueueEventsHost, QueueEventsListener, OnQueueEvent } from '@nestjs/bullmq'; + +@QueueEventsListener('audio') +export class AudioEventsListener extends QueueEventsHost { + + @OnQueueEvent('active') + onActive(job: { jobId: string; prev?: string; }) { + console.log( + `Processing job ${job.jobId}...`, + ); + } + // ... ``` -You can see the complete list of events as properties of QueueEventsListener [here](https://api.docs.bullmq.io/interfaces/v4.QueueEventsListener.html). +> info **Hint** QueueEvent Listeners must be registered as `providers` so the `@nestjs/bullmq` package can pick them up. + +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). #### Queue management From 357d18d88c5ebae9f8105121dd61c4d0d7d7d4ed Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 9 Aug 2024 09:38:53 +0200 Subject: [PATCH 2/4] Update content/techniques/queues.md --- content/techniques/queues.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/techniques/queues.md b/content/techniques/queues.md index e3ae546aa3..15fb560135 100644 --- a/content/techniques/queues.md +++ b/content/techniques/queues.md @@ -304,7 +304,6 @@ import { Job } from 'bullmq'; @Processor('audio') export class AudioConsumer { - @OnWorkerEvent('active') onActive(job: Job) { console.log( From bd8801d77e660a68f8be6c450c292b413252270a Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 9 Aug 2024 09:41:32 +0200 Subject: [PATCH 3/4] Update content/techniques/queues.md --- content/techniques/queues.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/techniques/queues.md b/content/techniques/queues.md index 15fb560135..f2ed463f56 100644 --- a/content/techniques/queues.md +++ b/content/techniques/queues.md @@ -312,6 +312,7 @@ export class AudioConsumer { } // ... +} ``` 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). From 139a8a399fd9a49dbbfe8117b587d9e2f5a10955 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 9 Aug 2024 09:41:51 +0200 Subject: [PATCH 4/4] Update content/techniques/queues.md --- content/techniques/queues.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/techniques/queues.md b/content/techniques/queues.md index f2ed463f56..1973aa69cf 100644 --- a/content/techniques/queues.md +++ b/content/techniques/queues.md @@ -324,7 +324,6 @@ import { QueueEventsHost, QueueEventsListener, OnQueueEvent } from '@nestjs/bull @QueueEventsListener('audio') export class AudioEventsListener extends QueueEventsHost { - @OnQueueEvent('active') onActive(job: { jobId: string; prev?: string; }) { console.log( @@ -333,6 +332,7 @@ export class AudioEventsListener extends QueueEventsHost { } // ... +} ``` > info **Hint** QueueEvent Listeners must be registered as `providers` so the `@nestjs/bullmq` package can pick them up.