class Ecs::Easy::Cluster::MemScale

Constants

EC2_PROFILE_PATH
INSTANCE_TYPES

Public Instance Methods

acceptable_task?( task_definition ) click to toggle source
# File lib/ecs/easy/cluster/mem_scale.rb, line 72
def acceptable_task?( task_definition )
  # It preserves 128MB for ecs-agent
  required_memory( task_definition ) <= current_instance_memory - (128*1024*1024)
end
make_task_running!( task_definition, overrides={} ) click to toggle source
# File lib/ecs/easy/cluster/mem_scale.rb, line 28
def make_task_running!( task_definition, overrides={} )
  unless exists?
    unless up!
      raise "Failed to create the new cluster. You should check the CloudFormation events."
    end
  end

  retry_count = 0
  begin
    wait_until_ready
    res = run_task!( task_definition, overrides )
    unless res.failures.empty?
      raise CannotStartTaskError.new( res )
    end
  rescue CannotStartTaskError => e
    puts e.failures
    case e.fail_reason
    when "RESOURCE:MEMORY"
      puts "No enough memory on current container instances to execute this task. Add another container instance automatically."

      unless acceptable_task?( task_definition )
        raise "Couldn\'t accept this task because of the lack of memory. You should upgrade ec2 instance type."
      end

      if num_instances >= max_instances
        puts "Couldn\'t scale more instances because it reaches maximum instances. You should upgrade the maximum number of instance to execute multiple tasks at the same time."
      else
        scale!
      end
    else
      raise "Unknown reason: #{e.failures}"
    end

    puts "Failed to run the task. Try again."
    sleep 10
    retry_count += 1
    retry if retry_count <= 3
  rescue => e
    raise "Unknown reason: #{e}"
  end

  res
end

Private Instance Methods

current_instance_memory() click to toggle source
# File lib/ecs/easy/cluster/mem_scale.rb, line 85
def current_instance_memory
  INSTANCE_TYPES[instance.type]["mem"] * 1024 * 1024 * 1024 # byte
end
required_memory( task_definition ) click to toggle source
# File lib/ecs/easy/cluster/mem_scale.rb, line 78
def required_memory( task_definition )
  res = ecs_client.describe_task_definition( task_definition: task_definition )
  container_mems = res.task_definition.container_definitions.map(&:memory)
  total_mem_mb = container_mems.inject(0){|sum,n| sum + n}
  total_mem_mb * 1024 * 1024 # byte
end