跳至内容 跳至搜索
方法
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 131
def after_discard(&blk)
  self.after_discard_procs += [blk]
end

discard_on(*exceptions, report: false)

如果引发了异常,则丢弃作业,不进行任何重试。这在作业的主题(如 Active Record)不再可用,并且作业因此不再相关时非常有用。

传递 :report 选项会在丢弃作业之前通过错误报告器报告错误。

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

retry_ondiscard_on 处理程序从下往上以及沿着类层次结构进行搜索。第一个满足 exception.is_a?(klass) 的类的处理程序将被调用(如果存在)。

示例

class SearchIndexingJob < ActiveJob::Base
  discard_on ActiveJob::DeserializationError
  discard_on CustomAppException, report: true
  discard_on(AnotherCustomAppException) do |job, error|
    CustomErrorHandlingCode.handle(job, 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 109
def discard_on(*exceptions, report: false)
  rescue_from(*exceptions) do |error|
    instrument :discard, error: error do
      ActiveSupport.error_reporter.report(error, source: "application.active_job") if report
      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, report: false)

捕获异常并在指定秒数后、指定次数的尝试后重新调度作业进行重新执行。如果异常在超出指定尝试次数后仍然引发,则允许异常冒泡到底层排队系统,该系统可能有自己的重试机制或将其放入持有队列进行检查。

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

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),代表可能等待时间的上限(表示为百分比)。

  • :report - 在重试之前,错误将报告给 Rails.error 报告器。

示例

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, report: true
  retry_on EvenWorseCustomAppException do |job, error|
    CustomErrorHandlingCode.handle(job, 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 64
def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: JITTER_DEFAULT, report: false)
  rescue_from(*exceptions) do |error|
    executions = executions_for(exceptions)
    if attempts == :unlimited || executions < attempts
      ActiveSupport.error_reporter.report(error, source: "application.active_job") if report
      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