加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Jenkins如何查找给定的从服务器是否正在运行作业

发布时间:2020-12-15 04:23:51 所属栏目:Java 来源:网络整理
导读:我有这个独特的要求来检查给定节点是否正在运行作业.我正在考虑使用groovy,因为它看起来最简单. 我发现这个答案很有用. 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 i
我有这个独特的要求来检查给定节点是否正在运行作业.我正在考虑使用groovy,因为它看起来最简单.

我发现这个答案很有用.

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

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

我正在考虑使用API功能setAcceptingTasks(false)将slave标记为运行Job,这样当我下次使用isAcceptingTasks()查询时,我会得到false,因此不会在该slave上启动作业.

但我宁愿拥有奴隶标记本身.

想到了taskAccepted()和taskCompleted().一旦接受任务,我就可以将setAcceptingTasks调用为false,并且在完成任务后再次将isAcceptingTasks设置为true.

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

我不确定我的任务假设是否等同于工作是否属实.

这就是我现在所拥有的:

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.

解决方法

这是我能够做到的hackish方式.我改变了我的工作流程以找到可用的免费奴隶而不是查找奴隶是否正忙,然后检查下一个看到它是免费的.
这个groovy脚本计算Slave上繁忙的执行程序的数量.它不断轮询,直到找到一个忙碌执行器数为零的在线从站.我讨厌民意调查,并要求知识渊博的成员提出建议,以某种方式插入Jenkins基于事件的通知.

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

它作为作业运行,并在找到合适的从站后立即返回.返回0是jenkins的成功.

如果有更好的方法,请填写.我不满意这种持续的民意调查我不得不这样做.我也不是遗嘱执行人的专家.所以如果有任何缺陷,请纠正我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读