Skip to content
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

[JENKINS-28379] Allow FingerprintFacet to block the deletion of Fingerprint #4478

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/java/hudson/model/Fingerprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,19 @@ void save(File file) throws IOException {
}
}

/**
* Returns a facet that blocks the deletion of the fingerprint.
* Returns null if no such facet.
* @since TODO
*/
public @CheckForNull FingerprintFacet getFacetBlockingDeletion() {
for (FingerprintFacet facet : facets) {
if (facet.isFingerprintDeletionBlocked())
return facet;
}
return null;
}

/**
* Update references to a renamed job in the fingerprint
*/
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/hudson/model/FingerprintCleanupThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/
package hudson.model;

import com.thoughtworks.xstream.converters.basic.DateConverter;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.Functions;
import jenkins.model.FingerprintFacet;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
Expand All @@ -52,6 +54,8 @@ public class FingerprintCleanupThread extends AsyncPeriodicWork {
static final String FINGERPRINTS_DIR_NAME = "fingerprints";
private static final Pattern FINGERPRINT_FILE_PATTERN = Pattern.compile("[0-9a-f]{28}\\.xml");

private static final DateConverter DATE_CONVERTER = new DateConverter();

public FingerprintCleanupThread() {
super("Fingerprint cleanup");
}
Expand Down Expand Up @@ -107,11 +111,15 @@ private void deleteIfEmpty(File dir) {
private boolean check(File fingerprintFile, TaskListener listener) {
try {
Fingerprint fp = loadFingerprint(fingerprintFile);
if (fp == null || !fp.isAlive()) {
if (fp == null || (!fp.isAlive() && fp.getFacetBlockingDeletion() == null) ) {
listener.getLogger().println("deleting obsolete " + fingerprintFile);
fingerprintFile.delete();
return true;
} else {
if (!fp.isAlive()) {
FingerprintFacet deletionBlockerFacet = fp.getFacetBlockingDeletion();
listener.getLogger().println(deletionBlockerFacet.getClass().getName() + " created on " + DATE_CONVERTER.toString(deletionBlockerFacet.getTimestamp()) + " blocked deletion of " + fingerprintFile);
}
// get the fingerprint in the official map so have the changes visible to Jenkins
// otherwise the mutation made in FingerprintMap can override our trimming.
fp = getFingerprint(fp);
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/jenkins/model/FingerprintFacet.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public long getTimestamp() {
return timestamp;
}

/**
* Returns whether Fingerprint deletion has been blocked by this Facet.
* Returns false by default. Override the default to block the deletion of the associated Fingerprint.
* @since TODO
*/
public boolean isFingerprintDeletionBlocked() {
return false;
}

/**
* Backdoor for {@link Fingerprint} to set itself to its facets.
* Public only because this needs to be accessible to {@link Fingerprint}. Do not call this method directly.
Expand Down