【问题标题】:exception_notification for delayed_job延迟作业的异常通知
【发布时间】:2011-05-05 11:34:42
【问题描述】:

delayed_job 是否有类似 exception_notification 的 gem? 最好与 REE-1.8.7 和 Rails 2.3.10 一起使用。

【问题讨论】:

  • 使用 Resque 时,this little gem 通过 exception_notification 报告异常。

标签: ruby-on-rails ruby exception delayed-job exception-notification


【解决方案1】:

我过去曾为延迟的工作抽查任务做过类似的事情:

require 'action_mailer'
class ExceptionMailer < ActionMailer::Base
  def setup_mail
    @from = ExceptionNotifier.sender_address
    @sent_on = Time.now
    @content_type = "text/plain"
  end

  def exception_message(subject, message)
    setup_mail
    @subject = subject
    @recipients = ExceptionNotifier.exception_recipients
    @body = message
  end
end

namespace :jobs do
desc "sync the local database with the remote CMS"
task(:sync_cms => :environment) do
  Resort.sync_all!
  result = Delayed::Job.work_off
  unless result[1].zero?
    ExceptionMailer.deliver_exception_message("[SYNC CMS] Error syncing CMS id: #{Delayed::Job.last.id}", Delayed::Job.last.last_error)
  end
end

结束

【讨论】:

    【解决方案2】:

    将此模块包含在要延迟的类中:

    
    require 'exception_notifier'
    module Delayed
      module ExceptionNotifier
        # Send error via exception notifier
        def error(job, e)
          env = {}
          env['exception_notifier.options'] = {
            :sections => %w(backtrace delayed_job),
            :email_prefix => '[Delayed Job ERROR] ',
            :exception_recipients => %w(some@email.com),
            :sender_address => %(other@email.com)
          }
          env['exception_notifier.exception_data'] = {:job => job}
          ::ExceptionNotifier::Notifier.exception_notification(env, e).deliver
        end
      end
    end
    

    并在 app/views/exception_notifier/_delayed_job.text.erb 中为通知创建模板:

    
    Job name: <%= @job.name %>
    Job: <%= raw @job.inspect %>
    
    * Process: <%= raw $$ %>
    * Server : <%= raw `hostname -s`.chomp %>
    

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2011-06-08
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2022-11-10
      • 2015-10-02
      相关资源
      最近更新 更多