【发布时间】:2018-09-05 21:19:44
【问题描述】:
控制器操作调用TextMailer.contact(fg, mailing).deliver_now,但是需要在确定的时间使用 resque-scheduler gem 将其移动到后台作业中。
因此控制器动作现在会调用:
Resque.enqueue_at(@time, DelayedMailer, @fg.id, @mailing.id)
使用 Resque 设置了一个新的 rake 任务...
task "resque:setup" => :environment do
Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
ENV['QUEUES'] = *
end
运行delayed_mailer 作业
class DelayedMailer
@queue = :mail
def self.perform(time, fg_id, mailing_id)
fg = Fg.where('id = ?', fg_id).first
mailing = Mailing.where('id = ?', mailing_id).first
TextMailer.contact(fg, mailing).deliver_now
end
有两个句法元素需要澄清。
1) perform 方法是否需要调用时间值(这似乎违反直觉,因为使用enqueue_at 调用 Resque 显式给出了一个时间键,这隐含地不需要重复)?
2) ActionMailer 方法是否可以在没有进一步更改的情况下被调用,就像它之前运行的那样,或者队列是否会以某种方式中断某些逻辑?
【问题讨论】:
-
我建议您阅读有关 ActiveJob 的内容,您可以将 Resque 配置为后端,并且 ActionMailer 将支持自动使用 Resque 的异步交付方法。
-
嗯,是的,我有。 rails 指南指出“您可以使用任意数量的参数定义 perform”......虽然
resque-schedulergithub 页面定义了 enqueue_at,但它没有说明是否需要在perform中调用原始的 at-time 调用(我假设不是因为作业已经存储在那个点“延迟队列存储在redis中并被持久化”)。
标签: ruby-on-rails resque-scheduler