2011-11-22 96 views
2

是否有處理delayed_job創業板錯誤的可能性?我希望找到一種方法(運行時)在發生錯誤時將電子郵件發送到給定帳戶,並在執行後刪除它們。處理延遲作業錯誤

回答

1

這是你想要的嗎?

def do_something 
    # some code here 
    begin 
    # something that could go wrong here 
    rescue Exception => e 
    YourMailer.delay.your_method(e.to_s) 
    # don't know what you want to delete, but you can do it here 
    end 
end 
1

找到了解決辦法在這裏:http://blog.salsify.com/engineering/delayed-jobs-callbacks-and-hooks-in-rails

# config/initizalizers/delayed_job.rb 

class ExceptionMailerPlugin < 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 
     ErrorMailer.exception_mail(error) 
     # Make sure we propagate the failure! 
     raise error 
     end 
    end 
    end 

end 


Delayed::Worker.plugins << ExceptionMailerPlugin