Class: EcsDeploy::InstanceFluctuationManager
- Inherits:
-
Object
- Object
- EcsDeploy::InstanceFluctuationManager
- Defined in:
- lib/ecs_deploy/instance_fluctuation_manager.rb
Constant Summary collapse
- MAX_UPDATABLE_ECS_CONTAINER_COUNT =
10
- MAX_DETACHEABLE_EC2_INSTACE_COUNT =
20
- MAX_DESCRIBABLE_ECS_TASK_COUNT =
100
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #decrease ⇒ Object
- #increase ⇒ Object
-
#initialize(region:, cluster:, auto_scaling_group_name:, desired_capacity:, logger:) ⇒ InstanceFluctuationManager
constructor
A new instance of InstanceFluctuationManager.
Constructor Details
#initialize(region:, cluster:, auto_scaling_group_name:, desired_capacity:, logger:) ⇒ InstanceFluctuationManager
Returns a new instance of InstanceFluctuationManager.
13 14 15 16 17 18 19 |
# File 'lib/ecs_deploy/instance_fluctuation_manager.rb', line 13 def initialize(region:, cluster:, auto_scaling_group_name:, desired_capacity:, logger:) @region = region @cluster = cluster @auto_scaling_group_name = auto_scaling_group_name @desired_capacity = desired_capacity @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/ecs_deploy/instance_fluctuation_manager.rb', line 7 def logger @logger end |
Instance Method Details
#decrease ⇒ Object
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 84 85 86 87 88 89 |
# File 'lib/ecs_deploy/instance_fluctuation_manager.rb', line 42 def decrease asg = fetch_auto_scaling_group decrease_count = asg.desired_capacity - @desired_capacity if decrease_count <= 0 @logger.info("The capacity is already #{asg.desired_capacity}") return end @logger.info("Decrease desired capacity of #{@auto_scaling_group_name}: #{asg.desired_capacity} => #{@desired_capacity}") container_instances = ecs_client.list_container_instances(cluster: @cluster).flat_map do |resp| ecs_client.describe_container_instances( cluster: @cluster, container_instances: resp.container_instance_arns ).container_instances end az_to_container_instances = container_instances.sort_by {|ci| - ci.running_tasks_count }.group_by do |ci| ci.attributes.find {|attribute| attribute.name == "ecs.availability-zone" }.value end if az_to_container_instances.empty? @logger.info("There are no instances to terminate.") return end target_container_instances = extract_target_container_instances(decrease_count, az_to_container_instances) @logger.info("running tasks: #{ecs_client.list_tasks(cluster: @cluster).task_arns.size}") all_running_task_arns = [] target_container_instances.map(&:container_instance_arn).each_slice(MAX_UPDATABLE_ECS_CONTAINER_COUNT) do |arns| @logger.info(arns) ecs_client.update_container_instances_state( cluster: @cluster, container_instances: arns, status: "DRAINING" ) arns.each do |arn| all_running_task_arns.concat(list_running_task_arns(arn)) end end stop_tasks_not_belonging_service(all_running_task_arns) wait_until_tasks_stopped(all_running_task_arns) instance_ids = target_container_instances.map(&:ec2_instance_id) terminate_instances(instance_ids) @logger.info("Succeeded in decreasing instances!") end |
#increase ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ecs_deploy/instance_fluctuation_manager.rb', line 21 def increase asg = fetch_auto_scaling_group @logger.info("Increase desired capacity of #{@auto_scaling_group_name}: #{asg.desired_capacity} => #{asg.max_size}") as_client.update_auto_scaling_group(auto_scaling_group_name: @auto_scaling_group_name, desired_capacity: asg.max_size) # Run in background because increasing instances may take time Thread.new do loop do cluster = ecs_client.describe_clusters(clusters: [@cluster]).clusters.first instance_count = cluster.registered_container_instances_count if instance_count == asg.max_size @logger.info("Succeeded in increasing instances!") break end @logger.info("Current registered instance count: #{instance_count}") sleep 5 end end end |