动作作业/邮件程序的`deliver_now`和`deliver_later`之间的区别 [英] Difference between Action Job/Mailer's `deliver_now` and `deliver_later`

查看:158
本文介绍了动作作业/邮件程序的`deliver_now`和`deliver_later`之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails中与ActionJob接口的常见模式是使用perform()方法设置Job,该方法通过perform_nowperform_later

The common pattern for interfacing with ActionJob in Rails is to set up a Job with a perform() method that gets called asynchronously via perform_now or perform_later

在Mailers的特殊情况下,由于ActionJobActionMailer集成良好,因此可以直接调用deliver_nowdeliver_later.

In the special case of Mailers, you can directly call deliver_now or deliver_later since ActionJob is well integrated with ActionMailer.

rails文档具有以下注释-

# If you want to send the email now use #deliver_now
UserMailer.welcome(@user).deliver_now

# If you want to send the email through Active Job use #deliver_later
UserMailer.welcome(@user).deliver_later

该措辞使deliver_now似乎使用ActiveJob发送邮件.是正确的吗?如果是,deliver_nowdeliver_later之间的真正区别是什么?一个不是异步的吗?

The wording makes it seem like deliver_now will not use ActiveJob to send the mail. Is that correct, and if so what's the true difference between deliver_now and deliver_later? Is one not asynchronous?

类似地,perform_nowperform_later是否具有相同的区别?

Similarly, does the same difference apply to perform_now and perform_later ?

谢谢!

推荐答案

正如您在问题中所说,deliver_now不使用ActiveJob.

As you say in your question, deliver_now does not use ActiveJob.

基本上,deliver_later是异步的.使用此方法时,当前不会发送电子邮件,而是将其发送到作业队列中.如果作业未运行,则不会发送电子邮件.无论工作状态如何,deliver_now都会立即发送电子邮件. 此处,您可以查看deliver方法的文档

Basically, deliver_later is asynchronous. When you use this method, the email is not send at the moment, but rather is pushed in a job's queue. If the job is not running, the email will not be sent. deliver_now will send the email at the moment, no matter what is the job's state. Here you can see the documentation for deliver methods.

根据第二个问题,perform_now将立即处理作业,而不发送到队列.但是,perform_later会将作业添加到队列中,并且该作业的队列空闲时将立即执行该作业. 此处,您可以查看perform方法的文档.

According to your second question, perform_now will process the job immediately without sending to the queue. perform_later, however, will add the job to the queue, and as soon the job's queue is free, will perform the job. Here you can see the documentation for perform methods.

这篇关于动作作业/邮件程序的`deliver_now`和`deliver_later`之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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