-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add script to terminate stale jobs #497
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is doStop enough for our case as these might take long to stop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a graceful stop which also takes care of stopping the underlying running process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes each agent will only run one build, then delete after. |
||
println "Aborted build #${build.number} for ${build.description}" | ||
} | ||
catch (Exception e) { | ||
if (build.result == Result.ABORTED) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add any checks for failed/already stopped builds? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
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()) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we terminating purely based on build description?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK when a gradle-check is triggered on a pull request the build description remains the same for each new build of the job. Let me know if I can have a check on any other attribute?