简体   繁体   English

Boto3 ECS Fargate:如何等到任务arn可用

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

I am increasing a task count in a cluster with with: 'update_service()'我正在使用以下命令增加集群中的任务计数:'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.然后我立即调用:'list_tasks(cluster=cluster_name)' 来尝试获取新任务 arn,但任务 arn 是空的,因为还没有。

Is there any way I can wait until an arn becomes available for the task?有什么办法可以等到 arn 可用于该任务?

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.如果我没记错,您等待taskSetArn继续执行脚本。 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.我说“指南”是因为我没有机会部署 AWS 资源来检查代码是否正常工作。 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.您需要使用describe_task_sets方法定期检查现有任务集的状态。 And once you get a non-null task ARN, you can go with it.一旦你得到一个非空任务 ARN,你就可以用它 go。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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