[英]Boto3 ECS Fargate: how to wait until task arn is available
我正在使用以下命令增加集群中的任务计数:'update_service()'
然后我立即调用:'list_tasks(cluster=cluster_name)' 来尝试获取新任务 arn,但任务 arn 是空的,因为还没有。
有什么办法可以等到 arn 可用于该任务?
示例代码:
# 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
如果我没记错,您等待taskSetArn
继续执行脚本。 下面的代码可以指导你。 我说“指南”是因为我没有机会部署 AWS 资源来检查代码是否正常工作。 但如有必要,您可以对其进行调整和纠正。
您需要使用describe_task_sets
方法定期检查现有任务集的状态。 一旦你得到一个非空任务 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.