import { DynamicModule , Global , Module , OnModuleInit } from "@nestjs/common" ; import { QueueModuleOptions } from "./queue.interfaces" ; import { QueueProvider } from "./queue.provider" ; import { MetadataScanner } from "@nestjs/core/metadata-scanner" ; import { QueueInjection } from "./queue.decorators" ; import { Queue } from "bull" ; const defaultOptions = { name : "default" , connection : { redis : { port : 6379 } } } ; @ Global ( ) @ Module ( { providers : [ QueueProvider , MetadataScanner ] } ) export class QueueModule implements OnModuleInit { constructor ( private readonly provider , @ QueueInjection ( ) private readonly queue ) { } onModuleInit ( ) { const consumers = this . provider . getEventConsumers ( ) ; if ( consumers && consumers . length ) { consumers . forEach ( consumer => { this . queue . process ( consumer . eventName , ( ... args ) => { consumer . instance . callback = consumer . callback ; return consumer . instance . callback ( ... args ) ; } ) ; } ) ; } } static forRoot ( options ) { const currentOptions = Object . assign ( defaultOptions , options ) ; const [ valueProvider , factoryProvider ] = QueueProvider . createProviders ( currentOptions ) ; return { module : QueueModule , providers : [ valueProvider , factoryProvider ] , exports : [ factoryProvider ] } ; } }