Class: Async::Background::Job::Options
- Inherits:
-
Data
- Object
- Data
- Async::Background::Job::Options
- Defined in:
- lib/async/background/job.rb
Instance Attribute Summary collapse
-
#attempt ⇒ Object
readonly
Returns the value of attribute attempt.
-
#backoff ⇒ Object
readonly
Returns the value of attribute backoff.
-
#jitter ⇒ Object
readonly
Returns the value of attribute jitter.
-
#retry ⇒ Object
readonly
Returns the value of attribute retry.
-
#retry_delay ⇒ Object
readonly
Returns the value of attribute retry_delay.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #attempts_made ⇒ Object
-
#initialize(timeout: DEFAULT_TIMEOUT, retry: 0, retry_delay: nil, backoff: :fixed, attempt: nil, jitter: nil) ⇒ Options
constructor
A new instance of Options.
- #next_attempt ⇒ Object
- #next_retry_delay(for_attempt = next_attempt, rng: Random) ⇒ Object
- #retry? ⇒ Boolean
- #with_attempt(n) ⇒ Object
Constructor Details
#initialize(timeout: DEFAULT_TIMEOUT, retry: 0, retry_delay: nil, backoff: :fixed, attempt: nil, jitter: nil) ⇒ Options
Returns a new instance of Options.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/async/background/job.rb', line 11 def initialize(timeout: DEFAULT_TIMEOUT, retry: 0, retry_delay: nil, backoff: :fixed, attempt: nil, jitter: nil) timeout = Integer(timeout) retries = Integer(binding.local_variable_get(:retry) || 0) retry_delay = Float(retry_delay) unless retry_delay.nil? backoff = backoff.to_sym attempt = Integer(attempt) unless attempt.nil? jitter = Float(jitter) unless jitter.nil? raise ArgumentError, "timeout must be > 0" unless timeout.positive? raise ArgumentError, "retry must be >= 0" if retries.negative? raise ArgumentError, "attempt must be >= 0" if attempt && attempt.negative? raise ArgumentError, "backoff must be one of #{BACKOFFS.inspect}" unless BACKOFFS.include?(backoff) if retries.positive? raise ArgumentError, "retry_delay is required when retry > 0" if retry_delay.nil? raise ArgumentError, "retry_delay must be > 0" unless retry_delay.positive? end raise ArgumentError, "jitter must be in [0, 1]" if jitter && !(0.0..1.0).cover?(jitter) super(timeout:, retry: retries, retry_delay:, backoff:, attempt:, jitter:) end |
Instance Attribute Details
#attempt ⇒ Object (readonly)
Returns the value of attribute attempt
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def attempt @attempt end |
#backoff ⇒ Object (readonly)
Returns the value of attribute backoff
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def backoff @backoff end |
#jitter ⇒ Object (readonly)
Returns the value of attribute jitter
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def jitter @jitter end |
#retry ⇒ Object (readonly)
Returns the value of attribute retry
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def retry @retry end |
#retry_delay ⇒ Object (readonly)
Returns the value of attribute retry_delay
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def retry_delay @retry_delay end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout
10 11 12 |
# File 'lib/async/background/job.rb', line 10 def timeout @timeout end |
Instance Method Details
#attempts_made ⇒ Object
33 |
# File 'lib/async/background/job.rb', line 33 def attempts_made = attempt || 0 |
#next_attempt ⇒ Object
34 |
# File 'lib/async/background/job.rb', line 34 def next_attempt = attempts_made + 1 |
#next_retry_delay(for_attempt = next_attempt, rng: Random) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/async/background/job.rb', line 36 def next_retry_delay(for_attempt = next_attempt, rng: Random) raise ArgumentError, "attempt must be > 0" unless for_attempt.positive? raise ArgumentError, "retry_delay must be configured" if retry_delay.nil? base = case backoff when :fixed then retry_delay when :linear then retry_delay * for_attempt when :exponential then retry_delay * (2**(for_attempt - 1)) end factor = jitter || DEFAULT_JITTER_FOR[backoff] factor.zero? ? base : base * (1 + rng.rand * factor) end |
#retry? ⇒ Boolean
32 |
# File 'lib/async/background/job.rb', line 32 def retry? = self.retry.positive? |
#with_attempt(n) ⇒ Object
35 |
# File 'lib/async/background/job.rb', line 35 def with_attempt(n) = with(attempt: Integer(n)) |