Class: TryUntil::Repeatedly
- Inherits:
-
Object
- Object
- TryUntil::Repeatedly
- Defined in:
- lib/try_until/repeatedly.rb
Overview
External interface Example: include TryUntil result = Repeatedly.new(Probe.new(Object.new, :to_s))
.attempts(5)
.interval(10)
.delay(120)
.rescues([ ArgumentError, IOError ])
.stop_when(lambda { |response| JSON.parse(response.body)['id'] == 'some_id' })
.log_to($stdout)
.execute
Not all of the above settings are required. These are the default values: attempts = 3 interval = 0 delay = 0 rescues = [] stop_when = lambda { |response| false } log_to = TryUntil::NullPrinter.new
Instance Method Summary collapse
- #attempts(int_num) ⇒ Object
- #configuration ⇒ Object
- #delay(seconds) ⇒ Object
-
#execute ⇒ Object
The heart of this gem: This method will repeatedly call ‘#sample’ on the subject and evaluate if the expectated result is returned.
-
#initialize(probe) ⇒ Repeatedly
constructor
A new instance of Repeatedly.
- #interval(seconds) ⇒ Object
- #log_to(io) ⇒ Object
- #rescues(errors) ⇒ Object
- #stop_when(callable) ⇒ Object
Constructor Details
#initialize(probe) ⇒ Repeatedly
Returns a new instance of Repeatedly.
24 25 26 |
# File 'lib/try_until/repeatedly.rb', line 24 def initialize(probe) @probe = probe end |
Instance Method Details
#attempts(int_num) ⇒ Object
28 29 30 31 |
# File 'lib/try_until/repeatedly.rb', line 28 def attempts(int_num) @attempts = int_num self end |
#configuration ⇒ Object
91 92 93 94 |
# File 'lib/try_until/repeatedly.rb', line 91 def configuration { :probe => @probe.to_s, :attempts => @attempts, :interval => @interval, :rescues => @rescues, :stop_when => @stop_when } end |
#delay(seconds) ⇒ Object
38 39 40 41 |
# File 'lib/try_until/repeatedly.rb', line 38 def delay(seconds) @delay = seconds self end |
#execute ⇒ Object
The heart of this gem: This method will repeatedly call ‘#sample’ on the subject and evaluate if the expectated result is returned. In case of errors it will rescue those and continue, provided the type of error is among the ones defined in @rescues.
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 88 89 |
# File 'lib/try_until/repeatedly.rb', line 62 def execute @attempts = 3 unless @attempts @interval = 0 unless @interval @delay = 0 unless @delay @rescues = [] unless @rescues @stop_when = lambda { |response| false } unless @stop_when @log_to = NullPrinter.new unless @log_to Kernel.sleep(@delay) if @delay > 0 count = 0 while count < @attempts begin result = @probe.sample if @stop_when.call(result) log_outcome(count, 'CONDITION_MET') return result end log_outcome(count, 'CONDITION_NOT_MET') rescue *@rescues => exception log_outcome(count, exception.class) raise exception, "During final attempt (#{@attempts} configured) target returned #{exception}" if count + 1 == @attempts ensure count += 1 Kernel.sleep @interval if @interval > 0 end end raise "After #{@attempts} attempts, the expected result was not returned!" end |
#interval(seconds) ⇒ Object
33 34 35 36 |
# File 'lib/try_until/repeatedly.rb', line 33 def interval(seconds) @interval = seconds self end |
#log_to(io) ⇒ Object
53 54 55 56 |
# File 'lib/try_until/repeatedly.rb', line 53 def log_to(io) @log_to = io self end |
#rescues(errors) ⇒ Object
43 44 45 46 |
# File 'lib/try_until/repeatedly.rb', line 43 def rescues(errors) @rescues = errors self end |
#stop_when(callable) ⇒ Object
48 49 50 51 |
# File 'lib/try_until/repeatedly.rb', line 48 def stop_when(callable) @stop_when = callable self end |