How can I efficiently list ** All ** current work orders in Jenkins using Groovy

I am trying to find an easy method in a Groovy scriptler script to list all the currently running jobs of any type. The only method I found reliable is as follows:

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 lists ALL current work tasks - we have thousands! - then list all the assemblies of each of these work tasks (some tasks will have 300 collections). The above may take up to five minutes to complete depending on how many works are currently under construction.

A more efficient method is to list the active executors, but this method is MISSES pipeline tasks (aka Workflow) 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 

Of course, the mismatch between the two counts is a pipeline job running on the main server.

I think my question comes down to the following:

Is there a way to effectively list all jobs performed on master ?

It is clear that Jenkins does not include a master in computers , and although there is a MasterComputer class, it is not clear how to get it or OffByOneExecutors , on which flyweight tasks (conveyor) are executed.

+3
source share
1 answer

Not directly with Jenkins types / objects, but through the Jenkins Remote Access API obtained from From Jenkins, how do I get a list of current jobs in JSON? :

 http://localhost:8080/api/xml?&tree=jobs[builds[*]]&xpath=/hudson/job/build[building="true"]&wrapper=builds 

leads to:

 <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 suppose should reference node, is not listed in my Jenkins v2.60.1 (yet?).

FROM

 http://localhost:8080/api/json?pretty=true 

You are getting:

 ... "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 and then:

 nodeDescription.equals('the master Jenkins node') && color.endsWith('_anime'). 
+1
source

Source: https://habr.com/ru/post/1011819/


All Articles