詹金斯如何找到一个给定的奴隶是否正在运行一个工作 [英] Jenkins How to find if a given slave is running a Job

查看:79
本文介绍了詹金斯如何找到一个给定的奴隶是否正在运行一个工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个独特的要求来检查给定节点是否正在运行作业。我在考虑使用groovy,因为它看起来是最简单的选择。



我发现这个答案很有用。



如何通过Jenkins中的脚本或插件检查从属服务器是否在线,然后从另一个项目开始构建它



它可以让我发现奴隶是否在线。对我来说,下一步是检查它是否正在运行一项工作。



我正在考虑使用 API 函数 setAcceptingTasks(false) 将slave标记为正在运行Job,以便在下次使用 isAcceptingTasks(),我得到了错误,因此不会在该奴隶上启动任务。



但我宁愿让奴隶标记自己。

taskAccepted() taskCompleted()一旦任务被接受并且任务完成后,我可以将setAcceptingTasks设置为false,然后再次将isAcceptingTasks设置为true。

但我不确定这些函数采用的参数,如执行程序和任务。



我不确定我的任务假设是否等同于某个工作是否正确。



这是我到现在为止的内容:

  import hudson .model。* 
def requiredNodes = ['Slave1','Slave2','Slave3'];
def status = 0;
for(node in requiredNodes)
{
println正在搜索$ node;
slave = Hudson.instance.slaves.find({it.name == node});
if(slave!= null)
{
computer = slave.getComputer();
if(computer.isOffline())
{
println错误!$ node is offline。;
status = 1;
}
else
{
printlnOK:$ node is online;
if(computer.isAcceptingTasks())
{
//启动作业
}
}
}
else
{
printlnSlave $ node not found!;
status = 1;
}
}
状态;

编辑:每个奴隶的执行者数量为1。

解决方案

这是我能够做到的骇人的方式。我改变了我的工作流程以找到可用的空闲奴隶,而不是查找奴隶是否繁忙,然后检查下一个奴隶是否空闲。
这个groovy脚本计算Slave上繁忙的执行者数量。它持续轮询,直到找到一个拥有零个忙执行者的在线奴隶。我讨厌投票,并会要求有知识的会员提供建议,以便以某种方式插入Jenkins基于事件的通知。

  import hudson.FilePath 
导入hudson.model.Node
导入hudson.model.Slave
导入jenkins.model.Jenkins
导入groovy.time。*

Jenkins jenkins = Jenkins.instance
def jenkinsNodes = jenkins.nodes
while(1)
{
for(Node jenkinsNodes中的节点)
{
sleep(1000 )
//确保slave在线
if(!node.getComputer()。isOffline())
{
//确保从忙执行程序号是0 。
if(node.getComputer()。countBusy()== 0)
{
println'$ node.nodeName'可以工作!!!
return 0
}
else
{
println$ node.nodeName'正在忙碌!!!
}
}
else
{
println'$ node.nodeName'is offline !!!


sleep(1000)
}

当它找到合适的奴隶时,它会作为工作并返回。返回0在Jenkins中是成功的。



如果还有其他更好的方法,请切入。我对这种持续不断的投票并不满意。
我也不是执行者的专家。所以,如果有任何缺陷,请纠正我。


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.

解决方案

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.

If there is any better way of doing it please chip in. I am not really happy with this continuous polling I have to to. Also I am not an expert on executors. So any flaws if there, please correct me.

这篇关于詹金斯如何找到一个给定的奴隶是否正在运行一个工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆