2017-07-10 01:49:52 +08:00
# frozen_string_literal: true
2017-07-10 21:40:03 +08:00
2016-08-07 00:41:18 +08:00
require_relative " ../support/job_buffer "
require " active_support/core_ext/integer/inflections "
2016-07-30 04:54:55 +08:00
2016-08-02 07:09:16 +08:00
class DefaultsError < StandardError ; end
2019-12-18 02:23:52 +08:00
class DisabledJitterError < StandardError ; end
class ZeroJitterError < StandardError ; end
2018-06-26 06:16:58 +08:00
class FirstRetryableErrorOfTwo < StandardError ; end
class SecondRetryableErrorOfTwo < StandardError ; end
2016-08-03 05:26:56 +08:00
class LongWaitError < StandardError ; end
2016-08-02 07:09:16 +08:00
class ShortWaitTenAttemptsError < StandardError ; end
2023-09-15 23:29:30 +08:00
class PolynomialWaitTenAttemptsError < StandardError ; end
2016-08-02 07:44:29 +08:00
class CustomWaitTenAttemptsError < StandardError ; end
2016-08-02 07:51:11 +08:00
class CustomCatchError < StandardError ; end
2016-08-02 07:09:16 +08:00
class DiscardableError < StandardError ; end
2018-06-26 06:16:58 +08:00
class FirstDiscardableErrorOfTwo < StandardError ; end
class SecondDiscardableErrorOfTwo < StandardError ; end
2017-09-17 02:59:32 +08:00
class CustomDiscardableError < StandardError ; end
2021-07-29 06:32:19 +08:00
class UnlimitedRetryError < StandardError ; end
2016-07-30 04:54:55 +08:00
class RetryJob < ActiveJob :: Base
2016-08-02 07:09:16 +08:00
retry_on DefaultsError
2019-12-18 02:23:52 +08:00
retry_on DisabledJitterError , jitter : nil
retry_on ZeroJitterError , jitter : 0 . 0
2019-01-05 08:54:38 +08:00
retry_on FirstRetryableErrorOfTwo , SecondRetryableErrorOfTwo , attempts : 4
2016-08-03 05:26:56 +08:00
retry_on LongWaitError , wait : 1 . hour , attempts : 10
2016-08-02 07:09:16 +08:00
retry_on ShortWaitTenAttemptsError , wait : 1 . second , attempts : 10
2023-09-15 23:29:30 +08:00
retry_on PolynomialWaitTenAttemptsError , wait : :polynomially_longer , attempts : 10
2016-08-02 07:44:29 +08:00
retry_on CustomWaitTenAttemptsError , wait : - > ( executions ) { executions * 2 } , attempts : 10
2018-05-21 21:43:16 +08:00
retry_on ( CustomCatchError ) { | job , error | JobBuffer . add ( " Dealt with a job that failed to retry in a custom way after #{ job . arguments . second } attempts. Message: #{ error . message } " ) }
2018-07-25 16:30:38 +08:00
retry_on ( ActiveJob :: DeserializationError ) { | job , error | JobBuffer . add ( " Raised #{ error . class } for the #{ job . executions } time " ) }
2021-07-29 06:32:19 +08:00
retry_on UnlimitedRetryError , attempts : :unlimited
2018-06-26 06:16:58 +08:00
2016-08-02 07:09:16 +08:00
discard_on DiscardableError
2018-06-26 06:16:58 +08:00
discard_on FirstDiscardableErrorOfTwo , SecondDiscardableErrorOfTwo
2018-05-21 21:43:16 +08:00
discard_on ( CustomDiscardableError ) { | job , error | JobBuffer . add ( " Dealt with a job that was discarded in a custom way. Message: #{ error . message } " ) }
2016-07-30 04:54:55 +08:00
2019-01-07 21:36:56 +08:00
before_enqueue do | job |
if job . arguments . include? ( :log_scheduled_at ) && job . scheduled_at
JobBuffer . add ( " Next execution scheduled at #{ job . scheduled_at } " )
end
end
def perform ( raising , attempts , * )
2019-01-05 08:54:38 +08:00
raising = raising . shift if raising . is_a? ( Array )
if raising && executions < attempts
2016-07-30 04:54:55 +08:00
JobBuffer . add ( " Raised #{ raising } for the #{ executions . ordinalize } time " )
raise raising . constantize
else
JobBuffer . add ( " Successfully completed job " )
end
end
end