可以通过API请求来检查一个代理是否忙碌还是空闲吗?

huangapple go评论73阅读模式
英文:

Is it possible to check whether an agent is busy or idle via API request?

问题

我正在尝试创建一个“计数器”,用于计算池中有多少代理处于忙碌状态和多少代理处于空闲状态。

我尝试使用以下API调用来实现这一目标

https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents?api-version=7.0

但是响应中没有“busy”属性。我发现我可以通过为每个代理执行另一个API调用来检查代理是否忙碌。我在以下相关问题中找到了这个信息:
https://stackoverflow.com/questions/47290467/get-if-tfs-build-agent-is-busy-or-not

但这意味着我必须为池中的每个代理进行一次API请求,这将严重减慢计数器的速度,因为池中有大约1000个代理。

是否有其他方法可以检查代理是否忙碌,而不必为每个代理执行API调用?

英文:

I'm trying to create a "counter" that counts how many agents in a pool are busy and how many are idle.

I was trying to achieve this by using the following API call

https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents?api-version=7.0

But the response doesn't have a "busy" property. I found out that I could check whether an agent is busy or not by doing another API call for every agent. I found this in this related question:
https://stackoverflow.com/questions/47290467/get-if-tfs-build-agent-is-busy-or-not

But this would mean that I have to make an API request for every agent in the pool, and that would slow down the counter very much, because the pool has about 1000 agents.

Is there another way to check whether an agent is busy or not without doing an API call for every agent?

答案1

得分: 1

您可以请求所有代理的当前任务分配。空闲代理将不包含“assignedRequest”属性。此PowerShell脚本将输出所有忙碌代理的名称:

$agentsUri = 'https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents?includeAssignedRequest=true&api-version=7.0'
(Invoke-RestMethod -Method 'GET' -Uri $agentsUri -Headers $headers).value | Where-Object {$_.assignedRequest} | ForEach-Object {$_.name}

检查代理列表请求的可选参数,以提取更多数据:https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/agents/list?view=azure-devops-rest-7.0

英文:

You can request current assignment for all agents. assignedRequest property will be absent for idle agents. This powershell will output all names for busy agents:

$agentsUri = 'https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents?includeAssignedRequest=true&api-version=7.0'
(Invoke-RestMethod -Method 'GET' -Uri $agentsUri -Headers $headers).value | Where-Object {$_.assignedRequest} | ForEach-Object {$_.name}

Check optional parameters of agents list request to extract even more data: https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/agents/list?view=azure-devops-rest-7.0

huangapple
  • 本文由 发表于 2023年3月4日 00:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定