Java – how Jenkins finds out if a given slave server is running a job

I have this unique requirement to check whether a given node is running a job I'm thinking about using groovy because it looks the simplest

I find this answer very 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 out if the slave station is online My next step is to check if it is running a job

I am considering using the API function setacceptingtasks (false) to mark a slave as running a job, so that the next time I use isacceptingtasks () to query, I will get false, so I will not start a job on the slave

But I'd rather have the slave mark itself

I thought of taskaccepted () and taskcompleted () Once I accept the task, I can call setacceptingtasks to false and set isacceptingtasks to true again after completing the task

But I'm not sure about the parameters of these functions, such as actuators and tasks Where are these function calls suitable for groovy scripts

I'm not sure whether my task hypothesis is equivalent to work or whether it is true

This is what I have 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: the number of programs executed on each slave station is 1

Solution

This is the hackish way I can do it I changed my workflow to find available free slaves instead of finding out if the slaves were busy, and then checked next to see that it was free

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)
}

It runs as a job and returns as soon as a suitable slave is found Returning 0 is the success of Jenkins

If there is a better method, please fill in I'm not satisfied with this continuous poll, and I have to do so Nor am I an expert on executors So if there are any defects, please correct me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>