Class: Unity::Utils::Retrier

Inherits:
Object
  • Object
show all
Defined in:
lib/unity/utils/retrier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = nil, max_retries = 5, sleep_factor = 0.3) ⇒ Retrier

Returns a new instance of Retrier.



12
13
14
15
16
# File 'lib/unity/utils/retrier.rb', line 12

def initialize(errors = nil, max_retries = 5, sleep_factor = 0.3)
  @errors = errors || [StandardError]
  @max_retries = max_retries
  @sleep_factor = sleep_factor
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/unity/utils/retrier.rb', line 6

def errors
  @errors
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



6
7
8
# File 'lib/unity/utils/retrier.rb', line 6

def max_retries
  @max_retries
end

#sleep_factorObject (readonly)

Returns the value of attribute sleep_factor.



6
7
8
# File 'lib/unity/utils/retrier.rb', line 6

def sleep_factor
  @sleep_factor
end

Class Method Details

.callObject



8
9
10
# File 'lib/unity/utils/retrier.rb', line 8

def self.call
  new.call(&Proc.new) if block_given?
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/unity/utils/retrier.rb', line 18

def call
  return unless block_given?

  retries = 0

  begin
    yield
  rescue *errors => e
    retries += 1
    raise e if retries > max_retries

    sleep_on(retries)
    retry
  end
end