问题描述
在Rails中与ActionJob
接口的常见模式是使用perform()
方法设置Job,该方法通过perform_now
或perform_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的特殊情况下,由于ActionJob
与ActionMailer
集成良好,因此可以直接调用deliver_now
或deliver_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_now
和deliver_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_now
和perform_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`之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!