• <bdo id='l0pRZ'></bdo><ul id='l0pRZ'></ul>

    <i id='l0pRZ'><tr id='l0pRZ'><dt id='l0pRZ'><q id='l0pRZ'><span id='l0pRZ'><b id='l0pRZ'><form id='l0pRZ'><ins id='l0pRZ'></ins><ul id='l0pRZ'></ul><sub id='l0pRZ'></sub></form><legend id='l0pRZ'></legend><bdo id='l0pRZ'><pre id='l0pRZ'><center id='l0pRZ'></center></pre></bdo></b><th id='l0pRZ'></th></span></q></dt></tr></i><div id='l0pRZ'><tfoot id='l0pRZ'></tfoot><dl id='l0pRZ'><fieldset id='l0pRZ'></fieldset></dl></div>

      1. <small id='l0pRZ'></small><noframes id='l0pRZ'>

      2. <tfoot id='l0pRZ'></tfoot>
        <legend id='l0pRZ'><style id='l0pRZ'><dir id='l0pRZ'><q id='l0pRZ'></q></dir></style></legend>
      3. 在 Typescript 中实现 Bull Queue

        Implementing Bull Queue in Typescript(在 Typescript 中实现 Bull Queue)

        1. <i id='WRz6H'><tr id='WRz6H'><dt id='WRz6H'><q id='WRz6H'><span id='WRz6H'><b id='WRz6H'><form id='WRz6H'><ins id='WRz6H'></ins><ul id='WRz6H'></ul><sub id='WRz6H'></sub></form><legend id='WRz6H'></legend><bdo id='WRz6H'><pre id='WRz6H'><center id='WRz6H'></center></pre></bdo></b><th id='WRz6H'></th></span></q></dt></tr></i><div id='WRz6H'><tfoot id='WRz6H'></tfoot><dl id='WRz6H'><fieldset id='WRz6H'></fieldset></dl></div>

            <bdo id='WRz6H'></bdo><ul id='WRz6H'></ul>

            1. <legend id='WRz6H'><style id='WRz6H'><dir id='WRz6H'><q id='WRz6H'></q></dir></style></legend>

              <small id='WRz6H'></small><noframes id='WRz6H'>

                <tbody id='WRz6H'></tbody>
            2. <tfoot id='WRz6H'></tfoot>

                • 本文介绍了在 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:

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

                  一些提示:

                  • 在您的模块中,请务必导入您的 BullModule(来自 @nestjs/bull).例如,您需要配置您的队列名称(在您的情况下为邮件")并设置您的队列.常见的设置包括使用 redis 主机名和端口进行配置.
                  • 在您的服务中,您需要将 jobs 以及可选的 payload 添加到队列中.在您的情况下,您正在尝试添加一个功能.相反,您应该添加一个作业名称,例如confirmationEmail",并传递一个有效负载,例如usertoken.我的示例如下所示:await this.mailQueue.add('confirmationEmail', { user, token })
                  • 您需要为您的队列实现一个处理器.这是一个用 @nestjs/bull 中的 @Processor(QUEUE_NAME) 装饰器装饰的类(在您的情况下为 @Processor('mail')).处理器处理添加到队列中的作业.
                  • 在您的处理器中,您可以实现一个方法,例如sendConfirmationEmail() 处理名为confirmationEmail"的作业.您可以使用 @Process(JOB_NAME) 来装饰该方法,例如@Process('confirmationEmail').该方法可以接收您的有效负载.根据我的示例,以下方法签名将提供 usertoken:async sendConfirmationEmail(job: Job<{ user: User, token: string }>): Promise<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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <i id='xgfCa'><tr id='xgfCa'><dt id='xgfCa'><q id='xgfCa'><span id='xgfCa'><b id='xgfCa'><form id='xgfCa'><ins id='xgfCa'></ins><ul id='xgfCa'></ul><sub id='xgfCa'></sub></form><legend id='xgfCa'></legend><bdo id='xgfCa'><pre id='xgfCa'><center id='xgfCa'></center></pre></bdo></b><th id='xgfCa'></th></span></q></dt></tr></i><div id='xgfCa'><tfoot id='xgfCa'></tfoot><dl id='xgfCa'><fieldset id='xgfCa'></fieldset></dl></div>
                      • <legend id='xgfCa'><style id='xgfCa'><dir id='xgfCa'><q id='xgfCa'></q></dir></style></legend>
                          <tbody id='xgfCa'></tbody>

                        <tfoot id='xgfCa'></tfoot>
                          • <bdo id='xgfCa'></bdo><ul id='xgfCa'></ul>

                            <small id='xgfCa'></small><noframes id='xgfCa'>