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.



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

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

Instance Attribute Details

#failsObject (readonly)

Returns the value of attribute fails.



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

def fails
  @fails
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#triesObject

Returns the value of attribute tries.



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

def tries
  @tries
end

#waitObject

Returns the value of attribute wait.



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

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}'."


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

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

Instance Method Details

#before_retry(&block) ⇒ Object



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

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

#try(&block) ⇒ Object



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
# File 'lib/tretry.rb', line 36

def try(&block)
  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)
      @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
      handle_error(e)
    end

    if @doraise
      if @return_error
        @fails << {error: @error}
        return Tretry::Result.new(
          fails: @fails,
          error: true
        )
      else
        raise @error
      end
    elsif @error
      @fails << {error: @error}
    end

    break if @dobreak
  end

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