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.
source share