module EC2Launcher::BackoffRunner
Helper module to run AWS requests.
Public Instance Methods
run_with_backoff(max_time, sleep_time, message, &block)
click to toggle source
Runs an AWS request inside a Ruby block with an exponential backoff in case we exceed the allowed AWS RequestLimit.
@param [Integer] max_time maximum amount of time to sleep before giving up. @param [Integer] sleep_time the initial amount of time to sleep before retrying. @param [message] message message to display if we get an exception. @param [Block] block Ruby code block to execute.
# File lib/ec2launcher/backoff_runner.rb, line 16 def run_with_backoff(max_time, sleep_time, message, &block) if sleep_time > max_time puts "AWS::EC2::Errors::RequestLimitExceeded ... failed #{message}" return false end begin block.call rescue AWS::EC2::Errors::RequestLimitExceeded puts "AWS::EC2::Errors::RequestLimitExceeded ... retrying #{message} in #{sleep_time} seconds" sleep sleep_time run_with_backoff(max_time, sleep_time * 2, message, &block) rescue AWS::EC2::Errors::InstanceLimitExceeded puts "AWS::EC2::Errors::InstanceLimitExceeded ... aborting launch." return false rescue Exception => bang print "Error for #{message}: #{bang}" return false end true end
test_with_backoff(max_time, sleep_time, message, &block)
click to toggle source
Runs a block that returns true or false. If the block returns false, retries the request after sleeping. Repeated failures trigger an exponential backoff in sleep time.
@return [Boolean] True if the request suceeded, False otherwise.
# File lib/ec2launcher/backoff_runner.rb, line 44 def test_with_backoff(max_time, sleep_time, message, &block) if sleep_time < max_time result = block.call unless result puts "Retrying #{message} in #{sleep_time} seconds" sleep sleep_time result = test_with_backoff(max_time, sleep_time * 2, message, &block) end result else false end end