我为我的网站创建了一个批量电子邮件系统。我遇到的问题非常糟糕,它不断发送电子邮件。看来这项工作陷入了无限循环。请指教。这很疯狂,因为在我的开发服务器上,每个帐户只发送一封电子邮件,但在我的生产服务器上,我收到了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 2013-03-10 06:05:51
jvnill,我该如何防止这种情况发生,并继续发送电子邮件?根据你的说法,我认为这与交付失败有关。 – Brian 2013-03-10 06:10:44
您需要在batch_emails上添加标志,该标志跟踪电子邮件是否已处理,以便它不会再次发送。 – jvnill 2013-03-10 06:32:52