Module: EcsDeploy::AutoScaler

Defined in:
lib/ecs_deploy/auto_scaler.rb,
lib/ecs_deploy/auto_scaler/config_base.rb,
lib/ecs_deploy/auto_scaler/service_config.rb,
lib/ecs_deploy/auto_scaler/trigger_config.rb,
lib/ecs_deploy/auto_scaler/instance_drainer.rb,
lib/ecs_deploy/auto_scaler/cluster_resource_manager.rb,
lib/ecs_deploy/auto_scaler/auto_scaling_group_config.rb,
lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb

Defined Under Namespace

Modules: ConfigBase Classes: AutoScalingGroupConfig, ClusterResourceManager, InstanceDrainer, ServiceConfig, SpotFleetRequestConfig, TriggerConfig

Constant Summary collapse

SERVICE_CONFIG_ATTRIBUTES =
%i(name cluster region step max_task_count min_task_count idle_time scheduled_min_task_count cooldown_time_for_reach_max upscale_triggers downscale_triggers desired_count required_capacity)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.error_loggerObject (readonly)

Returns the value of attribute error_logger.



13
14
15
# File 'lib/ecs_deploy/auto_scaler.rb', line 13

def error_logger
  @error_logger
end

.loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/ecs_deploy/auto_scaler.rb', line 13

def logger
  @logger
end

Class Method Details

.auto_scaling_group_configsObject



100
101
102
103
104
105
106
107
108
# File 'lib/ecs_deploy/auto_scaler.rb', line 100

def auto_scaling_group_configs
  @auto_scaling_group_configs ||= (@config["auto_scaling_groups"] || []).each.with_object({}) do |c, configs|
    configs[c["name"]] ||= {}
    if configs[c["name"]][c["region"]]
      raise "Duplicate entry in auto_scaling_groups (name: #{c["name"]}, region: #{c["region"]})"
    end
    configs[c["name"]][c["region"]] = AutoScalingGroupConfig.new(c, @logger)
  end.values.flat_map(&:values)
end

.load_config(yaml_path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ecs_deploy/auto_scaler.rb', line 70

def load_config(yaml_path)
  @config = YAML.load_file(yaml_path)
  @polling_interval = @config["polling_interval"] || 30
  if @config["services"]
    @error_logger&.warn('"services" property in root-level is deprecated. Please define it in "auto_scaling_groups" property or "spot_fleet_requests" property.')
    @config.delete("services").each do |svc|
      if svc["auto_scaling_group_name"] && svc["spot_fleet_request_id"]
        raise "You can specify only one of 'auto_scaling_group_name' or 'spot_fleet_request_name'"
      end

      svc_region = svc.delete("region")
      if svc["auto_scaling_group_name"]
        asg_name = svc.delete("auto_scaling_group_name")
        asg = @config["auto_scaling_groups"].find { |g| g["region"] == svc_region && g["name"] == asg_name }
        asg["services"] ||= []
        asg["services"] << svc
        asg["cluster"] = svc.delete("cluster")
      end

      if svc["spot_fleet_request_id"]
        sfr_id = svc.delete("spot_fleet_request_id")
        sfr = @config["spot_fleet_requests"].find { |r| r["region"] == svc_region && r["id"] == sfr_id }
        sfr["services"] ||= []
        sfr["services"] << svc
        sfr["cluster"] = svc.delete("cluster")
      end
    end
  end
end

.main_loop(cluster_scaling_config) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ecs_deploy/auto_scaler.rb', line 49

def main_loop(cluster_scaling_config)
  loop_with_polling_interval("loop of #{cluster_scaling_config.name}") do
    ths = cluster_scaling_config.service_configs.map do |service_config|
      Thread.new(service_config) do |s|
        @logger.debug "Start service scaling of #{s.name}"
        s.adjust_desired_count(cluster_scaling_config.cluster_resource_manager)
      end
    end
    ths.each { |th| th.abort_on_exception = true }

    ths.each(&:join)

    @logger.debug "Start cluster scaling of #{cluster_scaling_config.name}"

    required_capacity = cluster_scaling_config.service_configs.sum { |s| s.desired_count * s.required_capacity }
    cluster_scaling_config.update_desired_capacity(required_capacity)

    cluster_scaling_config.service_configs.each(&:wait_until_desired_count_updated)
  end
end

.run(yaml_path, log_file = nil, error_log_file = nil) ⇒ Object



15
16
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
47
# File 'lib/ecs_deploy/auto_scaler.rb', line 15

def run(yaml_path, log_file = nil, error_log_file = nil)
  @enable_auto_scaling = true
  setup_signal_handlers
  @logger = Logger.new(log_file || STDOUT)
  @logger.level = Logger.const_get(ENV["ECS_AUTO_SCALER_LOG_LEVEL"].upcase) if ENV["ECS_AUTO_SCALER_LOG_LEVEL"]
  STDOUT.sync = true unless log_file
  @error_logger = Logger.new(error_log_file || STDERR)
  @error_logger.level = Logger.const_get(ENV["ECS_AUTO_SCALER_LOG_LEVEL"].upcase) if ENV["ECS_AUTO_SCALER_LOG_LEVEL"]
  STDERR.sync = true unless error_log_file
  load_config(yaml_path)

  ths = (auto_scaling_group_configs + spot_fleet_request_configs).map do |cluster_scaling_config|
    Thread.new(cluster_scaling_config, &method(:main_loop)).tap { |th| th.abort_on_exception = true }
  end

  if @config["spot_instance_intrp_warns_queue_urls"]
    drainer = EcsDeploy::AutoScaler::InstanceDrainer.new(
      auto_scaling_group_configs: auto_scaling_group_configs,
      spot_fleet_request_configs: spot_fleet_request_configs,
      logger: logger,
    )
    polling_ths = @config["spot_instance_intrp_warns_queue_urls"].map do |queue_url|
      Thread.new(queue_url) do |url|
        drainer.poll_spot_instance_interruption_warnings(url)
      end.tap { |th| th.abort_on_exception = true }
    end
  end

  ths.each(&:join)

  drainer&.stop
  polling_ths&.each(&:join)
end

.spot_fleet_request_configsObject



110
111
112
113
114
115
116
117
118
# File 'lib/ecs_deploy/auto_scaler.rb', line 110

def spot_fleet_request_configs
  @spot_fleet_request_configs ||= (@config["spot_fleet_requests"] || []).each.with_object({}) do |c, configs|
    configs[c["id"]] ||= {}
    if configs[c["id"]][c["region"]]
      raise "Duplicate entry in spot_fleet_requests (id: #{c["id"]}, region: #{c["region"]})"
    end
    configs[c["id"]][c["region"]] = SpotFleetRequestConfig.new(c, @logger)
  end.values.flat_map(&:values)
end