Search code examples
azure-devopsazure-devops-rest-apiagent

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


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: 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?


Solution

  • 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