Class: Retriable::Config

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/retriable/config.rb

Constant Summary collapse

ATTRIBUTES =
(ExponentialBackoff::ATTRIBUTES + %i[
  sleep_disabled
  max_elapsed_time
  intervals
  timeout
  on
  retry_if
  on_retry
  contexts
]).freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/retriable/config.rb', line 23

def initialize(opts = {})
  backoff = ExponentialBackoff.new

  @tries            = backoff.tries
  @base_interval    = backoff.base_interval
  @max_interval     = backoff.max_interval
  @rand_factor      = backoff.rand_factor
  @multiplier       = backoff.multiplier
  @sleep_disabled   = false
  @max_elapsed_time = 900 # 15 min
  @intervals        = nil
  @timeout          = nil
  @on               = [StandardError]
  @retry_if         = nil
  @on_retry         = nil
  @contexts         = {}

  opts.each do |k, v|
    raise ArgumentError, "#{k} is not a valid option" if !ATTRIBUTES.include?(k)

    instance_variable_set(:"@#{k}", v)
  end

  validate!
end

Instance Method Details

#to_hObject



49
50
51
52
53
# File 'lib/retriable/config.rb', line 49

def to_h
  ATTRIBUTES.each_with_object({}) do |key, hash|
    hash[key] = public_send(key)
  end
end

#validate!Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/retriable/config.rb', line 55

def validate!
  validate_optional_non_negative_number(:max_elapsed_time, max_elapsed_time)
  validate_optional_non_negative_number(:timeout, timeout)
  validate_intervals
  return if intervals

  validate_positive_integer(:tries, tries)
  validate_non_negative_number(:base_interval, base_interval)
  validate_non_negative_number(:multiplier, multiplier)
  validate_non_negative_number(:max_interval, max_interval)
  validate_rand_factor
end