Added long running jobs cleanup pipeline
Change-Id: I2ff009efb425868ff324cce433c6e14c99f66bd1
diff --git a/abort-long-running-jobs.groovy b/abort-long-running-jobs.groovy
new file mode 100644
index 0000000..0fb8d9a
--- /dev/null
+++ b/abort-long-running-jobs.groovy
@@ -0,0 +1,29 @@
+/**
+ * Long running jobs killer
+ *
+ * MAX_DURATION_IN_HOURS - max permitted job duration in hours
+ */
+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} takes ${durationInSeconds}s")
+ try{
+ build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));
+ }catch(e){
+ common.errorMsg("Error occured during aborting build: Exception: ${e}")
+ }
+ }
+ }
+}
+
+
+