Skip to content

Commit

Permalink
Merge pull request #774 from degica/wait_for_stopping_tasks
Browse files Browse the repository at this point in the history
Count STOPPING state ECS tasks
  • Loading branch information
essa authored May 10, 2023
2 parents 6b9e688 + 433314d commit b4ad957
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
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

0 comments on commit b4ad957

Please sign in to comment.