简体   繁体   中英

Boto3 ECS Fargate: how to wait until task arn is available

I am increasing a task count in a cluster with with: 'update_service()'

I am then immediatly calling: 'list_tasks(cluster=cluster_name)' to try and get the new task arn, but the task arn is empty as there isnt one yet.

Is there any way I can wait until an arn becomes available for the task?

Sample code:

# increase task count to 1
response = ecs.update_service(
    cluster=cluster_name,
    service=service_name,
    desiredCount=1
)
print(response)

print("Listing tasks:")
response = ecs.list_tasks(
    cluster=cluster_name
)
print(response) # response shows blank arn

# code here that use's task arn

If I'm not wrong, you wait for taskSetArn to continue the script. The below code can guide you. I'm saying "guide" because I didn't have a chance to deploy the AWS resources to check if the code works correctly. But you can tweak and correct it if necessary.

You need to periodically check the status of the existing task sets with describe_task_sets method. And once you get a non-null task ARN, you can go with it.

import boto3
import time

ecs = boto3.client('ecs')

while True:

    response = ecs.describe_task_sets(
        cluster=cluster_name, 
        service=service_name,
    )
    # For this example, the first task is going to be considered.
    task_arn = response["taskSets"][0]["taskSetArn"]

    if not task_arn:
        print("Task ARN is not available yet!")
        time.sleep(10)
        continue
    else:
        print("Task ARN is: ", task_arn)
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM