delayed_job 失败时调用特定方法
Call specific method when delayed_job failed
当 delayed_job 在我的 rails 应用程序上失败时,我正在寻找一种方法来进行特定处理。我不知道这是否可行,以及如何为此配置 DelayedJob。
我看到我可以为一项特定的工作添加 error
方法,但我想要对 所有工作 .
类似的方法
有什么想法吗?
我建议您使用 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
当 delayed_job 在我的 rails 应用程序上失败时,我正在寻找一种方法来进行特定处理。我不知道这是否可行,以及如何为此配置 DelayedJob。
我看到我可以为一项特定的工作添加 error
方法,但我想要对 所有工作 .
有什么想法吗?
我建议您使用 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