Moved Job killing functions to JenkinsUtils helper

Change-Id: I43427851b553f09b47a84cfb1493b039d6cdf410
diff --git a/src/com/mirantis/mk/JenkinsUtils.groovy b/src/com/mirantis/mk/JenkinsUtils.groovy
index ef6ce0c..25395e6 100644
--- a/src/com/mirantis/mk/JenkinsUtils.groovy
+++ b/src/com/mirantis/mk/JenkinsUtils.groovy
@@ -1,4 +1,5 @@
 package com.mirantis.mk
+import com.cloudbees.groovy.cps.NonCPS
 
 /**
  *
@@ -18,4 +19,32 @@
    return job.builds.findAll{build -> build.isBuilding()}
   }
   return []
+}
+
+@NonCPS
+def getRunningBuilds(job){
+  return job.builds.findAll{build -> build.isBuilding()}
+}
+
+@NonCPS
+def killStuckBuilds(maxSeconds, job){
+  def common = new com.mirantis.mk.Common()
+  def result = true
+  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){
+      result = false
+      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"));
+        result = true
+      }catch(e){
+        common.errorMsg("Error occured during aborting build: Exception: ${e}")
+      }
+    }
+  }
+  return result
 }
\ No newline at end of file