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

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

unbounded_tries?

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/retriable/config.rb', line 36

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

Class Attribute Details

.timeout_deprecation_warnedObject

Returns the value of attribute timeout_deprecation_warned.



31
32
33
# File 'lib/retriable/config.rb', line 31

def timeout_deprecation_warned
  @timeout_deprecation_warned
end

Instance Method Details

#to_hObject



62
63
64
65
66
# File 'lib/retriable/config.rb', line 62

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

#validate!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/retriable/config.rb', line 68

def validate!
  warn_timeout_deprecation
  validate_optional_non_negative_number(:timeout, timeout)
  validate_on(on)
  validate_intervals
  if unbounded_tries?(tries)
    validate_unbounded_tries
  else
    validate_optional_non_negative_number(:max_elapsed_time, max_elapsed_time)
    return if intervals

    validate_positive_integer(:tries, tries)
  end

  validate_backoff_options
end