Add wrapper around parallel function to restrict number of threads
Change-Id: Ib54c5bfcd0426ed084fb4a87bd882037f4733b21
Related-bug: PROD-23465 (PROD:23465)
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 249175b..b2c8d8a 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -857,3 +857,31 @@
common.errorMsg("Can't update env parameteres, because: ${e.toString()}")
}
}
+
+/**
+ * Wrapper around parallel pipeline function
+ * with ability to restrict number of parallel threads
+ * running simultaneously
+ *
+ * @param branches - Map with Clousers to be executed
+ * @param maxParallelJob - Integer number of parallel threads allowed
+ * to run simultaneously
+ */
+def runParallel(branches, maxParallelJob = 10) {
+ def runningSteps = 0
+ branches.each { branchName, branchBody ->
+ if (branchBody instanceof Closure) {
+ branches[branchName] = {
+ while (!(runningSteps < maxParallelJob)) {
+ continue
+ }
+ runningSteps += 1
+ branchBody.call()
+ runningSteps -= 1
+ }
+ }
+ }
+ if (branches) {
+ parallel branches
+ }
+}