-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Functionality to delete logs. #3545
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,7 +39,8 @@ | |
| import java.util.List; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import static java.util.logging.Level.*; | ||
| import static java.util.logging.Level.FINE; | ||
| import static java.util.logging.Level.FINER; | ||
|
|
||
| /** | ||
| * Default implementation of {@link BuildDiscarder}. | ||
|
|
@@ -76,10 +77,25 @@ public class LogRotator extends BuildDiscarder { | |
| */ | ||
| private final Integer artifactNumToKeep; | ||
|
|
||
| /** | ||
| * If not -1 nor null, build logs are only kept up to this days. | ||
| * Null handling is necessary to remain data compatible with old versions. | ||
| * @since FIXME | ||
| */ | ||
| private final Integer logDaysToKeep; | ||
|
|
||
| /** | ||
| * If not -1, only this number of build logs are kept. | ||
| * Null handling is necessary to remain data compatible with old versions. | ||
| * @since FIXME | ||
| */ | ||
| private final Integer logNumToKeep; | ||
|
|
||
| @DataBoundConstructor | ||
| public LogRotator (String daysToKeepStr, String numToKeepStr, String artifactDaysToKeepStr, String artifactNumToKeepStr) { | ||
| public LogRotator (String daysToKeepStr, String numToKeepStr, String artifactDaysToKeepStr, String artifactNumToKeepStr, String logDaysToKeepStr, String logNumToKeepStr) { | ||
|
Member
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. 🐛 this method is a part of public API, so this change breaks binary compatibility. The original method should be retained OR you could use |
||
| this (parse(daysToKeepStr),parse(numToKeepStr), | ||
| parse(artifactDaysToKeepStr),parse(artifactNumToKeepStr)); | ||
| parse(artifactDaysToKeepStr),parse(artifactNumToKeepStr), | ||
| parse(logDaysToKeepStr),parse(logNumToKeepStr)); | ||
| } | ||
|
|
||
| public static int parse(String p) { | ||
|
|
@@ -93,30 +109,61 @@ public static int parse(String p) { | |
|
|
||
| /** | ||
| * @deprecated since 1.350. | ||
| * Use {@link #LogRotator(int, int, int, int)} | ||
| * Use {@link #LogRotator(int, int, int, int, int, int)} | ||
| */ | ||
| @Deprecated | ||
| public LogRotator(int daysToKeep, int numToKeep) { | ||
| this(daysToKeep, numToKeep, -1, -1); | ||
| this(daysToKeep, numToKeep, -1, -1, -1, -1); | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated since FIXME | ||
| * Use {@link #LogRotator(int, int, int, int, int, int)} | ||
| */ | ||
| @Deprecated | ||
| public LogRotator(int daysToKeep, int numToKeep, int artifactDaysToKeep, int artifactNumToKeep) { | ||
| this(daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep, -1, -1); | ||
| } | ||
|
|
||
| public LogRotator(int daysToKeep, int numToKeep, int artifactDaysToKeep, int artifactNumToKeep, int logDaysToKeep, int logNumToKeep) { | ||
| this.daysToKeep = daysToKeep; | ||
| this.numToKeep = numToKeep; | ||
| this.artifactDaysToKeep = artifactDaysToKeep; | ||
| this.artifactNumToKeep = artifactNumToKeep; | ||
|
|
||
| this.logDaysToKeep = logDaysToKeep; | ||
| this.logNumToKeep = logNumToKeep; | ||
|
|
||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") | ||
| public void perform(Job<?,?> job) throws IOException, InterruptedException { | ||
| LOGGER.log(FINE, "Running the log rotation for {0} with numToKeep={1} daysToKeep={2} artifactNumToKeep={3} artifactDaysToKeep={4}", new Object[] {job, numToKeep, daysToKeep, artifactNumToKeep, artifactDaysToKeep}); | ||
|
|
||
| LOGGER.log(FINE, "Running the log rotation for {0} with numToKeep={1} daysToKeep={2} artifactNumToKeep={3} " + | ||
| "artifactDaysToKeep={4} logNumToKeep={5} logDaysToKeep={6}", new Object[] {job, numToKeep, daysToKeep, | ||
| artifactNumToKeep, artifactDaysToKeep, logNumToKeep, logDaysToKeep}); | ||
|
|
||
| performDeletionOf("build", job, numToKeep, daysToKeep, Run::delete); | ||
| performDeletionOf("artifacts", job, artifactNumToKeep, artifactDaysToKeep, Run::deleteArtifacts); | ||
| performDeletionOf("log", job, logNumToKeep, logDaysToKeep, Run::deleteLog); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes build metadata based on the parameters passed. | ||
| * | ||
| * Each build or build metadata have different delete functions as specified in {@link Run} | ||
| * @param buildMetaData String indicating whether builds, artifacts, or logs are being deleted (used for logging) | ||
| * @param job the Jenkins job whose builds and/or metadata are to be deleted | ||
| * @param numToKeep the number of the most recent builds to keep | ||
| * @param daysToKeep the number of days to keep the most recent builds up until | ||
| * @param runAction the deletion function passed | ||
| * @throws IOException if fail to delete | ||
| * @since FIXME | ||
| */ | ||
| private void performDeletionOf(String buildMetaData, Job<?,?> job, Integer numToKeep, Integer daysToKeep, | ||
| RunAction runAction) throws IOException{ | ||
| // always keep the last successful and the last stable builds | ||
| Run lsb = job.getLastSuccessfulBuild(); | ||
| Run lstb = job.getLastStableBuild(); | ||
|
|
||
| if(numToKeep!=-1) { | ||
| if(numToKeep!=null && numToKeep!=-1) { | ||
| // Note that RunList.size is deprecated, and indeed here we are loading all the builds of the job. | ||
| // However we would need to load the first numToKeep anyway, just to skip over them; | ||
| // and we would need to load the rest anyway, to delete them. | ||
|
|
@@ -127,12 +174,12 @@ public void perform(Job<?,?> job) throws IOException, InterruptedException { | |
| if (shouldKeepRun(r, lsb, lstb)) { | ||
| continue; | ||
| } | ||
| LOGGER.log(FINE, "{0} is to be removed", r); | ||
| r.delete(); | ||
| LOGGER.log(FINE, "{0}'s {1} to be removed", new Object[] {r, buildMetaData}); | ||
| runAction.call(r); | ||
| } | ||
| } | ||
|
|
||
| if(daysToKeep!=-1) { | ||
| if(daysToKeep!= null && daysToKeep!=-1) { | ||
| Calendar cal = new GregorianCalendar(); | ||
| cal.add(Calendar.DAY_OF_YEAR,-daysToKeep); | ||
| Run r = job.getFirstBuild(); | ||
|
|
@@ -141,35 +188,8 @@ public void perform(Job<?,?> job) throws IOException, InterruptedException { | |
| break; | ||
| } | ||
| if (!shouldKeepRun(r, lsb, lstb)) { | ||
| LOGGER.log(FINE, "{0} is to be removed", r); | ||
| r.delete(); | ||
| } | ||
| r = r.getNextBuild(); | ||
| } | ||
| } | ||
|
|
||
| if(artifactNumToKeep!=null && artifactNumToKeep!=-1) { | ||
| List<? extends Run<?,?>> builds = job.getBuilds(); | ||
| for (Run r : copy(builds.subList(Math.min(builds.size(), artifactNumToKeep), builds.size()))) { | ||
| if (shouldKeepRun(r, lsb, lstb)) { | ||
| continue; | ||
| } | ||
| LOGGER.log(FINE, "{0} is to be purged of artifacts", r); | ||
| r.deleteArtifacts(); | ||
| } | ||
| } | ||
|
|
||
| if(artifactDaysToKeep!=null && artifactDaysToKeep!=-1) { | ||
| Calendar cal = new GregorianCalendar(); | ||
| cal.add(Calendar.DAY_OF_YEAR,-artifactDaysToKeep); | ||
| Run r = job.getFirstBuild(); | ||
| while (r != null) { | ||
| if (tooNew(r, cal)) { | ||
| break; | ||
| } | ||
| if (!shouldKeepRun(r, lsb, lstb)) { | ||
| LOGGER.log(FINE, "{0} is to be purged of artifacts", r); | ||
| r.deleteArtifacts(); | ||
| LOGGER.log(FINE, "{0}'s {1} to be removed", new Object[] {r, buildMetaData}); | ||
| runAction.call(r); | ||
| } | ||
| r = r.getNextBuild(); | ||
| } | ||
|
|
@@ -228,6 +248,14 @@ public int getArtifactNumToKeep() { | |
| return unbox(artifactNumToKeep); | ||
| } | ||
|
|
||
| public int getLogDaysToKeep() { | ||
| return unbox(logDaysToKeep); | ||
| } | ||
|
|
||
| public int getLogNumToKeep() { | ||
| return unbox(logNumToKeep); | ||
| } | ||
|
|
||
| public String getDaysToKeepStr() { | ||
| return toString(daysToKeep); | ||
| } | ||
|
|
@@ -244,6 +272,14 @@ public String getArtifactNumToKeepStr() { | |
| return toString(artifactNumToKeep); | ||
| } | ||
|
|
||
| public String getLogDaysToKeepStr() { | ||
| return toString(logDaysToKeep); | ||
| } | ||
|
|
||
| public String getLogNumToKeepStr() { | ||
| return toString(logNumToKeep); | ||
| } | ||
|
|
||
| private int unbox(Integer i) { | ||
| return i==null ? -1: i; | ||
| } | ||
|
|
@@ -253,6 +289,15 @@ private String toString(Integer i) { | |
| return String.valueOf(i); | ||
| } | ||
|
|
||
| /** | ||
| * Interface to allow for lambda functions to run a call on a given run. | ||
| * @since FIXME | ||
| */ | ||
| @FunctionalInterface | ||
| interface RunAction { | ||
| void call(Run<?,?> r) throws IOException; | ||
| } | ||
|
|
||
| @Extension @Symbol("logRotator") | ||
| public static final class LRDescriptor extends BuildDiscarderDescriptor { | ||
| public String getDisplayName() { | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
Binary file modified
BIN
-3 Bytes
(99%)
test/src/test/resources/jenkins/model/BuildDiscarderPropertyTest/buildDiscarderField.zip
Binary file not shown.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It makes an assumption that the logs are stored on the disk.
Some plugins like Logstash plugin do not actually do that, especially if there is External Logging implementation like jenkinsci/logstash-plugin#18 in place
Not a big problem for this PR as long as the method is overridable.
But it would great to ensure it does not blow up if the log file does not exist