user1692315
user1692315

Reputation: 149

Get if TFS build agent is busy or not

I would like to solve with TFS 2015 REST API to get if an agent is busy or not. I tried to get requests from an agent but seems, it's listed only completed ones:

http://tfsserver:8080/tfs/_apis/distributedtask/pools/2/jobrequests?agentId=41

I looked up all possible api call but I couldn't figured out, how can I get if and agent has running build or not.

May somebody can help me out.

Thank you!

Upvotes: 1

Views: 946

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30432

Just check the latest Request (top first request) of the response, if there is the item "result" (eg: "result": "succeeded") in the value, then the agent is idle, otherwise it's busy.

In short :

  • Idle:

    "result": "succeeded", or "result": "abandoned", or "result": "succeededWithIssues",

  • Busy:

    No "result" item

You can use below PowerShell script to identify that, if the Result is empty, then the agent is busy:

$serverurl = "http://tfsserver:8080/tfs"
$poolid = "39"
$agentid = "147"
$baseUrl = "$serverurl/_apis/distributedtask/pools/$poolid/jobrequests?agentId=$agentid"            
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential).value

$requests = @()

foreach($request in $response){

    $customObject = new-object PSObject -property @{
          "RequestId" = $request.requestId
          "Result" = $request.result
          "AgentName" = $request.reservedAgent.name
          "AgentVersion" = $request.reservedAgent.version
          "AgentStatus" = $request.reservedAgent.status
          "BuildDefinitionID" = $request.definition.id
          "BuildDefinitionName" = $request.definition.name
          "BuildID" = $request.owner.id
          "BuildName" = $request.owner.name
        } 

    $requests += $customObject      
}

$requests | Select-Object -first 1 `
                RequestId,
                Result, 
                AgentName,
                AgentVersion, 
                AgentStatus,
                BuildDefinitionID,
                BuildDefinitionName,
                BuildID,
                BuildName

Actually, the simplest way is checking the agnet status via web portal:

Manage Project (gear icon) --> Agent Pools --> Select the specific Agent pool and Agent --> Check the Requests

enter image description here

Upvotes: 3

Related Questions