jakubjosef
6/6/2017 - 3:14 PM

jenkins-kill-stuck-jobs.groovy

def MAX_DURATION_IN_HOURS = "6"
for (int i=0; i < Jenkins.instance.items.size(); i++) {
  if(!killStuckBuilds(3600 * Integer.parseInt(MAX_DURATION_IN_HOURS), Jenkins.instance.items[i])){
     println("Kill failed!")
  }
}
def killStuckBuilds(maxSeconds, job){
  def result = true
  def runningBuilds = getRunningBuilds(job)
  def jobName = job.name
  for(int j=0; j < runningBuilds.size(); j++){
    int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0
    if(durationInSeconds > maxSeconds){
      result = false
      def buildId = runningBuilds[j].id
      println("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s")
      try{
        runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
        result = true
      }catch(e){
        println("Error occured during aborting build: Exception: ${e}")
      }
    }
  }
  return result
}
def getRunningBuilds(job){
  return job.builds.findAll{build -> build.isBuilding()}
}