Efficient way to use Bull Queue in Nestjs

233 views Asked by At

I'm integrating Bull Queue for my email service which I'm using in multiple modules. But after injecting Queue in the email.service.ts file, I'm getting dependency errors like the below for all modules where I was using this service.

app.module.ts

@Module({
  imports: [
    BullModule.forRoot({}),
  ],
  exports: [BullModule],
})

email.module.ts

@Module({
  imports: [
    BullModule.registerQueue({
      name: 'email',
    }),
  ],
  providers: [EmailService, EmailConsumer],
  exports: [EmailService],
})

email.service.ts

constructor(@InjectQueue('email') private readonly emailQueue: Queue) {}

Dependency Error

auth.module.ts (before error)

@Module({
  imports: [JwtModule.register({})],
  controllers: [AuthController],
  providers: [
    AuthService,
    ...authProviders,
  ],
})

auth.module.ts (after resolving error)

@Module({
  imports: [
    JwtModule.register({}),
    BullModule.registerQueue({
      name: 'email',
    })
],
  controllers: [AuthController],
  providers: [
    AuthService,
    ...authProviders,
  ],
})

I resolved this error by registering a queue in every module file (10-15 in my case) as I did in email.module.ts. but I think there must be an efficient way to do this. Instead of copy-pasting code in every module.

0

There are 0 answers