Last active
August 29, 2015 14:27
-
-
Save larrycai/5479abdf31bcc6505eb5 to your computer and use it in GitHub Desktop.
Some groovy scripts for running jobs which executed time is longer than expected, also waiting time in queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonBuilder | |
import org.apache.commons.httpclient.* | |
import org.apache.commons.httpclient.methods.* | |
import hudson.model.Queue.Executable | |
int WARNING_DURATION_IN_SECONDS = 100 * 2 // 6 hours | |
dashing_url="http://172.17.0.2:3030/widgets/events" | |
def busyExecutors = Jenkins.instance.computers | |
.collect { | |
c -> c.executors.findAll { it.isBusy() } | |
} | |
.flatten() // reminder: transforms list(list(executor)) into list(executor) | |
// check estimated remaining time is less than 0 | |
// & running over 2 hours | |
println "Busy Executors list" | |
def longDuration = [:] | |
busyExecutors.each { e -> | |
println e ; | |
println e.getCurrentExecutable() | |
println("estimated time is " + e.getEstimatedRemainingTimeMillis()/1000.0 + " seconds" ) | |
int durationInSeconds = (System.currentTimeMillis() - e.executable.getTimeInMillis())/1000.0 | |
println "duration=$durationInSeconds seconds" | |
println "getEstimatedRemainingTimeMillis()=" | |
println e.getEstimatedRemainingTimeMillis() | |
if(durationInSeconds > WARNING_DURATION_IN_SECONDS && e.getEstimatedRemainingTimeMillis() < 0 ) | |
{ | |
longDuration[e.getCurrentExecutable().toString()] = durationInSeconds/60 | |
} | |
} | |
println longDuration | |
// construct dashing widget request | |
def data = [ | |
auth_token: "YOUR_AUTH_TOKEN", | |
items: longDuration.collect { name, minutes -> | |
[ | |
"arrow": "icon-warning-sign", | |
"color": "red", | |
"value": minutes, | |
label: name + " => " + minutes.toString() + " mins" | |
]} | |
] | |
def json = new JsonBuilder(data) | |
println("JSON data:" + json.toPrettyString()) | |
// send to dashing | |
def post = new PostMethod(dashing_url) | |
try { | |
post.setRequestEntity( | |
new StringRequestEntity(json.toPrettyString(), | |
"application/json", | |
"UTF-8")) | |
post.setRequestHeader("Content-Type", | |
"application/json; charset=UTF-8") | |
def httpclient = new HttpClient() | |
int status_code = httpclient.executeMethod(post) | |
println "INFO: Response status code: ${status_code}" | |
if(status_code != 204){ | |
throw new IOException("Dashing push failure, status: ${status_code}") | |
} | |
} finally { | |
post.releaseConnection() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hudson.model.* | |
import groovy.time.TimeCategory | |
import groovy.time.TimeDuration | |
import groovy.json.JsonBuilder | |
import org.apache.commons.httpclient.* | |
import org.apache.commons.httpclient.methods.* | |
def queues = Hudson.instance.queue | |
println "Queue contains ${queues.items.length} items" | |
// get the queue list from jenkins instance , [name: waitingminutes] | |
start = new Date() | |
def longQueues = [:] | |
for(queue in queues.items) { | |
println(queue.getInQueueSince()) | |
println(new Date(((long)queue.getInQueueSince()))) | |
println(queue.task.name) | |
TimeDuration td = TimeCategory.minus( start,new Date(((long)queue.getInQueueSince())) ) | |
longQueues[queue.task.name] = td.getMinutes() + 1 | |
} | |
dashing_url="http://172.17.0.2:3030/widgets/events" | |
// construct dashing widget request | |
def data = [ | |
auth_token: "YOUR_AUTH_TOKEN", | |
items: longQueues.collect { name, minutes -> | |
[ | |
"arrow": "icon-warning-sign", | |
"color": "red", | |
"value": minutes, | |
label: name + " => " + minutes.toString() + " mins" | |
]} | |
] | |
def json = new JsonBuilder(data) | |
// send to dashing | |
def post = new PostMethod(dashing_url) | |
try { | |
println("JSON data:" + json.toPrettyString()) | |
post.setRequestEntity( | |
new StringRequestEntity(json.toPrettyString(), | |
"application/json", | |
"UTF-8")) | |
post.setRequestHeader("Content-Type", | |
"application/json; charset=UTF-8") | |
def httpclient = new HttpClient() | |
int status_code = httpclient.executeMethod(post) | |
println "INFO: Response status code: ${status_code}" | |
if(status_code != 204){ | |
throw new IOException("Dashing push failure, status: ${status_code}") | |
} | |
} finally { | |
post.releaseConnection() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment