Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know how long a Jenkins job has been in the wait queue after the job is finished?

Tags:

jenkins

groovy

For the statistics, I want to see how long the job is in the waiting queue, therefore I can tune the system to make the job is run in time.

If the job is just in queue, it is possible to find in waiting queue in front page see How can I tell how long a Jenkins job has been in the wait queue?

Or the http://<jenkins_url>/queue/api/json?pretty=true

Is it possible to check somewhere to get "Time waiting in queue" for the specific job after the job is finished ?

Will be nice if it can be gotten in public jenkins API.

like image 837
Larry Cai Avatar asked Feb 26 '15 05:02

Larry Cai


People also ask

How do I get build duration in Jenkins?

You can use ${currentBuild. durationString} to get build duration.

How can you avoid the waiting time for the triggered jobs in Jenkins?

You can set timeout duration for a particular project by defining a global timeout variable and using it in your job. Go to Manage Jenkins > Configure System and in Global Properties check Environment Variables , then add a variable (ex: EXEC_TIME_OUT) to it and assign the timeout amount.


1 Answers

// got answer from colleague

It can be achieved by installing Jenkins Metrics Plugin, after it is installed, in the build result page, you will see

time wait in queue

Jenkins REST API: Then you can get wait time in queue from http://localhost:8080/job/demo/1/api/json?pretty=true&depth=2 . queuingDurationMillis is the data I wanted.

"actions" : [
{
  "queuingDurationMillis" : 33,
  "totalDurationMillis" : 3067
}
],

Groovy script: Also we can get this data in groovy via internal data, check below code in Jenkins Script console http://localhost:8080/script

job = hudson.model.Hudson.instance.getItem("demo")
build = job.getLastBuild()
action = build.getAction(jenkins.metrics.impl.TimeInQueueAction.class)
println action.getQueuingDurationMillis()

You can see the demo using docker by running below and open in browser for demo job

docker run -it -p 8080:8080 larrycai/jenkins-metrics
like image 59
Larry Cai Avatar answered Sep 21 '22 07:09

Larry Cai