我收到一个奇怪的错误:'使用MyMailer.delay.mailer_action(args)延迟发送电子邮件。'但我似乎可以找出它来自哪里:
app/mailers/user_notifier.rb
class UserNotifier < ActionMailer::Base
default from: "test@testing.com"
def signup_email(user)
@user = user
mail(to: @user.email, subject: t("mailer.signup.subject"))
end
...
end
在控制台中,未延迟的工作:
UserNotifier.signup_email(user).deliver
但添加延迟时会发生错误:
UserNotifier.signup_email(user).delay.deliver
Rendered user_notifier/signup_email.html.erb (0.7ms)
RuntimeError: Use MyMailer.delay.mailer_action(args) to delay sending of emails.
from /Users/quindici/.rvm/gems/ruby-2.1.2/gems/delayed_job-4.0.6/lib/delayed/performable_mailer.rb:20:in `delay'
from (irb):6
from /Users/quindici/.rvm/gems/ruby-2.1.2/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/quindici/.rvm/gems/ruby-2.1.2/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/quindici/.rvm/gems/ruby-2.1.2/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
过去曾经有过工作,但有时候却被打破了。仍然没有理解我的头脑。任何想法将不胜感激!谢谢。
答案 0 :(得分:2)
我认为你倒退了:
UserNotifier.signup_email(user).delay.deliver
应该是
UserNotifier.delay.signup_email(user)
答案 1 :(得分:2)
当您使用Delayed_job时,您可以访问.dealy
方法,然后您可以使用.deliver_later
之类的操作邮件方法来延迟发送电子邮件。
在项目中添加delayed_job的简单方法:
添加你的Gemfile:
gem "delayed_job_active_record"
gem "daemons"
使用以下命令创建文件“config / initializers / delayed_job_config.rb”:
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes
Delayed::Worker.read_ahead = 10
Delayed::Worker.default_queue_name = 'default'
Delayed::Worker.delay_jobs = !Rails.env.test?
Delayed::Worker.raise_signal_exceptions = :term
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
在终端:
$ bin/rails generate delayed_job:active_record
$ bin/rake db:migrate
$ bundle
$ bin/delayed_job start
查看日志的可选项:tail -f log/delayed_job.log
在项目中的某个Mailer中(您可以在rails控制台中执行此操作):
SomeMailer.delay.some_method
您的队列将在60秒内处理完毕。您可以删除属性Delayed::Worker.sleep_delay = 60
以立即处理。
否则,您可以将应用程序配置为使用delayed_job(如适配器)作为活动作业,并在后台处理。然后你需要放config.active_job.queue_adapter = :delayed_job
。此模式始终会发送您不需要使用.delay
的电子邮件,而是调用邮件程序操作和.deliver_later
,例如:MyMailer.your_method.deliver_later
。在此模式下,活动作业会将电子邮件排入delayed_job。
欢迎提出问题和建议。