2017-01-23 24 views
1

我正在尋找一種方法,以在我的rails應用程序中delayed_job失敗時執行特定處理。我不知道是否有可能,以及如何爲此配置DelayedJob。
我看到我可以爲一項特定工作添加error方法,但我想要類似所有工作當delayed_job失敗時調用特定的方法

任何想法?

回答

1

我會建議你使用Delayed::Plugin,並按照本教程:
http://www.salsify.com/blog/engineering/delayed-jobs-callbacks-and-hooks-in-rails

您將可以觸發事件在你的應用程序的任何作業失敗。
例如:

require 'airbrake' 
 
require 'delayed_job' 
 

 
class AirbrakePlugin < Delayed::Plugin 
 

 
    callbacks do |lifecycle| 
 
    lifecycle.around(:invoke_job) do |job, *args, &block| 
 
     begin 
 
     # Forward the call to the next callback in the callback chain 
 
     block.call(job, *args) 
 
     rescue Exception => error 
 
     ::Airbrake.notify_or_ignore(
 
      :error_class => error.class.name, 
 
      :error_message => "#{error.class.name}: #{error.message}", 
 
      :backtrace => error.backtrace, 
 
      :parameters => { 
 
       :failed_job => job.inspect 
 
      } 
 
     ) 
 
     # Make sure we propagate the failure! 
 
     raise error 
 
     end 
 
    end 
 
    end

相關問題