Module: EC2Launcher::BackoffRunner

Included in:
Launcher
Defined in:
lib/ec2launcher/backoff_runner.rb

Overview

Helper module to run AWS requests.

Instance Method Summary collapse

Instance Method Details

#run_with_backoff(max_time, sleep_time, message, &block) ⇒ Object

we exceed the allowed AWS RequestLimit.

Parameters:

  • max_time (Integer)

    maximum amount of time to sleep before giving up.

  • sleep_time (Integer)

    the initial amount of time to sleep before retrying.

  • message (message)

    message to display if we get an exception.

  • block (Block)

    Ruby code block to execute.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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
    yield
  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)
  end  
  true
end