我尝试在 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:
https://firxworx.com/blog/coding/nodejs/email-module-for-nestjs-with-bull-queue-and-the-nest-mailer/
一些提示:
BullModule
(来自 @nestjs/bull
).例如,您需要配置您的队列名称(在您的情况下为邮件")并设置您的队列.常见的设置包括使用 redis 主机名和端口进行配置.user
和token
.我的示例如下所示:await this.mailQueue.add('confirmationEmail', { user, token })
@nestjs/bull
中的 @Processor(QUEUE_NAME)
装饰器装饰的类(在您的情况下为 @Processor('mail')
).处理器处理添加到队列中的作业.sendConfirmationEmail()
处理名为confirmationEmail"的作业.您可以使用 @Process(JOB_NAME)
来装饰该方法,例如@Process('confirmationEmail')
.该方法可以接收您的有效负载.根据我的示例,以下方法签名将提供 user
和 token
:async sendConfirmationEmail(job: Job<{ user: User, token: string }>): Promise<any>
(注意 Job
来自 bull
包,你可能希望输入你的回报与使用 any代码>).这是您实际发送电子邮件的地方.
@nestjs/bull
还提供了特殊的方法装饰器,包括 @OnQueueActive()
、@OnQueueCompleted()
、<代码>@OnQueueFailed().请参阅文档,但您可能会发现这些对日志记录或其他用途很有用.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.user
and token
. My example would look like this: await this.mailQueue.add('confirmationEmail', { user, token })
@Processor(QUEUE_NAME)
decorator from @nestjs/bull
(@Processor('mail')
in your case). The processor handles jobs that are added to the queue.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.@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!