リトライを諦めた時の処理をブロックで書ける
ブロックの中に書く。これってリトライ時の処理じゃないのか!諦めた時の処理なのか! 意外すぎた。
  retry_on SomeError, wait: 3.seconds, attempts: 3 do |job, exception|
    puts "JOB SURRENDERD!"
  end
retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)Link
Catch the exception and reschedule job for re-execution after so many seconds, for a specific number of attempts. If the exception keeps getting raised beyond the specified number of attempts, the exception is allowed to bubble up to the underlying queuing system, which may have its own retry mechanism or place it in a holding queue for inspection.You can also pass a block that'll be invoked if the retry attempts fail for custom logic rather than letting the exception bubble up. This block is yielded with the job instance as the first and the error instance as the second parameter.
リトライ時にも好きな処理をさせたい
retry_on メソッドを無理やり上書きしてみると、とりあえず動いた。
# https://api.rubyonrails.org/v6.0.0/classes/ActiveJob/Exceptions/ClassMethods.html
# https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activejob/lib/active_job/exceptions.rb#L50
class SomeJob < ApplicationJob
  queue_as :default
  class SomeError < StandardError; end
  def self.retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil)
    rescue_from(*exceptions) do |error|
      executions = executions_for(exceptions)
      if executions < attempts
        # https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activejob/lib/active_job/exceptions.rb#L50
       # ここに puts を挟んだだけ
        puts "RETRY!"
        retry_job wait: determine_delay(seconds_or_duration_or_algorithm: wait, executions: executions), queue: queue, priority: priority, error: error
      else
        if block_given?
          instrument :retry_stopped, error: error do
            yield self, error
          end
        else
          instrument :retry_stopped, error: error
          raise error
        end
      end
    end
  end
  retry_on SomeError, wait: 3.seconds, attempts: 3 do |job, exception|
    puts "JOB SURRENDERD!"
  end
  def perform(message)
    raise SomeError.new('Watch out!')
  end
end
Worker の標準出力の例
Original by Github issue
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

