如何使用AWS Boto3查询ECS服务的Desired Count、Remaining Count
解决方案
你当前使用的list_tasks接口仅能返回对应服务下的任务ARN列表,无法获取服务的配置与调度状态数据,需要调用describe_services接口获取你需要的Desired Count和Remaining Count数值。
代码示例
# 原有统计运行中任务数的代码 tasks = client.list_tasks(cluster=cluster, serviceName=serviceName) running_tasks = len(tasks['taskArns']) # 新增查询服务详情的代码 service_resp = client.describe_services( cluster=cluster, services=[serviceName] ) service_detail = service_resp['services'][0] # 提取Desired Count(期望运行任务总数) desired_count = service_detail['desiredCount'] # 计算Remaining Count(还未进入运行状态的待完成任务数) remaining_count = desired_count - service_detail['runningCount']
补充说明
- 你也可以省略
list_tasks调用,直接从服务详情中获取运行中任务数,对应字段为service_detail['runningCount'],和你之前统计的结果一致 - 如果需要排除已经提交调度、处于启动中状态的PENDING任务,剩余待调度任务数可按如下公式计算:
remaining_count = desired_count - service_detail['runningCount'] - service_detail['pendingCount']
内容的提问来源于stack exchange,提问作者David A
相关产品推荐
相关产品推荐

