简体   繁体   中英

Implementing Bull Queue in Typescript

I try to implement Bull queue in Typescript and NestJS, my code:

@Injectable()
export class MailService {
    constructor(
        @InjectQueue('mail')
        private readonly mailQueue: Queue
    ) {}

    async addToQueue(): Promise<void> {
        this.mailQueue.add(() => {
            return this.sendMail(); 
        })
    }
    
    
    async sendMail(): Promise<void> {

        //logic to implement

        this.addToQueue();
    }
}

fast question: Is this implementation sufficient for my job queuing to work?, If not: what i must to do?

I recently wrote a blog post that seems to relate to your use-case:

https://firxworx.com/blog/coding/nodejs/email-module-for-nestjs-with-bull-queue-and-the-nest-mailer/

A few tips:

  • In your module, be sure to import your BullModule (from @nestjs/bull ). For example, you need to configure with your queue name ("mail" in your case) and setup your queue. A common setup would include configuring with the redis hostname and port.
  • In your service, you need to add jobs to the queue, along with optional payload . In your case, you are trying to add a function. Instead, you should add a job name, eg "confirmationEmail", and pass a payload, eg user and token . My example would look like this: await this.mailQueue.add('confirmationEmail', { user, token })
  • You need to implement a processor for your queue. This is a class decorated with the @Processor(QUEUE_NAME) decorator from @nestjs/bull ( @Processor('mail') in your case). The processor handles jobs that are added to the queue.
  • In your processor, you could implement a method eg sendConfirmationEmail() that handles the job named "confirmationEmail". You would decorate that method with @Process(JOB_NAME) , eg @Process('confirmationEmail') . The method can receive your payload. Per my example, the following method signature would provide the user and token : async sendConfirmationEmail(job: Job<{ user: User, token: string }>): Promise<any> (note Job is from the bull package, and that you may wish to type your return vs. using any ). Here is where you would actually send out the email.
  • In your processor class, @nestjs/bull also provides special method decorators including @OnQueueActive() , @OnQueueCompleted() , @OnQueueFailed() . Refer to the docs but you may find these useful for logging or other purposes.

The idea is that your processor handles jobs in the queue when the app is otherwise idle.

Your mail module would presumably have at least a mail.module.ts with configuration, a mail.service.ts that adds jobs to the "mail" queue, and a mail.processor.ts that takes care of completing any jobs added to the "mail" queue.

Further documentation from NestJS is available at:

https://docs.nestjs.com/techniques/queues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM