【问题标题】:Rails Delayed Job Continuously RunningRails延迟作业连续运行
【发布时间】:2013-06-04 09:08:40
【问题描述】:

我为我的网站创建了一个批处理电子邮件系统。我遇到的可怕问题是它不断地发送电子邮件。看来这项工作陷入了无限循环。请指教。这很疯狂,因为在我的开发服务器上,每个帐户只发送一封电子邮件,但在我的生产服务器上,我收到了 5 封电子邮件。因此,这意味着我网站的所有用户都收到了多封电子邮件。

控制器:

 class BatchEmailsController < ApplicationController

 before_filter :authenticate_admin_user!

 def deliver
 flash[:notice] = "Email Being Delivered"
 Delayed::Job.enqueue(BatchEmailJob.new(params[:batch_email_id]), 3, 10.seconds.from_now, :queue => 'batch-email', :attempts => 0)
 redirect_to admin_batch_emails_path
end
end

lib 文件夹中的作业:

 class BatchEmailJob < Struct.new(:batch_email_id)
 def perform
 be = BatchEmail.find(batch_email_id)
 if be.to.eql?("Contractors")
   cs = Contractor.all
   cs.each do|c|
     begin
        BatchEmailMailer.batch_email(be.subject, be.message, be.link_name, be.link_path, be.to, c.id).deliver
     rescue Exception => e
          Rails.logger.warn "Batch Email Error: #{e.message}"
     end
 else
   ps = Painter.all
   ps.each do |p|
     begin
       BatchEmailMailer.batch_email(be.subject, be.message, be.link_name, be.link_path, be.to, p.id).deliver
     rescue Exception => e
       Rails.logger.warn "Batch Email Error: #{e.message}"
     end
   end
 end
 end
 end

延迟作业初始化器:

 Delayed::Worker.max_attempts = 0

请提供有关此方法的反馈。我想向所有用户发送批量电子邮件,但如果出现问题,请避免多次重试。我添加了救援块来捕获电子邮件异常,希望批处理会跳过错误并继续处理。作为最后的手段,如果出现其他问题,请不要再次运行。

【问题讨论】:

  • 作业可能在某处遇到错误,导致作业重新启动。
  • jvnill,我怎样才能防止这种情况发生并继续发送电子邮件?根据您所说,我假设这与交付失败有关。
  • 您需要在 batch_emails 上添加标志,用于跟踪电子邮件是否已被处理,以便不会再次发送。
  • 嗨,我不能在初始化程序中添加以下内容:Delayed::Worker.max_attempts = 1
  • 是的,这也是一个可能的解决方案。请注意,在电子邮件发生后应该发送的任何电子邮件都不会发送出去

标签: ruby-on-rails delayed-job


【解决方案1】:

在收到数百万封电子邮件后,我的一款应用似乎可以完美运行:

1) 在初始化程序中,不要让 DelayedJob 重新尝试失败的作业,也不要让 DJ 删除失败的作业:

Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.max_attempts = 1

2) 安排群发电子邮件是一项工作,也就是“主要工作”

3) 当 THAT 作业运行时,它会生成 N 个作业,其中 N 是正在发送的电子邮件数量。因此,每封电子邮件都有自己的工作。 (注意:如果您使用具有“批量”功能的生产电子邮件服务,则一个“电子邮件”实际上可能是一批 100 或 1000 封电子邮件。)

4) 我们有一个管理面板,它会显示我们是否有任何失败的作业,如果是,因为我们没有删除它们,我们可以检查失败的作业并查看发生了什么(格式错误的电子邮件地址等)

如果一封电子邮件失败,其他电子邮件不受影响。而且任何电子邮件都不能发送两次。

【讨论】:

    猜你喜欢
    • 2013-12-07
    • 1970-01-01
    • 2014-01-01
    • 2011-02-12
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2012-11-05
    • 2023-04-03
    相关资源
    最近更新 更多