【问题标题】:Call specific method when delayed_job faileddelay_job 失败时调用特定方法
【发布时间】:2017-01-24 00:18:47
【问题描述】:

我正在寻找一种方法来在我的 rails 应用程序上的延迟作业失败时进行特定处理。我不知道这是否可能,以及如何为此配置 DelayedJob。
我看到我可以为一项特定的工作添加 error 方法,但我希望为所有工作添加类似的方法。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby 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

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多