Last active
August 29, 2015 13:56
-
-
Save mountkin/9226590 to your computer and use it in GitHub Desktop.
wait_for.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fog | |
def self.wait_for(timeout=Fog.timeout, interval=Fog.interval, &block) | |
duration = 0 | |
start = Time.now | |
retries = 0 | |
until yield || duration > timeout | |
sleep(wait_policy(retries += 1, interval)) | |
duration = Time.now - start | |
end | |
if duration > timeout | |
raise Errors::TimeoutError.new("The specified wait_for timeout (#{timeout} seconds) was exceeded") | |
else | |
{ :duration => duration } | |
end | |
end | |
def self.wait_policy(retries, policy) | |
interval = policy.respond_to?(:call) ? policy.call(retries) : policy | |
interval.to_f | |
end | |
end | |
__END__ | |
#test | |
i = 0 | |
r = Fog.wait_for(10, lambda { |retries| [2 ** (retries - 1), 32].min }) do | |
i += 1 | |
i > 3 | |
end | |
puts r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment