#Rails의 ActiveJob retryon에 각각 재시도 처리, 재시도 취소 처리 쓰기 (when surrender retry in block)

6351 단어 Rails

다시 시도할 때의 처리를 네모난 상자로 쓸 수 있습니다


네모난 테두리에 쓰다.이것은 재시도할 때의 처리가 아니냐!포기할 때의 처리인가!너무 뜻밖이야.
  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_가까스로 온 덮어쓰기 방법을 시도하면 먼저 움직인다.
# 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

좋은 웹페이지 즐겨찾기