Search code examples
javagroovyjenkins

Jenkins How to find if a given slave is running a Job


I have this unique requirement to check if the given node is running a job or not. I am thinking of using groovy as it looks easiest option.

I have found this answer to be useful.

How can I check via a script or a plugin in Jenkins whether a slave is online before starting a build from another project on it

It allows me to find if the slave is online or not. Next step for me is to check if it is running a job.

I was considering using the API function setAcceptingTasks(false) to mark slave as running a Job so that when next time I query using isAcceptingTasks(), I get false and hence do not launch job on that slave.

But I would rather have the slave mark itself.

taskAccepted() and taskCompleted() come to mind. I can call the setAcceptingTasks to false once task is accepted and on completion of tasks set isAcceptingTasks to true once again.

But I am not sure of the arguments these functions take e.g executor and task. And where do these function calls fit in a groovy script.

I am not sure if my assumption of task is equivalent to a job is true or not.

This is what I have got till now:

import hudson.model.*
def requiredNodes = ['Slave1', 'Slave2', 'Slave3'];
def status = 0;
for (node in requiredNodes) 
{
      println "Searching for $node";
      slave = Hudson.instance.slaves.find({it.name == node});
      if (slave != null)
       {
        computer = slave.getComputer();
        if (computer.isOffline())
         {
           println "Error! $node is offline.";
           status = 1;
         }
         else 
         {
           println "OK: $node is online";
           if(computer.isAcceptingTasks())
           {
              //Launch job
           }
         }
       }
       else 
       {
         println "Slave $node not found!";
         status = 1;
       }
}
status;

EDIT: Number of executors on each slave is 1.


Solution

  • Here is the hackish way I was able to do it. I changed my workflow to find the available free slave rather than finding if a slave was busy and then checking the next one to see it is free.

    This Groovy script counts the number of busy executors on the Slave. It polls continuously till it finds an online slave with zero number of busy executors. I hate polling and will request knowledgeable members to chip in with suggestions to somehow plug into Jenkins event based notification.

    import hudson.FilePath
    import hudson.model.Node
    import hudson.model.Slave
    import jenkins.model.Jenkins
    import groovy.time.*
    
    Jenkins jenkins = Jenkins.instance
    def jenkinsNodes =jenkins.nodes
    while(1)
    {
        for (Node node in jenkinsNodes) 
        {
            sleep(1000)
            // Make sure slave is online
            if (!node.getComputer().isOffline()) 
            {           
                //Make sure that the slave busy executor number is 0.
                if(node.getComputer().countBusy()==0)
                {
                    println "'$node.nodeName' can take jobs !!!"
                    return 0
                }
                else
                {
                    println "$node.nodeName' is busy !!!"
                }
            }
            else
            {
                println "'$node.nodeName' is offline !!!" 
            }
        }
        sleep(1000)
    }
    

    This runs as job and returns as soon as it finds a suitable slave. Return 0 is a success in Jenkins.

    I am open to better ways of doing it. I am not really happy with this continuous polling I have to do.