简体   繁体   English

在 Typescript 中实现公牛队列

[英]Implementing Bull Queue in Typescript

I try to implement Bull queue in Typescript and NestJS, my code:我尝试在 Typescript 和 NestJS 中实现 Bull 队列,我的代码:

@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/ 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 ).在你的模块中,一定要导入你的BullModule (来自@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.一个常见的设置包括使用 redis 主机名和端口进行配置。
  • 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.在您的情况下,您正在尝试添加 function。 Instead, you should add a job name, eg "confirmationEmail", and pass a payload, eg user and token .相反,您应该添加一个作业名称,例如“confirmationEmail”,并传递一个有效负载,例如usertoken My example would look like this: await this.mailQueue.add('confirmationEmail', { user, token })我的示例如下所示: 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).这是一个 class 装饰有来自@nestjs/bull@Processor(QUEUE_NAME)装饰器(在你的例子中是@Processor('mail') )。 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".在您的处理器中,您可以实现一个方法,例如sendConfirmationEmail()来处理名为“confirmationEmail”的作业。 You would decorate that method with @Process(JOB_NAME) , eg @Process('confirmationEmail') .您可以使用@Process(JOB_NAME)来装饰该方法,例如@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 ).根据我的示例,以下方法签名将提供usertokenasync sendConfirmationEmail(job: Job<{ user: User, token: string }>): Promise<any> (注意Job来自bull package,而你不妨输入您的回报与使用any )。 Here is where you would actually send out the email.这是您实际发送 email 的地方。
  • In your processor class, @nestjs/bull also provides special method decorators including @OnQueueActive() , @OnQueueCompleted() , @OnQueueFailed() .在您的处理器 class 中, @nestjs/bull还提供了特殊的方法装饰器,包括@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.您的邮件模块可能至少有一个带有配置的mail.module.ts ,一个将作业添加到“邮件”队列的mail.service.ts ,以及一个mail.processor.ts完成添加到“邮件”队列。

Further documentation from NestJS is available at: NestJS 的更多文档可在以下位置获得:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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