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

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
31
32
33
34
35
36
# 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) ⇒ 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.

Returns:

  • (Boolean)

    True if the request suceeded, False otherwise.



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, 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