首先,我定义了一个邮件程序方法
class UserMailer < Devise::Mailer
# customize mail format
def user_feedback(opts={})
@v = "Hello"
@opts = opts
mail(:to => "xxx@gmail.com", :bcc => ["yyy@gmail.com"], :subject => "Test")
end
handle_asynchronously :user_feedback
end
而且,我在请求中调用此方法
class MailsController < ApplicationController
def feedback
options[:feedback_time] = Time.now.strftime("%Y%m%d %H:%M:%S")
UserMailer.user_feedback(options).deliver
end
end
然后,我就像这样启动delay_job工作者
rake jobs:workoff
这是日志
[Worker(host:localhost.localdomain pid:2809)] Starting job worker
[Worker(host:localhost.localdomain pid:2809)] Job Delayed::PerformableMethod (id=11) RUNNING
[Worker(host:localhost.localdomain pid:2809)] No more jobs available. Exiting
[Worker(host:localhost.localdomain pid:2208)] Starting job worker
[Worker(host:localhost.localdomain pid:2208)] Job Class#user_feedback (id=1) RUNNING
[Worker(host:localhost.localdomain pid:2208)] Job Class#user_feedback (id=1) COMPLETED after 0.0819
[Worker(host:localhost.localdomain pid:2208)] Job Delayed::PerformableMethod (id=2) RUNNING
[Worker(host:localhost.localdomain pid:2208)] 1 jobs processed at 2.7340 j/s, 0 failed
[Worker(host:localhost.localdomain pid:2208)] No more jobs available. Exiting
所有这些都已完成,但邮件未发送!
答案 0 :(得分:2)
请勿在您调用UserMailer的方法之前调用.deliver并添加.delay
请参阅:https://github.com/collectiveidea/delayed_job#rails-3-mailers
例如:
UserMailer.delay.user_feedback(options)
答案 1 :(得分:0)
在这里,我想我需要分享一下如何获得正确答案的经验
当我们对方法使用handle_asynchronously
时,它会向方法调用alias_method_chain
,就像问题中的方法'user_feedback'一样。
访问https://github.com/collectiveidea/delayed_job/blob/44edd9473adbc415a3e51ead60a63107a9ad41e4/lib/delayed/message_sending.rb了解详情
当delayed_job工作人员处理延迟发送电子邮件作业时,它会调用动态方法,例如 user_feedback_without_delay 。现在问题来了,如果我们没有明确的话 指出邮件模板。它会尝试找到像 user_feedback_without_delay.html.erb 这样的模板。
所以,我们应该做的就是像这样明确地给出模板
mail(:to => "xxx@gmail.com", :bcc => ["yyy@gmail.com"], :subject => "Test", template_name: 'user_feedback')