Skip to content

Instantly share code, notes, and snippets.

@mountkin
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mountkin/9226590 to your computer and use it in GitHub Desktop.
Save mountkin/9226590 to your computer and use it in GitHub Desktop.
wait_for.rb
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