Module: EcsDeploy::AutoScaler

Defined in:
lib/ecs_deploy/auto_scaler.rb

Defined Under Namespace

Modules: ConfigBase Classes: AutoScalingConfig, ServiceConfig, TriggerConfig

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.error_loggerObject (readonly)

Returns the value of attribute error_logger.



8
9
10
# File 'lib/ecs_deploy/auto_scaler.rb', line 8

def error_logger
  @error_logger
end

.loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/ecs_deploy/auto_scaler.rb', line 8

def logger
  @logger
end

Class Method Details

.auto_scaling_group_configsObject



94
95
96
# File 'lib/ecs_deploy/auto_scaler.rb', line 94

def auto_scaling_group_configs
  @auto_scaling_group_configs ||= @config["auto_scaling_groups"].map(&AutoScalingConfig.method(:new))
end

.load_config(yaml_path) ⇒ Object



85
86
87
88
# File 'lib/ecs_deploy/auto_scaler.rb', line 85

def load_config(yaml_path)
  @config = YAML.load_file(yaml_path)
  @polling_interval = @config["polling_interval"] || 30
end

.main_loop(asg_config, configs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ecs_deploy/auto_scaler.rb', line 32

def main_loop(asg_config, configs)
  loop_with_polling_interval("loop of #{asg_config.name}") do
    ths = configs.map do |service_config|
      Thread.new(service_config) do |s|
        next if s.idle?

        @logger.debug "Start service scaling of #{s.name}"

        difference = 0
        s.upscale_triggers.each do |trigger|
          step = trigger.step || s.step
          next if difference >= step

          if trigger.match?
            logger.info "Fire upscale trigger of #{s.name} by #{trigger.alarm_name} #{trigger.state}"
            difference = step
          end
        end

        if difference == 0 && s.desired_count > s.current_min_task_count
          s.downscale_triggers.each do |trigger|
            next unless trigger.match?

            logger.info "Fire downscale trigger of #{s.name} by #{trigger.alarm_name} #{trigger.state}"
            step = trigger.step || s.step
            difference = [difference, -step].min
          end
        end

        if s.current_min_task_count > s.desired_count + difference
          difference = s.current_min_task_count - s.desired_count
        end

        if difference >= 0 && s.desired_count > s.max_task_count.max
          difference = s.max_task_count.max - s.desired_count
        end

        if difference != 0
          s.update_service(difference)
        end
      end
    end

    ths.each(&:join)

    @logger.debug "Start asg scaling of #{asg_config.name}"

    total_service_count = configs.inject(0) { |sum, s| sum + s.desired_count }
    asg_config.update_auto_scaling_group(total_service_count, configs[0])
    asg_config.detach_and_terminate_orphan_instances(configs[0])
  end
end

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ecs_deploy/auto_scaler.rb', line 10

def run(yaml_path, log_file = nil, error_log_file = nil)
  trap(:TERM) { @stop = true }
  trap(:INT) { @stop = true }
  @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)
  service_configs
  auto_scaling_group_configs

  config_groups = service_configs.group_by { |s| [s.region, s.auto_scaling_group_name] }
  ths = config_groups.map do |(region, auto_scaling_group_name), configs|
    asg_config = auto_scaling_group_configs.find { |c| c.name == auto_scaling_group_name && c.region == region }
    Thread.new(asg_config, configs, &method(:main_loop))
  end

  ths.each(&:join)
end

.service_configsObject



90
91
92
# File 'lib/ecs_deploy/auto_scaler.rb', line 90

def service_configs
  @service_configs ||= @config["services"].map(&ServiceConfig.method(:new))
end