Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count STOPPING state ECS tasks #774

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions drain_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import os
import time
import random

from botocore.config import Config
from botocore.exceptions import ClientError

session = boto3.session.Session()
config = Config(
Expand All @@ -27,6 +29,25 @@ def ciFor(ec2Id):

return None, None

def taskExists(clusterName, ciId):
running_tasks = ecs.list_tasks(cluster=clusterName, containerInstance=ciId, desiredStatus='RUNNING')['taskArns']
if (len(running_tasks) > 0):
return True

# Assume there are not more than 100 tasks in a container
stopping_tasks = ecs.list_tasks(cluster=clusterName, containerInstance=ciId, desiredStatus='STOPPED')['taskArns']
for task_arn in stopping_tasks:
response = ecs.describe_tasks(
cluster=clusterName,
tasks=[task_arn]
)
status = response['tasks'][0]['lastStatus']
if status != 'STOPPED':
return True

print('No tasks, will proceed terminating the instance')
return False

def lambda_handler(event, context):
msg = json.loads(event['Records'][0]['Sns']['Message'])
ec2Id = msg['EC2InstanceId']
Expand All @@ -48,13 +69,15 @@ def lambda_handler(event, context):
if status != 'DRAINING':
ecs.update_container_instances_state(cluster=clusterName,containerInstances=[ciId],status='DRAINING')

tasks = ecs.list_tasks(cluster=clusterName, containerInstance=ciId)['taskArns']
if len(tasks) > 0:
if taskExists(clusterName, ciId):
time.sleep(5)
session.client('sns', config=config).publish(TopicArn=topicArn, Message=json.dumps(msg), Subject='Invoking lambda again')
else:
session.client('autoscaling', config=config).complete_lifecycle_action(LifecycleHookName=lifecycleHookName, AutoScalingGroupName=asgName, LifecycleActionResult='CONTINUE', InstanceId=ec2Id)
except ecs.exceptions.ThrottlingException:
sec = random.uniform(3, 5)
time.sleep(sec)
session.client('sns').publish(TopicArn=topicArn, Message=json.dumps(msg), Subject='Invoking lambda again')
except ClientError as exception_obj:
if exception_obj.response['Error']['Code'] == 'ThrottlingException':
sec = random.uniform(3, 5)
time.sleep(sec)
session.client('sns').publish(TopicArn=topicArn, Message=json.dumps(msg), Subject='Invoking lambda again')
else:
raise
1 change: 1 addition & 0 deletions lib/barcelona/network/autoscaling_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def build_resources
"ecs:DescribeContainerInstances",
"ecs:UpdateContainerInstancesState",
"ecs:ListTasks",
"ecs:DescribeTasks",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
Expand Down
1 change: 1 addition & 0 deletions spec/lib/barcelona/network/network_stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"ecs:DescribeContainerInstances",
"ecs:UpdateContainerInstancesState",
"ecs:ListTasks",
"ecs:DescribeTasks",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
Expand Down