blob: 278c4c32ba4576ecd49304b5d55a084a6dad1d9e [file] [log] [blame]
Jakub Joseff7283ee2017-03-20 19:10:13 +01001/**
2 * Long running jobs killer
3 *
4 * MAX_DURATION_IN_HOURS - max permitted job duration in hours
5 */
6common = new com.mirantis.mk.Common()
7
8def MAX_ALLOWED_DURATION_IN_SECONDS = 3600 * Interger.parseInt(MAX_DURATION_IN_HOURS)
9
10for (int i=0; i < Jenkins.instance.items.size(); i++) {
11 def job = Jenkins.instance.items[i]
12 def runningBuilds = job.builds.findAll{build -> build.isBuilding()}
13 def jobName = job.name
14 for(int j=0; j < runningBuilds.size(); j++){
15 def build = runningBuilds[j]
16 int durationInSeconds = (System.currentTimeMillis() - build.getTimeInMillis())/1000.0
17 if(durationInSeconds > MAX_ALLOWED_DURATION_IN_SECONDS){
Jakub Josef617e1162017-03-21 15:58:27 +010018 common.infoMsg("Aborting ${job.name}-${build.id} which is running for ${durationInSeconds}s")
Jakub Joseff7283ee2017-03-20 19:10:13 +010019 try{
Jakub Josef617e1162017-03-21 15:58:27 +010020 build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
Jakub Joseff7283ee2017-03-20 19:10:13 +010021 }catch(e){
22 common.errorMsg("Error occured during aborting build: Exception: ${e}")
23 }
24 }
25 }
26}
27
28
29