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_logger ⇒ Object (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

.logger ⇒ Object (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_configs ⇒ Object



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

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

.load_config(yaml_path) ⇒ Object



76
77
78
79
# File 'lib/ecs_deploy/auto_scaler.rb', line 76

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

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



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ecs_deploy/auto_scaler.rb', line 10

def run(yaml_path, log_file = nil, error_log_file = nil)
  trap(:TERM) { exit 0 }
  @logger = Logger.new(log_file || STDOUT)
  STDOUT.sync = true unless log_file
  @error_logger = Logger.new(error_log_file || STDERR)
  STDERR.sync = true unless error_log_file
  load_config(yaml_path)

  until @stop
    run_loop
  end
end

.run_loop ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ecs_deploy/auto_scaler.rb', line 27

def run_loop
  service_configs.each do |s|
    next if s.idle?

    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|
        if 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
    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(s.desired_count + difference)
    end
  end

  service_configs.group_by { |s| [s.region, s.auto_scaling_group_name] }.each do |(region, auto_scaling_group_name), configs|
    total_service_count = configs.inject(0) { |sum, s| sum + s.desired_count }
    asg_config = auto_scaling_group_configs.find { |c| c.name == auto_scaling_group_name && c.region == region }
    asg_config.update_auto_scaling_group(total_service_count)
  end

  TriggerConfig.clear_alarm_cache

  sleep @polling_interval
end

.service_configs ⇒ Object



81
82
83
# File 'lib/ecs_deploy/auto_scaler.rb', line 81

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

.stop ⇒ Object



23
24
25
# File 'lib/ecs_deploy/auto_scaler.rb', line 23

def stop
  @stop = true
end