3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Rails の ActiveJob retry_on で リトライ時の処理、リトライを諦めた時の処理をそれぞれに書く ( when surrender retry in block )

Last updated at Posted at 2019-09-21

リトライを諦めた時の処理をブロックで書ける

ブロックの中に書く。これってリトライ時の処理じゃないのか!諦めた時の処理なのか! 意外すぎた。

  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 の標準出力の例

image
image

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?