New version of kill stuck jobs pipeline
Change-Id: I1738d1842b15e6907e858c53fa7feb96f8980c95
diff --git a/abort-long-running-jobs.groovy b/abort-long-running-jobs.groovy
index a612d7f..f4b4d36 100644
--- a/abort-long-running-jobs.groovy
+++ b/abort-long-running-jobs.groovy
@@ -5,33 +5,37 @@
*/
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 runningBuilds = _getRunningBuilds(Jenkins.instance.items[i])
- def jobName = Jenkins.instance.items[i].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")
+ killStuckBuilds(3600 * Integer.parseInt(MAX_DURATION_IN_HOURS), Jenkins.instance.items[i])
}
}
}
@NonCPS
-def _getRunningBuilds(job){
+def getRunningBuilds(job){
return job.builds.findAll{build -> build.isBuilding()}
+}
+
+@NonCPS
+def killStuckBuilds(maxSeconds, job){
+ def result = false
+ 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){
+ def buildId = runningBuilds[j].id
+ common.infoMsg("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"));
+ }catch(e){
+ common.errorMsg("Error occured during aborting build: Exception: ${e}")
+ }
+ result = true
+ }
+ }
+ return result
}
\ No newline at end of file