Skip to content

Instantly share code, notes, and snippets.

@depop-blog
Created June 27, 2018 15:38
Show Gist options
  • Save depop-blog/1ae66ecacaad0fd25dbc40b1cfec0886 to your computer and use it in GitHub Desktop.
Save depop-blog/1ae66ecacaad0fd25dbc40b1cfec0886 to your computer and use it in GitHub Desktop.
Ahead of time scheduling on ECS/EC2: high_level.py
for asg in auto_scaling_groups:
cluster_name = next(x for x in asg['Tags'] if x['Key'] == 'cluster_name')['Value']
instance_list = ecs.list_container_instances(cluster=cluster_name, status='ACTIVE')
instances = ecs.describe_container_instances(
cluster=cluster_name,
containerInstances=instance_list['containerInstanceArns'])
# all tasks running on current cluster
tasks = get_tasks(cluster_name)
# get the largest CPU reservation of any container
max_cpu = int(max(tasks, key=lambda x: x['cpu'])['cpu'])
# get the largest memory reservation of any container
max_memory = int(max(tasks, key=lambda x: x['memory'])['memory'])
schedulable_containers = 0
for instance in instances['containerInstances']:
remaining_resources = {resource['name']: resource
for resource in instance['remainingResources']}
# the amount of tasks that could be started based on CPU utilisation
containers_by_cpu = int(remaining_resources['CPU']['integerValue'] / max_cpu)
# the amount of tasks that could be started based on memory utilisation
containers_by_mem = int(remaining_resources['MEMORY']['integerValue'] / max_memory)
# the amount of containers that could be started on the current instance
free_slots = min(containers_by_cpu, containers_by_mem)
# add to the total of the cluster
schedulable_containers += free_slots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment