Class: Async::Background::Job::Options

Inherits:
Data
  • Object
show all
Defined in:
lib/async/background/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: DEFAULT_TIMEOUT, retry: 0, retry_delay: nil, backoff: :fixed, attempt: nil, jitter: nil) ⇒ Options

Returns a new instance of Options.

Raises:

  • (ArgumentError)


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

#attemptObject (readonly)

Returns the value of attribute attempt

Returns:

  • (Object)

    the current value of attempt



10
11
12
# File 'lib/async/background/job.rb', line 10

def attempt
  @attempt
end

#backoffObject (readonly)

Returns the value of attribute backoff

Returns:

  • (Object)

    the current value of backoff



10
11
12
# File 'lib/async/background/job.rb', line 10

def backoff
  @backoff
end

#jitterObject (readonly)

Returns the value of attribute jitter

Returns:

  • (Object)

    the current value of jitter



10
11
12
# File 'lib/async/background/job.rb', line 10

def jitter
  @jitter
end

#retryObject (readonly)

Returns the value of attribute retry

Returns:

  • (Object)

    the current value of retry



10
11
12
# File 'lib/async/background/job.rb', line 10

def retry
  @retry
end

#retry_delayObject (readonly)

Returns the value of attribute retry_delay

Returns:

  • (Object)

    the current value of retry_delay



10
11
12
# File 'lib/async/background/job.rb', line 10

def retry_delay
  @retry_delay
end

#timeoutObject (readonly)

Returns the value of attribute timeout

Returns:

  • (Object)

    the current value of timeout



10
11
12
# File 'lib/async/background/job.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#attempts_madeObject



33
# File 'lib/async/background/job.rb', line 33

def attempts_made   = attempt || 0

#next_attemptObject



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

Raises:

  • (ArgumentError)


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

Returns:

  • (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))