Module: EC2Launcher::BackoffRunner
- Included in:
- BlockDeviceBuilder, HostnameGenerator, Launcher, Terminator
- Defined in:
- lib/ec2launcher/backoff_runner.rb
Overview
Helper module to run AWS requests.
Instance Method Summary collapse
-
#run_with_backoff(max_time, sleep_time, message, &block) ⇒ Object
we exceed the allowed AWS RequestLimit.
-
#test_with_backoff(max_time, sleep_time, message, &block) ⇒ Boolean
Runs a block that returns true or false.
Instance Method Details
#run_with_backoff(max_time, sleep_time, message, &block) ⇒ Object
we exceed the allowed AWS RequestLimit.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ec2launcher/backoff_runner.rb', line 16 def run_with_backoff(max_time, sleep_time, , &block) if sleep_time > max_time puts "AWS::EC2::Errors::RequestLimitExceeded ... failed #{}" return false end begin block.call rescue AWS::EC2::Errors::RequestLimitExceeded puts "AWS::EC2::Errors::RequestLimitExceeded ... retrying #{} in #{sleep_time} seconds" sleep sleep_time run_with_backoff(max_time, sleep_time * 2, , &block) rescue AWS::EC2::Errors::InstanceLimitExceeded puts "AWS::EC2::Errors::InstanceLimitExceeded ... aborting launch." return false rescue Exception => bang print "Error for #{}: #{bang}" return false end true end |
#test_with_backoff(max_time, sleep_time, message, &block) ⇒ Boolean
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.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ec2launcher/backoff_runner.rb', line 44 def test_with_backoff(max_time, sleep_time, , &block) if sleep_time < max_time result = block.call unless result puts "Retrying #{} in #{sleep_time} seconds" sleep sleep_time result = test_with_backoff(max_time, sleep_time * 2, , &block) end result else false end end |