在Typescript中实现Bull Queue [英] Implementing Bull Queue in Typescript

查看:156
本文介绍了在Typescript中实现Bull Queue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Typescript和NestJS中实现Bull队列,我的代码:

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:

一些提示:

  • 在您的模块中,请确保导入您的 BullModule (从 @ nestjs/bull ).例如,您需要配置队列名称(在您的情况下为"mail")并设置队列.常见的设置包括使用redis主机名和端口进行配置.
  • 在您的服务中,您需要将 jobs 与可选的 payload 一起添加到队列中.在您的情况下,您正在尝试添加功能.相反,您应该添加工作名称,例如"confirmationEmail",并传递有效负载,例如 user token .我的示例如下所示:等待this.mailQueue.add('confirmationEmail',{用户,令牌})
  • 您需要为队列实现处理器.这是一个用 @ nestjs/bull (在您的情况下为 @Processor('mail'))中的 @Processor(QUEUE_NAME)装饰器修饰的类).处理器处理添加到队列中的作业.
  • 在处理器中,您可以实现例如 sendConfirmationEmail()处理名为"confirmationEmail"的作业.您可以使用 @Process(JOB_NAME)装饰该方法,例如 @Process('confirmationEmail').该方法可以接收您的有效载荷.以我的示例为例,以下方法签名将提供 user token : async sendConfirmationEmail(job:Job< {user:User,token:string}>):承诺< any> (请注意 Job 来自 bull 包,并且您可能希望使用 any ).这是您实际发送电子邮件的地方.
  • 在处理器类中, @ nestjs/bull 还提供特殊的方法修饰符,包括 @OnQueueActive() @OnQueueCompleted() @OnQueueFailed().请参阅文档,但您可能会发现这些对于日志记录或其他用途很有用.
  • 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, e.g. "confirmationEmail", and pass a payload, e.g.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 e.g. sendConfirmationEmail() that handles the job named "confirmationEmail". You would decorate that method with @Process(JOB_NAME), e.g. @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.

您的邮件模块可能至少具有配置的 mail.module.ts mail.service.ts ,用于将作业添加到邮件"目录中.队列和 mail.processor.ts 负责完成添加到邮件"邮件中的所有作业.队列.

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.

NestJS的其他文档可在以下位置找到:

Further documentation from NestJS is available at:

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

这篇关于在Typescript中实现Bull Queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆