Completed long job aborting pipeline
Change-Id: I481a533518302b2b5badff03e735d235cf5b0303
diff --git a/abort-long-running-jobs.groovy b/abort-long-running-jobs.groovy
index 278c4c3..b4a63f3 100644
--- a/abort-long-running-jobs.groovy
+++ b/abort-long-running-jobs.groovy
@@ -5,25 +5,34 @@
*/
common = new com.mirantis.mk.Common()
-def MAX_ALLOWED_DURATION_IN_SECONDS = 3600 * Interger.parseInt(MAX_DURATION_IN_HOURS)
-
-for (int i=0; i < Jenkins.instance.items.size(); i++) {
- def job = Jenkins.instance.items[i]
- def runningBuilds = job.builds.findAll{build -> build.isBuilding()}
- 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}")
+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()}
+}
\ No newline at end of file