0

控制器操作调用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 方法是否需要调用时间值(这似乎违反直觉,因为调用 Resqueenqueue_at显式给出了一个时间键,这隐含地不需要重复)?

2) ActionMailer 方法是否可以在没有进一步更改的情况下被调用,就像它之前运行的那样,或者队列是否以某种方式中断了一些逻辑?

4

1 回答 1

1

您可以配置 resque 以使用 ActionMailer。

  1. 添加gem 'resque'到您的 gemfile。
  2. 更改适配器application.rb-config.active_job.queue_adapter = :resque
  3. 使用以下内容生成作业 -rails g job SendEmail

class SendEmail< ActiveJob::Base
  queue_as :default

  def perform(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
end

在您的控制器中,您可以执行

SendEmail.set(wait: 10.seconds).perform_later(@fg.id, @mailing.id)
于 2018-09-06T06:35:59.940 回答