跳至内容 跳至搜索
方法
A
D
R

实例公共方法

after_discard(&blk)

当作业即将因任何原因被丢弃时运行的代码块。

示例

class WorkJob < ActiveJob::Base
  after_discard do |job, exception|
    ExceptionNotifier.report(exception)
  end

  ...

end
# File activejob/lib/active_job/exceptions.rb, line 124
def after_discard(&blk)
  self.after_discard_procs += [blk]
end

discard_on(*exceptions)

如果抛出异常,则丢弃作业,不尝试重新执行。当作业的主题(如ActiveRecord)不再可用,并且作业不再相关时,这很有用。

您也可以传递一个将被调用的代码块。此代码块将以作业实例作为第一个参数和错误实例作为第二个参数传递。

retry_ondiscard_on 处理程序从底部到顶部以及类层次结构向上搜索。第一个满足 exception.is_a?(klass) 条件的类的处理程序将被调用,如果有的话。

示例

class SearchIndexingJob < ActiveJob::Base
  discard_on ActiveJob::DeserializationError
  discard_on(CustomAppException) do |job, error|
    ExceptionNotifier.caught(error)
  end

  def perform(record)
    # Will raise ActiveJob::DeserializationError if the record can't be deserialized
    # Might raise CustomAppException for something domain specific
  end
end
# File activejob/lib/active_job/exceptions.rb, line 103
def discard_on(*exceptions)
  rescue_from(*exceptions) do |error|
    instrument :discard, error: error do
      yield self, error if block_given?
      run_after_discard_procs(error)
    end
  end
end

retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: JITTER_DEFAULT)

捕获异常并重新安排作业在几秒钟后重新执行,执行指定次数。如果异常持续出现,超过指定的尝试次数,则允许异常冒泡到底层的排队系统,该系统可能具有自己的重试机制,或将其放入等待队列以供检查。

您也可以传递一个代码块,如果重试尝试失败,则调用它以进行自定义逻辑,而不是让异常冒泡。此代码块将以作业实例作为第一个参数和错误实例作为第二个参数传递。

retry_ondiscard_on 处理程序从底部到顶部以及类层次结构向上搜索。第一个满足 exception.is_a?(klass) 条件的类的处理程序将被调用,如果有的话。

选项

  • :wait - 重新排队作业,延迟以秒为单位(默认:3 秒),作为计算过程,该过程以执行次数作为参数,或作为:polynomially_longer 的符号引用,该引用应用了((executions**4) + (Kernel.rand * (executions**4) * jitter)) + 2 的等待算法(第一次等待约 3 秒,然后约 18 秒,然后约 83 秒,等等)

  • :attempts - 将作业排队指定的次数(默认:5 次尝试)或:unlimited 的符号引用,以重试作业直到成功。尝试次数包括原始作业执行。

  • :queue - 将作业重新排队到不同的队列

  • :priority - 将作业重新排队到不同的优先级

  • :jitter - 计算回退时使用的随机延迟等待时间。默认值为 15% (0.15),表示可能等待时间的上限(以百分比表示)

示例

class RemoteServiceJob < ActiveJob::Base
  retry_on CustomAppException # defaults to ~3s wait, 5 attempts
  retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 }
  retry_on CustomInfrastructureException, wait: 5.minutes, attempts: :unlimited

  retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
  retry_on Net::OpenTimeout, Timeout::Error, wait: :polynomially_longer, attempts: 10 # retries at most 10 times for Net::OpenTimeout and Timeout::Error combined
  # To retry at most 10 times for each individual exception:
  # retry_on Net::OpenTimeout, wait: :polynomially_longer, attempts: 10
  # retry_on Net::ReadTimeout, wait: 5.seconds, jitter: 0.30, attempts: 10
  # retry_on Timeout::Error, wait: :polynomially_longer, attempts: 10

  retry_on(YetAnotherCustomAppException) do |job, error|
    ExceptionNotifier.caught(error)
  end

  def perform(*args)
    # Might raise CustomAppException, AnotherCustomAppException, or YetAnotherCustomAppException for something domain specific
    # Might raise ActiveRecord::Deadlocked when a local db deadlock is detected
    # Might raise Net::OpenTimeout or Timeout::Error when the remote service is down
  end
end
# File activejob/lib/active_job/exceptions.rb, line 62
def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: JITTER_DEFAULT)
  rescue_from(*exceptions) do |error|
    executions = executions_for(exceptions)
    if attempts == :unlimited || executions < attempts
      retry_job wait: determine_delay(seconds_or_duration_or_algorithm: wait, executions: executions, jitter: jitter), queue: queue, priority: priority, error: error
    else
      if block_given?
        instrument :retry_stopped, error: error do
          yield self, error
        end
        run_after_discard_procs(error)
      else
        instrument :retry_stopped, error: error
        run_after_discard_procs(error)
        raise error
      end
    end
  end
end