Class: EcsDeploy::AutoScaler::AutoScalingGroupConfig

Inherits:
Struct
  • Object
show all
Includes:
ConfigBase
Defined in:
lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb

Constant Summary collapse

MAX_DETACHABLE_INSTANCE_COUNT =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigBase

#logger

Constructor Details

#initialize(attributes = {}, logger) ⇒ AutoScalingGroupConfig

Returns a new instance of AutoScalingGroupConfig.



15
16
17
18
19
20
21
22
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 15

def initialize(attributes = {}, logger)
  attributes = attributes.dup
  services = attributes.delete("services")
  super(attributes, logger)
  self.service_configs = services.map do |s|
    ServiceConfig.new(s.merge("cluster" => cluster, "region" => region), logger)
  end
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer

Returns:

  • (Object)

    the current value of buffer



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def buffer
  @buffer
end

#clusterObject

Returns the value of attribute cluster

Returns:

  • (Object)

    the current value of cluster



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def cluster
  @cluster
end

#disable_drainingObject

Returns the value of attribute disable_draining

Returns:

  • (Object)

    the current value of disable_draining



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def disable_draining
  @disable_draining
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def name
  @name
end

#regionObject

Returns the value of attribute region

Returns:

  • (Object)

    the current value of region



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def region
  @region
end

#service_configsObject

Returns the value of attribute service_configs

Returns:

  • (Object)

    the current value of service_configs



10
11
12
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 10

def service_configs
  @service_configs
end

Instance Method Details

#cluster_resource_managerObject



56
57
58
59
60
61
62
63
64
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 56

def cluster_resource_manager
  @cluster_resource_manager ||= EcsDeploy::AutoScaler::ClusterResourceManager.new(
    region: region,
    cluster: cluster,
    service_configs: service_configs,
    capacity_based_on: "instances",
    logger: @logger,
  )
end

#detach_instances(instance_ids:, should_decrement_desired_capacity:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 66

def detach_instances(instance_ids:, should_decrement_desired_capacity:)
  return if instance_ids.empty?

  instance_ids.each_slice(MAX_DETACHABLE_INSTANCE_COUNT) do |ids|
    client.detach_instances(
      auto_scaling_group_name: name,
      instance_ids: ids,
      should_decrement_desired_capacity: should_decrement_desired_capacity,
    )
  end

  @logger.info "#{log_prefix} Detached instances from ASG: #{instance_ids.inspect}"
end

#update_desired_capacity(required_capacity) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb', line 24

def update_desired_capacity(required_capacity)
  detach_and_terminate_orphan_instances

  desired_capacity = (required_capacity + buffer.to_f).ceil

  current_asg = client.describe_auto_scaling_groups({
    auto_scaling_group_names: [name],
  }).auto_scaling_groups[0]

  if current_asg.desired_capacity > desired_capacity
    decreased_capacity = decrease_desired_capacity(current_asg.desired_capacity - desired_capacity)
    if decreased_capacity > 0
      new_desired_capacity = current_asg.desired_capacity - decreased_capacity
      cluster_resource_manager.trigger_capacity_update(current_asg.desired_capacity, new_desired_capacity)
      @logger.info "#{log_prefix} Updated desired_capacity to #{new_desired_capacity}"
    else
      @logger.info "#{log_prefix} Tried to Update desired_capacity but there were no deregisterable instances"
    end
  elsif current_asg.desired_capacity < desired_capacity
    client.update_auto_scaling_group(
      auto_scaling_group_name: name,
      min_size: 0,
      max_size: [current_asg.max_size, desired_capacity].max,
      desired_capacity: desired_capacity,
    )
    cluster_resource_manager.trigger_capacity_update(current_asg.desired_capacity, desired_capacity)
    @logger.info "#{log_prefix} Updated desired_capacity to #{desired_capacity}"
  end
rescue => e
  AutoScaler.error_logger.error(e)
end