blob: b4a63f3195b3030017b275f0e195ff7c02231709 [file] [log] [blame]
/**
* Long running jobs killer
*
* MAX_DURATION_IN_HOURS - max permitted job duration in hours
*/
common = new com.mirantis.mk.Common()
def MAX_ALLOWED_DURATION_IN_SECONDS = 3600 * Integer.parseInt(MAX_DURATION_IN_HOURS)
node{
stage("Kill long running jobs"){
def jobKilled = false
for (int i=0; i < Jenkins.instance.items.size(); i++) {
def job = Jenkins.instance.items[i]
def runningBuilds = _getRunningBuilds(job)
def jobName = job.name
for(int j=0; j < runningBuilds.size(); j++){
def build = runningBuilds[j]
int durationInSeconds = (System.currentTimeMillis() - build.getTimeInMillis())/1000.0
if(durationInSeconds > MAX_ALLOWED_DURATION_IN_SECONDS){
common.infoMsg("Aborting ${job.name}-${build.id} which is running for ${durationInSeconds}s")
try{
build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
}catch(e){
common.errorMsg("Error occured during aborting build: Exception: ${e}")
}
}
}
}
if(!jobKilled){
common.infoMsg("No job aborted")
}
}
}
@NonCPS
def _getRunningBuilds(job){
return job.builds.findAll{build -> build.isBuilding()}
}