Class: AwsHelpers::ElasticLoadBalancing::PollHealthyInstances

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_helpers/elastic_load_balancing/poll_healthy_instances.rb

Instance Method Summary collapse

Constructor Details

#initialize(elastic_load_balancing_client, load_balancer_name, required_instances, timeout) ⇒ PollHealthyInstances

Returns a new instance of PollHealthyInstances


8
9
10
11
12
13
# File 'lib/aws_helpers/elastic_load_balancing/poll_healthy_instances.rb', line 8

def initialize(elastic_load_balancing_client, load_balancer_name, required_instances, timeout)
  @elastic_load_balancing_client = elastic_load_balancing_client
  @load_balancer_name = load_balancer_name
  @required_instances = required_instances
  @timeout = timeout
end

Instance Method Details

#executeObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aws_helpers/elastic_load_balancing/poll_healthy_instances.rb', line 15

def execute
  puts "Polling #{@load_balancer_name} for minimum #{@required_instances} healthy instances"
  Timeout::timeout(@timeout) {
    loop do
      response = @elastic_load_balancing_client.describe_instance_health(load_balancer_name: @load_balancer_name)
      instance_states = response[:instance_states]
      in_service_count = instance_states.select{|instance_state| instance_state[:state] == IN_SERVICE }.count
      out_of_service = instance_states.count - in_service_count

      puts "In Service: #{in_service_count} Out of Service: #{out_of_service}"

      break if in_service_count >= @required_instances
      sleep 15
    end
  }

end