|
1 | 1 | import { DynamicModule, Module, Provider, Type } from '@nestjs/common'; |
2 | | -import { BullModule } from '@nestjs/bullmq'; |
| 2 | +import { BullModule, getQueueToken } from '@nestjs/bullmq'; |
| 3 | +import { Queue } from 'bullmq'; |
3 | 4 | import { |
4 | 5 | NOTIFICATION_MODULE_OPTIONS, |
5 | 6 | EMAIL_PROVIDER, |
@@ -203,6 +204,58 @@ export class NotificationModule { |
203 | 204 | }, |
204 | 205 | inject: [NOTIFICATION_MODULE_OPTIONS], |
205 | 206 | }, |
| 207 | + // Conditional BullMQ queue providers — creates Queue instances only |
| 208 | + // when the channel is enabled and Redis config is present. |
| 209 | + // We avoid BullModule.registerQueueAsync here to prevent @Processor |
| 210 | + // workers from being created for disabled channels. |
| 211 | + { |
| 212 | + provide: getQueueToken('notifications-email'), |
| 213 | + useFactory: (options: NotificationModuleOptions) => { |
| 214 | + if (!options.channels.email?.enabled || !options.queue?.redis) { |
| 215 | + return null; |
| 216 | + } |
| 217 | + return new Queue('notifications-email', { |
| 218 | + connection: { |
| 219 | + host: options.queue.redis.host, |
| 220 | + port: options.queue.redis.port ?? 6379, |
| 221 | + password: options.queue.redis.password, |
| 222 | + }, |
| 223 | + }); |
| 224 | + }, |
| 225 | + inject: [NOTIFICATION_MODULE_OPTIONS], |
| 226 | + }, |
| 227 | + { |
| 228 | + provide: getQueueToken('notifications-sms'), |
| 229 | + useFactory: (options: NotificationModuleOptions) => { |
| 230 | + if (!options.channels.sms?.enabled || !options.queue?.redis) { |
| 231 | + return null; |
| 232 | + } |
| 233 | + return new Queue('notifications-sms', { |
| 234 | + connection: { |
| 235 | + host: options.queue.redis.host, |
| 236 | + port: options.queue.redis.port ?? 6379, |
| 237 | + password: options.queue.redis.password, |
| 238 | + }, |
| 239 | + }); |
| 240 | + }, |
| 241 | + inject: [NOTIFICATION_MODULE_OPTIONS], |
| 242 | + }, |
| 243 | + { |
| 244 | + provide: getQueueToken('notifications-push'), |
| 245 | + useFactory: (options: NotificationModuleOptions) => { |
| 246 | + if (!options.channels.push?.enabled || !options.queue?.redis) { |
| 247 | + return null; |
| 248 | + } |
| 249 | + return new Queue('notifications-push', { |
| 250 | + connection: { |
| 251 | + host: options.queue.redis.host, |
| 252 | + port: options.queue.redis.port ?? 6379, |
| 253 | + password: options.queue.redis.password, |
| 254 | + }, |
| 255 | + }); |
| 256 | + }, |
| 257 | + inject: [NOTIFICATION_MODULE_OPTIONS], |
| 258 | + }, |
206 | 259 | ]; |
207 | 260 |
|
208 | 261 | const controllers: Type[] = [InAppController, DeviceTokenController, PreferenceController]; |
|
0 commit comments