Module: Nifty::Utils::UntilWithMaxAttempts
- Defined in:
- lib/nifty/utils/until_with_max_attempts.rb
Defined Under Namespace
Classes: MaxAttemptsReached
Class Method Summary collapse
-
.until(condition, options = {}, &block) ⇒ Object
Like a usual while block but with a maximum attempt counter.
Class Method Details
.until(condition, options = {}, &block) ⇒ Object
Like a usual while block but with a maximum attempt counter. Can be used like this
Attempts.until proc { job.completed? }, :attempts => 10, :gap => 5 do
puts "Waiting for job to complete..."
end
The proc is the condition which will be evaluated, :attempts sets the maximum number of times it will wait before raising an exception and :gap is the length of time in seconds to wait between each check.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nifty/utils/until_with_max_attempts.rb', line 23 def self.until(condition, = {}, &block) [:attempts] ||= 10 count = 0 until condition.call if count == [:attempts] raise MaxAttemptsReached, "Maximum attempts reached (#{[:attempts]}) without success" end yield count += 1 if [:gap] && count < [:attempts] sleep [:gap] end end end |