Class: EcsDeploy::AutoScaler::InstanceDrainer

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_deploy/auto_scaler/instance_drainer.rb

Instance Method Summary collapse

Constructor Details

#initialize(auto_scaling_group_configs:, spot_fleet_request_configs:, logger:) ⇒ InstanceDrainer

Returns a new instance of InstanceDrainer.



10
11
12
13
14
15
# File 'lib/ecs_deploy/auto_scaler/instance_drainer.rb', line 10

def initialize(auto_scaling_group_configs:, spot_fleet_request_configs:, logger:)
  @auto_scaling_group_configs = auto_scaling_group_configs || []
  @spot_fleet_request_configs = spot_fleet_request_configs || []
  @logger = logger
  @stop = false
end

Instance Method Details

#poll_spot_instance_interruption_warnings(queue_url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ecs_deploy/auto_scaler/instance_drainer.rb', line 17

def poll_spot_instance_interruption_warnings(queue_url)
  @logger.debug "Start polling spot instance interruption warnings of #{queue_url}"

  # cf. https://docs.aws.amazon.com/general/latest/gr/rande.html#sqs_region
  region = URI.parse(queue_url).host.split(".")[1]

  poller = Aws::SQS::QueuePoller.new(queue_url, client: sqs_client(region))
  poller.before_request do |stats|
    throw :stop_polling if @stop
  end

  until @stop
    begin
      poller.poll(max_number_of_messages: 10, visibility_timeout: 15) do |messages, _|
        instance_ids = messages.map do |msg|
          JSON.parse(msg.body).dig("detail", "instance-id")
        end

        config_to_instance_ids = build_config_to_instance_ids(instance_ids, region)
        set_instance_state_to_draining(config_to_instance_ids, region)
        # Detach the instances to launch other instances
        detach_instances_from_auto_scaling_groups(config_to_instance_ids, region)
      end
    rescue => e
      AutoScaler.error_logger.error(e)
    end
  end

  @logger.debug "Stop polling spot instance interruption warnings of #{queue_url}"
end

#stopObject



48
49
50
# File 'lib/ecs_deploy/auto_scaler/instance_drainer.rb', line 48

def stop
  @stop = true
end