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

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

问题描述

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

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.

如何在从另一个项目开始构建之前通过 Jenkins 中的脚本或插件检查从属设备是否在线

它允许我查找奴隶是否在线.我的下一步是检查它是否正在运行作业.

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.

我正在考虑使用 API 函数 setAcceptingTasks(false) 将 slave 标记为正在运行作业,以便当下次我使用 isAcceptingTasks() 查询时,我得到了错误,因此不会在该奴隶上启动工作.

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()taskCompleted() 浮现在脑海中.我可以在接受任务后将 setAcceptingTasks 调用为 false,并在任务完成后再次将 isAcceptingTasks 设置为 true.

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.

但我不确定这些函数采用的参数,例如执行器和任务.以及这些函数调用在哪里适合 groovy 脚本.

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;

每个从站上的执行程序数为 1.

Number of executors on each slave is 1.

推荐答案

这是我能够做到的骇人听闻的方法.我改变了我的工作流程来查找可用的空闲从站,而不是查找一个从站是否忙,然后检查下一个从站是否空闲.这个 groovy 脚本计算 Slave 上繁忙的 executors 的数量.它会不断轮询,直到找到一个忙碌的执行器数量为零的在线从站.我讨厌投票,会要求知识渊博的成员提供建议,以某种方式插入基于 Jenkins 事件的通知.

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

这作为作业运行并在找到合适的从站后立即返回.Return 0 在 Jenkins 中是成功的.

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天全站免登陆