Jenkins How do you know if a given slave is working?

I have this unique requirement to check if a node job is running or not. I think to use groovy because it looks the easiest.

I found this answer helpful.

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

This allows me to find if a subordinate is or not. The next step for me is to check if this works.

I was considering using the setAcceptingTasks (false) API to mark the slave as starting a job so that on the next request using isAcceptingTasks () , I get false and therefore not starting the job on that slave.

But I would have preferred the slave sign itself.

taskAccepted () and taskCompleted () comes to mind. I can call setAcceptingTasks to false as soon as the task is accepted, and when the task completes, set the AcceptingTasks value back to true.

But I'm not sure about the arguments that these functions do, like the executor and the task. And where do these function calls fit into the groovy script.

I'm not sure if my guess about the task equals the task or not.

This is what I have so far:

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: The number of performers on each slave is 1.

+3


source to share


2 answers


Here is a hacky way I was able to do it. I modified my workflow to find an available free slave rather than find if a slave is busy and then check the next one to make sure it is free. This groovy script counts the number of busy executors on the slave. He constantly polls until he finds an online slave with zero employed performers. I hate polling and ask knowledgeable members of chip solutions with suggestions to somehow hook 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 a job and returns as soon as it finds a suitable slave. Return 0 - success in Jenkins.

If there is a better way to do this, please turn on the chip. I don't really like this continuous survey that I need. Also I'm not a performer specialist. So, any shortcomings, if any, please correct me.

+5


source


To get the name of the node that the current build is running on, just use System.getenv ("NODE_NAME")

When Jenkins job is running, it sets some environment variables.



You may find these variables very useful. Check this link

0


source







All Articles