Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether the jenkins build executors are idle or not in jenkins using java api

Tags:

java

jenkins

I want to know the whether the build executors in jenkins are idle(either free or any job is running in the master node), by using java-jenkins API. And my requirement is like this.

  1. If any executor is free I've to trigger a jenkins job otherwise I'll wait till they are available.
  2. For this I've to use API of Jenkins
  3. And all this implementation I've to do in Java

Please help me which API method will give that status of executor.Executors are the highlighted things shown in the pic below:

like image 377
3lokesh Avatar asked Mar 13 '23 07:03

3lokesh


1 Answers

System groovy script to check if slaves are online and idle.

import hudson.model.Node
import hudson.model.Slave
import jenkins.model.Jenkins

Jenkins jenkins = Jenkins.instance
def jenkinsNodes =jenkins.nodes

for (Node node in jenkinsNodes) 
{
    // Make sure slave is online
    if (!node.getComputer().isOffline()) 
    {           
        //Make sure that the slave busy executor number is 0.
        if(node.getComputer().countBusy()==0)
        {
           ...Do somthing...
        }
    }
}
like image 115
Dvir669 Avatar answered Apr 14 '23 14:04

Dvir669