forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to terminate stale jobs (opensearch-project#497)
Signed-off-by: Rishabh Singh <[email protected]> Signed-off-by: Rishabh Singh <[email protected]> Co-authored-by: Sayali Gaikawad <[email protected]>
- Loading branch information
1 parent
d88c840
commit c37f893
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** Library to fetch failing tests at the end of gradle-check run and index the results in an OpenSearch cluster. | ||
* | ||
* @param Map args = [:] args A map of the following parameters | ||
* @param args.jobName <required> - The name of the jenkins job. | ||
* @param args.lookupTime <optional> - Fetch builds from past N hours for the job, defaults to 6 hours. | ||
*/ | ||
|
||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import jenkins.model.Jenkins | ||
import hudson.model.Result | ||
|
||
void call(Map args = [:]) { | ||
String jobName = args.jobName.toString() | ||
long lookupTime = isNullOrEmpty(args.lookupTime.toString()) ? 6 : Long.parseLong(args.lookupTime.toString()) | ||
|
||
if (isNullOrEmpty(jobName)) { | ||
throw new IllegalArgumentException("Error: jobName is null or empty") | ||
} | ||
|
||
def currentBuildNumber = currentBuild.number | ||
def currentBuildDescription = currentBuild.description | ||
def endTime = Instant.now() | ||
def startTime = endTime.minus(lookupTime, ChronoUnit.HOURS) | ||
def startMillis = startTime.toEpochMilli() | ||
def endMillis = endTime.toEpochMilli() | ||
|
||
// Add sleep to let job-id get assigned to queued jobs when triggered via generic webhook url | ||
sleep(15) | ||
|
||
def currentJob = Jenkins.instance.getItemByFullName(jobName) | ||
|
||
//Fetch all builds for the job based on look up time provided | ||
def builds = currentJob.getBuilds().byTimestamp(startMillis,endMillis) | ||
for (build in builds) { | ||
if (build.isBuilding() && currentBuildNumber > build.number && currentBuildDescription == build.description) { | ||
try { | ||
build.doStop() | ||
println "Aborted build #${build.number} for ${build.description}" | ||
} | ||
catch (Exception e) { | ||
if (build.result == Result.ABORTED) { | ||
println "Build #${build.number} is already aborted!" | ||
} | ||
else { | ||
println "Failed to abort build #${build.number}: ${e.message}" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
boolean isNullOrEmpty(String str) { return (str == 'Null' || str == null || str.allWhitespace || str.isEmpty()) } |