如何使用Groovy有效地列出Jenkins当前正在运行的所有作业 [英] How do I Efficiently list **All** currently running jobs on Jenkins using Groovy
问题描述
我一直试图在Groovy scriptler脚本中找到一个轻量级方法来列出所有当前正在运行的任何类型的作业。我发现唯一可靠的方法是:
$ b $ pre $ start = System.currentTimeMillis()
def jobsFound = []
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll {
it.isBuilding()
}
buildingJobs.each {job->
allRuns = job._getRuns()
allRuns.each {item->
if(!item.isBuilding()){return} //这份工作不是建立
jobsFound.push(item.getUrl())
}
}
timespent =(System.currentTimeMillis() - start)/ 1000
println时间:$ {timespent}秒
println{jobsFound.size}
//结果:
//时间:2.015秒。 15职位
问题是,上面列举了所有当前正在运行的作业 - 我们有成千上万! - 然后枚举每个正在运行的作业的所有构建(一些作业有多达300个构建)。根据当前正在创建的作业数量,上述操作可能需要5分钟才能完成。
更有效的方法是枚举活动执行程序
但是这种方法 MISSES 在master上运行的管道(aka Workflow)作业:
start = System .currentTimeMillis()
def busyExecutors = Jenkins.instance.computers.collect {
c - > c.executors.findAll {it.isBusy()}
} .flatten()
def jobsFound = []
busyExecutors.each {e - >
job = e.getCurrentExecutable()
jobsFound.push(job.getUrl())
}
timespent =(System.currentTimeMillis() - start)/ 1000
println时间:$ {timespent}秒。$ {jobsFound.size}作业
//结果:
//时间:0.005秒。 12个职位
两个计数之间的差异是管道作业在主服务器上运行。
我想我的问题归结为:
有没有办法有效地枚举所有在 master 上运行的作业
显然Jenkins不包含计算机
,虽然有一个 MasterComputer
类,但它不清楚如何获取它或 OffByOneExecutors $ c
不直接与Jenkins类型/对象,而是通过Jenkins的从。 com / q / 14843874/1744774> Jenkins,我如何获得JSON中当前正在运行的作业列表? :
HTT p:// localhost:8080 / api / xml?& tree = jobs [builds [*]]& xpath = / hudson / job / build [building =true]&wrapper = builds
结果为:
<构建>
< build _class =hudson.model.FreeStyleBuild>
< action _class =hudson.model.CauseAction/>
< action />
< action />
<建筑>真实< /建筑>
< displayName>#10< / displayName>
<持续时间> 0< /持续时间>
< estimatedDuration> 3617< / estimatedDuration>
< executor />
< fullDisplayName> Freestyle-Project#10< / fullDisplayName>
< id> 10< / id>
< keepLog> false< / keepLog>
< number> 10< / number>
< queueId> 2< / queueId>
< timestamp> 1499611190781< / timestamp>
< url> http:// localhost:8080 / job / Freestyle-Project / 10 /< / url>
< builtOn />
< changeSet _class =hudson.scm.EmptyChangeLogSet/>
< / build>
< / builds>
不幸< builtOn />
,我猜,这应该是指节点,在我的Jenkins v2.60.1(尚未?)中没有提供。
使用:
http:// localhost:8080 / api / json?pretty = true
您会得到:
...
nodeDescription :主Jenkins节点,
...
jobs:[
{
_class:hudson.model.FreeStyleProject,
名称:Freestyle-Project,
url:http:// xmg:8080 / job / Freestyle-Project /,
color:aborted_anime
}
]
...
您可以使用以下方式进行过滤: p>
nodeDescription.equals('主Jenkins节点')&& color.endsWith( _动漫)。
I have been trying to find a lightweight method in a Groovy scriptler script to list all currently running jobs of any type. The only method I have found to be reliable is this:
start = System.currentTimeMillis()
def jobsFound = []
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll {
it.isBuilding()
}
buildingJobs.each { job->
allRuns = job._getRuns()
allRuns.each { item->
if (!item.isBuilding()) { return } // This job is not building
jobsFound.push(item.getUrl())
}
}
timespent = (System.currentTimeMillis() - start ) / 1000
println "Time: ${timespent} seconds"
println "{jobsFound.size} jobs"
// RESULTS:
// Time: 2.015 seconds. 15 jobs
The problem is that the above enumerates ALL currently running jobs - we have thousands! - then enumerate all builds of each of those running jobs (some jobs have as many as 300 builds). The above can take as long as FIVE minutes to complete depending on how many jobs are currently building.
A much more efficient method is to enumerate the active executors but this method MISSES the pipeline (aka Workflow) jobs running on the master:
start = System.currentTimeMillis()
def busyExecutors = Jenkins.instance.computers.collect {
c -> c.executors.findAll { it.isBusy() }
}.flatten()
def jobsFound = []
busyExecutors.each { e ->
job = e.getCurrentExecutable()
jobsFound.push(job.getUrl())
}
timespent = (System.currentTimeMillis() - start ) / 1000
println "Time: ${timespent} seconds. ${jobsFound.size} jobs"
// RESULTS:
// Time: 0.005 seconds. 12 jobs
Sure enough the discrepancy between the two counts are pipeline jobs running on the master.
I guess my question boils down to:
Is there a way to efficiently enumerate all jobs running on the master?
Clearly Jenkins does not include the master in computers
, and although there is a MasterComputer
class, its not clear how to get it or the OffByOneExecutors
on which flyweight (pipeline) jobs run.
Not directly with Jenkins types/objects but via Jenkins' Remote access API derived from From Jenkins, how do I get a list of the currently running jobs in JSON?:
http://localhost:8080/api/xml?&tree=jobs[builds[*]]&xpath=/hudson/job/build[building="true"]&wrapper=builds
results in:
<builds>
<build _class="hudson.model.FreeStyleBuild">
<action _class="hudson.model.CauseAction"/>
<action/>
<action/>
<building>true</building>
<displayName>#10</displayName>
<duration>0</duration>
<estimatedDuration>3617</estimatedDuration>
<executor/>
<fullDisplayName>Freestyle-Project #10</fullDisplayName>
<id>10</id>
<keepLog>false</keepLog>
<number>10</number>
<queueId>2</queueId>
<timestamp>1499611190781</timestamp>
<url>http://localhost:8080/job/Freestyle-Project/10/</url>
<builtOn/>
<changeSet _class="hudson.scm.EmptyChangeLogSet"/>
</build>
</builds>
Unfortunately <builtOn/>
, which, I guess, is supposed to refer to the node, isn't supplied in my Jenkins v2.60.1 (yet?).
With:
http://localhost:8080/api/json?pretty=true
you get:
...
"nodeDescription" : "the master Jenkins node",
...
"jobs" : [
{
"_class" : "hudson.model.FreeStyleProject",
"name" : "Freestyle-Project",
"url" : "http://xmg:8080/job/Freestyle-Project/",
"color" : "aborted_anime"
}
]
...
which you can filter then with:
nodeDescription.equals('the master Jenkins node') && color.endsWith('_anime').
这篇关于如何使用Groovy有效地列出Jenkins当前正在运行的所有作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!