Skip to content

Commit 2573e26

Browse files
docs(): fix misleading bull docs (queue)
1 parent fa8fea4 commit 2573e26

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

content/techniques/queues.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import { BullModule } from '@nestjs/bull';
3030

3131
@Module({
3232
imports: [
33-
BullModule.registerQueue({
34-
name: 'audio',
33+
BullModule.forRoot({
3534
redis: {
3635
host: 'localhost',
3736
port: 6379,
@@ -42,27 +41,70 @@ import { BullModule } from '@nestjs/bull';
4241
export class AppModule {}
4342
```
4443

45-
The `registerQueue()` method is used to instantiate and/or register queues. Queues are shared across modules and processes that connect to the same underlying Redis database with the same credentials. Each queue is unique by its name property (see below). When sharing queues (across modules/processes), the first `registerQueue()` method to run both **instantiates** the queue and **registers** it for that module. Other modules (in the same or separate processes) simply **register** the queue. Queue registration creates an **injection token** that can be used to access the queue in a given Nest module.
44+
The `forRoot()` method is used to register a `bull` package configuration object that will be used by all queues registered in the application (unless specified otherwise). A configuration object consist of the following properties:
4645

47-
For each queue, pass a configuration object containing the following properties:
48-
49-
- `name: string` - A queue name, which will be used as both an injection token (for injecting the queue into controllers/providers), and as an argument to decorators to associate consumer classes and listeners with queues. Required.
5046
- `limiter: RateLimiter` - Options to control the rate at which the queue's jobs are processed. See [RateLimiter](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue) for more information. Optional.
5147
- `redis: RedisOpts` - Options to configure the Redis connection. See [RedisOpts](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue) for more information. Optional.
5248
- `prefix: string` - Prefix for all queue keys. Optional.
5349
- `defaultJobOptions: JobOpts` - Options to control the default settings for new jobs. See [JobOpts](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queueadd) for more information. Optional.
5450
- `settings: AdvancedSettings` - Advanced Queue configuration settings. These should usually not be changed. See [AdvancedSettings](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue) for more information. Optional.
5551

56-
As noted, the `name` property is required. The rest of the options are optional, providing detailed control over queue behavior. These are passed directly to the Bull `Queue` constructor. Read more about these options [here](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue). When registering a queue in a second or subsequent module, it is best practice to omit all options but the `name` property from the configuration object. These options should be specified only in the module that **instantiates** the queue.
52+
All the options are optional, providing detailed control over queue behavior. These are passed directly to the Bull `Queue` constructor. Read more about these options [here](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue).
53+
54+
To register a queue, import the `BullModule#registerQueue()` dynamic module, as follows:
55+
56+
```typescript
57+
BullModule.registerQueue({
58+
name: 'audio',
59+
});
60+
```
5761

5862
> info **Hint** Create multiple queues by passing multiple comma-separated configuration objects to the `registerQueue()` method.
5963
64+
The `registerQueue()` method is used to instantiate and/or register queues. Queues are shared across modules and processes that connect to the same underlying Redis database with the same credentials. Each queue is unique by its name property. A queue name is used as both an injection token (for injecting the queue into controllers/providers), and as an argument to decorators to associate consumer classes and listeners with queues.
65+
66+
You can also override some of the pre-configured options for a specific queue, as follows:
67+
68+
```typescript
69+
BullModule.registerQueue({
70+
name: 'audio',
71+
redis: {
72+
port: 6380,
73+
},
74+
});
75+
```
76+
6077
Since jobs are persisted in Redis, each time a specific named queue is instantiated (e.g., when an app is started/restarted), it attempts to process any old jobs that may exist from a previous unfinished session.
6178

6279
Each queue can have one or many producers, consumers, and listeners. Consumers retrieve jobs from the queue in a specific order: FIFO (the default), LIFO, or according to priorities. Controlling queue processing order is discussed <a href="techniques/queues#consumers">here</a>.
6380

6481
<app-banner-enterprise></app-banner-enterprise>
6582

83+
#### Named configurations
84+
85+
If your queues connect to multiple different Redis instances, you can use a technique called **named configurations**. This feature allows you to register several configurations under specified keys, which then you can refer to in the queue options.
86+
87+
For example, assuming that you have an additional Redis instance (apart from the default one) used by a few queues registered in your application, you can register its configuration as follows:
88+
89+
```typescript
90+
BullModule.forRoot('alternative-config', {
91+
redis: {
92+
port: 6381,
93+
},
94+
});
95+
```
96+
97+
In the example above, `'alternative-config'` is just a configuration key (it can be any arbitrary string).
98+
99+
With this in place, you can now point to this configuration in the `registerQueue()` options object:
100+
101+
```typescript
102+
BullModule.registerQueue({
103+
configKey: 'alternative-queue'
104+
name: 'video',
105+
});
106+
```
107+
66108
#### Producers
67109

68110
Job producers add jobs to queues. Producers are typically application services (Nest [providers](/providers)). To add jobs to a queue, first inject the queue into the service as follows:
@@ -301,13 +343,12 @@ await audioQueue.resume();
301343

302344
#### Async configuration
303345

304-
You may want to pass your queue options asynchronously instead of statically. In this case, use the `registerQueueAsync()` method, which provides several ways to deal with async configuration.
346+
You may want to pass `bull` options asynchronously instead of statically. In this case, use the `forRootAsync()` method which provides several ways to deal with async configuration. Likewise, if you want to pass queue options asynchronously, use the `registerQueueAsync()` method.
305347

306348
One approach is to use a factory function:
307349

308350
```typescript
309-
BullModule.registerQueueAsync({
310-
name: 'audio',
351+
BullModule.forRootAsync({
311352
useFactory: () => ({
312353
redis: {
313354
host: 'localhost',
@@ -320,8 +361,7 @@ BullModule.registerQueueAsync({
320361
Our factory behaves like any other [asynchronous provider](https://docs.nestjs.com/fundamentals/async-providers) (e.g., it can be `async` and it's able to inject dependencies through `inject`).
321362

322363
```typescript
323-
BullModule.registerQueueAsync({
324-
name: 'audio',
364+
BullModule.forRootAsync({
325365
imports: [ConfigModule],
326366
useFactory: async (configService: ConfigService) => ({
327367
redis: {
@@ -336,8 +376,7 @@ BullModule.registerQueueAsync({
336376
Alternatively, you can use the `useClass` syntax:
337377

338378
```typescript
339-
BullModule.registerQueueAsync({
340-
name: 'audio',
379+
BullModule.forRootAsync({
341380
useClass: BullConfigService,
342381
});
343382
```
@@ -361,8 +400,7 @@ class BullConfigService implements BullOptionsFactory {
361400
In order to prevent the creation of `BullConfigService` inside `BullModule` and use a provider imported from a different module, you can use the `useExisting` syntax.
362401

363402
```typescript
364-
BullModule.registerQueueAsync({
365-
name: 'audio',
403+
BullModule.forRootAsync({
366404
imports: [ConfigModule],
367405
useExisting: ConfigService,
368406
});

0 commit comments

Comments
 (0)