You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/techniques/queues.md
+54-16Lines changed: 54 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,8 +30,7 @@ import { BullModule } from '@nestjs/bull';
30
30
31
31
@Module({
32
32
imports: [
33
-
BullModule.registerQueue({
34
-
name: 'audio',
33
+
BullModule.forRoot({
35
34
redis: {
36
35
host: 'localhost',
37
36
port: 6379,
@@ -42,27 +41,70 @@ import { BullModule } from '@nestjs/bull';
42
41
exportclassAppModule {}
43
42
```
44
43
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:
46
45
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.
50
46
-`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.
51
47
-`redis: RedisOpts` - Options to configure the Redis connection. See [RedisOpts](https://github.com/OptimalBits/bull/blob/master/REFERENCE.md#queue) for more information. Optional.
52
48
-`prefix: string` - Prefix for all queue keys. Optional.
53
49
-`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.
54
50
-`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.
55
51
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
+
```
57
61
58
62
> info **Hint** Create multiple queues by passing multiple comma-separated configuration objects to the `registerQueue()` method.
59
63
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
+
60
77
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.
61
78
62
79
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 <ahref="techniques/queues#consumers">here</a>.
63
80
64
81
<app-banner-enterprise></app-banner-enterprise>
65
82
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
+
66
108
#### Producers
67
109
68
110
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:
Ourfactorybehaveslikeanyother [asynchronousprovider](https://docs.nestjs.com/fundamentals/async-providers) (e.g., it can be `async` and it's able to inject dependencies through `inject`).
0 commit comments