3
votes

Is there a way to only get the builds that are waiting in queue for an available agent in a specific pool from the Azure DevOps rest API?

I currently have this endpoint that provides me with all the job requests that occurred in the pool:

https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolid}/jobrequests

I looked through the API documentation and am unable to find anything regarding agent pools.

2

2 Answers

1
votes

There is no out of the box such API, but we can use the regular API and filter the results.

For example, I use the API you provided and I got all the builds in the pool, then, I filtered the results with PowerShell to get only the builds that waiting for an available agent.

How do I know who waiting? in the JSON result, to each build have some properties, if the build started to run on an agent he got a property assignTime, so I search builds without this property.

#... Do the API call and get the repsone
$json = $repsone | ConvertFrom-Json

$json.value.ForEach
({
    if(!$_.assignTime)
    {
        Write-Host "Build waiting for an agent:"
        Write-Host Build Definition Name: $_.definition.name
        Write-Host Build Id: $_.owner.id
        Write-Host Queue Time $_.queueTime
        # You can print more details about the build
    }
})


# Printed on screen:
Build waiting for an agent:
Build Definition Name: GitSample-CI
Build Id: 59
Queue Time 2019-01-16T07:36:52.8666667Z

If you don't want to iterate on all the builds (that make sense) you can retrieve the waiting builds in this way:

$waitingBuilds = $json.value | where {-not $_.assignTime} 
# Then print the details
1
votes

I needed the same thing but I'm running on Linux. The equivalent answer to @shayki-abramczyk in Linux is:

jobRequests=$(curl -u peterjgrainger:${{ YOUR_DEVOPS_TOKEN }} https://dev.azure.com/{your_org}/_apis/distributedtask/pools/{your_pool}/jobrequests?api-version=6.0)
queuedJobs=$(echo $jobRequests | jq '.value | map(select(has("assignTime") | not)) | length')
runningJobs=$(echo $jobRequests | jq '.value | map(select(.result == null)) | length')