-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(queue-events): add QueueEventsProducer for publishing custom eve…
…nts (#2844)
- Loading branch information
1 parent
43d80b3
commit 5eb03cd
Showing
7 changed files
with
187 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Create Custom Events | ||
|
||
In BullMQ, creating a generic distributed realtime event emitter is possible by using our **QueueEventsProducer** class. | ||
|
||
Consumers must use **QueueEvents** class to subscribe to those events that they are interested in. | ||
|
||
```typescript | ||
const queueName = 'customQueue'; | ||
const queueEventsProducer = new QueueEventsProducer(queueName, { | ||
connection, | ||
}); | ||
const queueEvents = new QueueEvents(queueName, { | ||
connection, | ||
}); | ||
|
||
interface CustomListener extends QueueEventsListener { | ||
example: (args: { custom: string }, id: string) => void; | ||
} | ||
queueEvents.on<CustomListener>('example', async ({ custom }) => { | ||
// custom logic | ||
}); | ||
|
||
interface CustomEventPayload { | ||
eventName: string; | ||
custom: string; | ||
} | ||
|
||
await queueEventsProducer.publishEvent<CustomEventPayload>({ | ||
eventName: 'example', | ||
custom: 'value', | ||
}); | ||
``` | ||
|
||
Only eventName attribute is required. | ||
|
||
{% hint style="warning" %} | ||
Some event names are reserved from [Queue Listener API Reference](https://api.docs.bullmq.io/interfaces/v5.QueueListener.html). | ||
{% endhint %} | ||
|
||
## Read more: | ||
|
||
- 💡 [Queue Events API Reference](https://api.docs.bullmq.io/classes/v5.QueueEvents.html) | ||
- 💡 [Queue Events Listener API Reference](https://api.docs.bullmq.io/interfaces/v5.QueueEventsListener.html) | ||
- 💡 [Queue Events Producer API Reference](https://api.docs.bullmq.io/interfaces/v5.QueueEventsProducer.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { QueueBaseOptions } from '../interfaces'; | ||
import { QueueBase } from './queue-base'; | ||
import { RedisConnection } from './redis-connection'; | ||
|
||
/** | ||
* The QueueEventsProducer class is used for publishing custom events. | ||
*/ | ||
export class QueueEventsProducer extends QueueBase { | ||
constructor( | ||
name: string, | ||
opts: QueueBaseOptions = { | ||
connection: {}, | ||
}, | ||
Connection?: typeof RedisConnection, | ||
) { | ||
super( | ||
name, | ||
{ | ||
blockingConnection: false, | ||
...opts, | ||
}, | ||
Connection, | ||
); | ||
|
||
this.opts = opts; | ||
} | ||
|
||
/** | ||
* Publish custom event to be processed in QueueEvents. | ||
* @param argsObj - Event payload | ||
* @param maxEvents - Max quantity of events to be saved | ||
*/ | ||
async publishEvent<T extends { eventName: string }>( | ||
argsObj: T, | ||
maxEvents = 1000, | ||
): Promise<void> { | ||
const client = await this.client; | ||
const key = this.keys.events; | ||
const { eventName, ...restArgs } = argsObj; | ||
const args: any[] = ['MAXLEN', '~', maxEvents, '*', 'event', eventName]; | ||
|
||
for (const [key, value] of Object.entries(restArgs)) { | ||
args.push(key, value); | ||
} | ||
|
||
await client.xadd(key, ...args); | ||
} | ||
|
||
/** | ||
* Closes the connection and returns a promise that resolves when the connection is closed. | ||
*/ | ||
async close(): Promise<void> { | ||
if (!this.closing) { | ||
this.closing = this.connection.close(); | ||
} | ||
await this.closing; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters