gpt4 book ai didi

amazon-web-services - 如何获取与特定服务关联的任务ID

转载 作者:行者123 更新时间:2023-12-01 21:09:45 25 4
gpt4 key购买 nike

当我调用DescribeServies时,我期望得到一系列与我的服务相关联的任务arn或任务id。但是情况并非如此,除非我检查events对象:

Events: [
{
CreatedAt: 2020-07-03 03:57:56 +0000 UTC,
Id: "af62a356-2ab7-4b18-b51c-718bef02901c",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has reached a steady state."
},
{
CreatedAt: 2020-07-03 03:57:44 +0000 UTC,
Id: "de2a4fad-8ea8-419d-8493-8c7cb5406afb",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has stopped 1 running tasks: (task 1f92882b-72d0-490b-aa03-18fd32edb2e8)."
},
{
CreatedAt: 2020-07-03 01:35:09 +0000 UTC,
Id: "bd8da7f5-9bb1-4d23-b416-f79678b487a8",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has reached a steady state."
},
{
CreatedAt: 2020-07-03 01:34:40 +0000 UTC,
Id: "da05f6a0-4163-4d93-b7af-22297d307620",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has started 1 tasks: (task e132a2a7-ad49-4436-88cb-07b2ce399dd2)."
},
{
CreatedAt: 2020-07-02 14:29:21 +0000 UTC,
Id: "cb56a4ff-de6a-402f-aaa5-377bf236b00a",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has started 1 tasks: (task e69ca53b-7b5c-4c7d-aed2-b28d9261b111)."
}
]
如您所见,提取任务ID非常棘手
我的最终目标是以编程方式获取已部署服务的实例的IP地址。
我当前的笨拙解决方案:(为简洁起见,省略了错误处理)

servicesResponse, _ := s.ecsClient.DescribeServices(describeServicesParams)


var taskId string
for _, serviceEvent := range servicesResponse.Services[0].Events {

if strings.Contains(*serviceEvent.Message, "has stopped 1 running tasks") {
break
}

if strings.Contains(*serviceEvent.Message, "has started 1 tasks") {
found := strings.Split(*serviceEvent.Message, "has started 1 tasks: (task ")[1]
taskId = strings.Replace(found, ").", "", -1)
break
}
}
log.Info("taskId", taskId)


之后,我调用 DescribeTasks以获得 containerInstanceArn,然后我调用 DescribeInstances

最佳答案

如您所述,DescribeTasks不太适合用于获取服务中的任务ID。
通常,要获取服务中的任务列表,可以使用ListTasks调用。对于调用,您可以指定感兴趣的服务名称:

serviceName: The name of the service with which to filter the ListTasks results. Specifying a serviceName limits the results to tasks that belong to that service.

ListTasks将为您提供 taskArns,该代码又可用于 DescribeTasks中以获取任务 details
任务ID是其 arn的一部分,因此,如果仅需要ID,则可以从arns中提取它们。

关于amazon-web-services - 如何获取与特定服务关联的任务ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62708443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com