Skip to content

Commit

Permalink
Update ProjectUtils.groovy
Browse files Browse the repository at this point in the history
  • Loading branch information
avelasquezr committed Mar 13, 2024
1 parent 799ee5a commit 9ff71be
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,32 @@ class ProjectUtils {
}

/**
* Retrieves the count of running executions for a given project or projects.
* Retrieves the count of running executions for a given project, waiting until the count exceeds a specified value or a timeout occurs.
*
* @param projectName The name of the project to query. Must not be null and can be *.
* @param projectName The name of the project to query. Must not be null.
* @param valueMoreThan The threshold value for the count of running executions. Must be greater than zero.
* @param client The RdClient instance used to make HTTP requests. Must not be null.
* @return The count of running executions in the project.
* @throws RuntimeException if fetching running executions fails.
* @return True if the count of running executions exceeds the specified value within the timeout period, false otherwise.
* @throws RuntimeException if fetching running executions fails or if a timeout occurs.
*/
static def jobCountExecutions = (String projectName, RdClient client) -> {
def response = client.doGet("/project/${projectName}/executions/runing?includePostponed=true")
if (!response.successful) {
throw new RuntimeException("Failed to get running executions: ${response.body().string()}")
static def jobCountExecutions = (String projectName, int valueMoreThan, RdClient client) -> {
def startTime = System.currentTimeMillis()
def timeout = 10000
def pollingInterval = 1000
while (true) {
def response = client.doGet("/project/${projectName}/executions/running?includePostponed=true")
if (!response.successful) {
throw new RuntimeException("Failed to get running executions: ${response.body().string()}")
}
def valueCount = client.jsonValue(response.body(), Map).paging.count
if (valueCount > valueMoreThan) {
return Boolean.TRUE
}
if (System.currentTimeMillis() - startTime > timeout) {
throw new RuntimeException("Timeout: No running executions found within ${timeout} milliseconds.")
}
sleep pollingInterval
}
return client.jsonValue(response.body(), Map).paging.count
}

}

0 comments on commit 9ff71be

Please sign in to comment.