Class: Tretry

Inherits:
Object
  • Object
show all
Defined in:
lib/tretry.rb

Overview

A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors: nil, exit: true, interrupt: true, return_error: nil, timeout: nil, tries: 3, wait: nil) ⇒ Tretry

Returns a new instance of Tretry.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tretry.rb', line 19

def initialize(errors: nil, exit: true, interrupt: true, return_error: nil, timeout: nil, tries: 3, wait: nil)
  self.errors = errors
  self.exit = exit
  self.fails = []
  self.interrupt = interrupt
  @before_retry = []
  self.return_error = return_error
  self.timeout = timeout
  self.tries = tries
  self.wait = wait
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



5
6
7
# File 'lib/tretry.rb', line 5

def error
  @error
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/tretry.rb', line 5

def errors
  @errors
end

#exitObject

Returns the value of attribute exit.



5
6
7
# File 'lib/tretry.rb', line 5

def exit
  @exit
end

#failsObject

Returns the value of attribute fails.



5
6
7
# File 'lib/tretry.rb', line 5

def fails
  @fails
end

#interruptObject

Returns the value of attribute interrupt.



5
6
7
# File 'lib/tretry.rb', line 5

def interrupt
  @interrupt
end

#return_errorObject

Returns the value of attribute return_error.



5
6
7
# File 'lib/tretry.rb', line 5

def return_error
  @return_error
end

#timeoutObject

Returns the value of attribute timeout.



5
6
7
# File 'lib/tretry.rb', line 5

def timeout
  @timeout
end

#triesObject

Returns the value of attribute tries.



5
6
7
# File 'lib/tretry.rb', line 5

def tries
  @tries
end

#waitObject

Returns the value of attribute wait.



5
6
7
# File 'lib/tretry.rb', line 5

def wait
  @wait
end

Class Method Details

.try(**args, &block) ⇒ Object

Runs a block of code a given amount of times until it succeeds.

Examples

res = Tretry.try(:tries => 3) do
   #something that often fails
end

puts "Tries: '#{res[:tries]}'."
puts "Result: '#{res[:result}'."


15
16
17
# File 'lib/tretry.rb', line 15

def self.try(**args, &block)
  Tretry.new(**args).try(&block)
end

Instance Method Details

#before_retry(&block) ⇒ Object



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

def before_retry(&block)
  @before_retry << block
end

#try(&block) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tretry.rb', line 35

def try(&block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  raise "No block given." unless block

  @block = block

  tries.times do |count|
    @count = count

    unless first_try?
      # Sleep for a given amount of time if the 'wait'-argument is given.
      sleep(wait) if wait

      call_before_retry(error: error)
      self.error = nil
    end

    begin
      # If a timeout-argument has been given, then run the code through the timeout.
      if timeout
        try_with_timeout
      else
        # Else call block normally.
        @res = @block.call
        @dobreak = true
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      handle_error(e)
    end

    if @doraise
      if return_error
        fails << {error: error}

        return Tretry::Result.new(
          fails: fails,
          error: true
        )
      end

      raise error
    elsif error
      fails << {error: error}
    end

    break if @dobreak
  end

  Tretry::Result.new(
    fails: fails,
    result: @res,
    error: false
  )
end