| Jakub Josef | f7283ee | 2017-03-20 19:10:13 +0100 | [diff] [blame] | 1 | /** | 
|  | 2 | * Long running jobs killer | 
|  | 3 | * | 
|  | 4 | *  MAX_DURATION_IN_HOURS - max permitted job duration in hours | 
|  | 5 | */ | 
|  | 6 | common = new com.mirantis.mk.Common() | 
|  | 7 |  | 
|  | 8 | def MAX_ALLOWED_DURATION_IN_SECONDS = 3600 * Interger.parseInt(MAX_DURATION_IN_HOURS) | 
|  | 9 |  | 
|  | 10 | for (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 Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 18 | common.infoMsg("Aborting ${job.name}-${build.id} which is running for ${durationInSeconds}s") | 
| Jakub Josef | f7283ee | 2017-03-20 19:10:13 +0100 | [diff] [blame] | 19 | try{ | 
| Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 20 | build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer")); | 
| Jakub Josef | f7283ee | 2017-03-20 19:10:13 +0100 | [diff] [blame] | 21 | }catch(e){ | 
|  | 22 | common.errorMsg("Error occured during aborting build: Exception: ${e}") | 
|  | 23 | } | 
|  | 24 | } | 
|  | 25 | } | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 |  | 
|  | 29 |  |