Skip to content

Commit

Permalink
AbstractFolder.reloadThis & .addLoadedChild (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 26, 2024
2 parents ca05711 + 7b6cbb3 commit 7c78021
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.cloudbees.hudson.plugins.folder;

import com.cloudbees.hudson.plugins.folder.computed.ComputedFolder;
import com.cloudbees.hudson.plugins.folder.computed.FolderComputation;
import com.cloudbees.hudson.plugins.folder.config.AbstractFolderConfiguration;
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetric;
import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetricDescriptor;
Expand Down Expand Up @@ -94,12 +95,14 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import javax.servlet.ServletException;
import jenkins.model.DirectlyModifiableTopLevelItemGroup;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import jenkins.model.ProjectNamingStrategy;
import jenkins.model.TransientActionFactory;
import net.sf.json.JSONObject;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.Beta;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
Expand Down Expand Up @@ -148,6 +151,10 @@ public abstract class AbstractFolder<I extends TopLevelItem> extends AbstractIte
private static final AtomicBoolean loadJobTotalRan = new AtomicBoolean();
private static final int TICK_INTERVAL = 15000;

/** Whether execution is currently inside {@link #reloadThis}. */
@Restricted(NoExternalUse.class)
protected static final ThreadLocal<Boolean> reloadingThis = ThreadLocal.withInitial(() -> false);

@Initializer(before=InitMilestone.JOB_LOADED, fatal=false)
public static void loadJobTotal() {
if (!loadJobTotalRan.compareAndSet(false, true)) {
Expand Down Expand Up @@ -404,13 +411,50 @@ protected final I itemsPut(String name, I item) {
return items.put(name, item);
}

/**
* Reloads this folder itself.
* Compared to {@link #load}, this method skips parts of {@link #onLoad} such as the call to {@link #loadChildren}.
* Nor will it set {@link Items#whileUpdatingByXml}.
* In the case of a {@link ComputedFolder} it also will not call {@link FolderComputation#load}.
*/
@SuppressWarnings("unchecked")
@Restricted(Beta.class)
public void reloadThis() throws IOException {
LOGGER.fine(() -> "reloadThis " + this);
checkPermission(Item.CONFIGURE);
getConfigFile().unmarshal(this);
boolean old = reloadingThis.get();
try {
reloadingThis.set(true);
onLoad(getParent(), getParent().getItemName(getRootDir(), this));
} finally {
reloadingThis.set(old);
}
}

Check warning on line 433 in src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 423-433 are not covered by tests

/**
* Adds an item to be the folder which was already loaded via {@link Items#load}.
* Unlike {@link DirectlyModifiableTopLevelItemGroup#add} this can be used even on a {@link ComputedFolder}.
*/
@Restricted(Beta.class)
public void addLoadedChild(I item, String name) throws IOException, IllegalArgumentException {
if (items.containsKey(name)) {

Check warning on line 441 in src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 441 is only partially covered, one branch is missing
throw new IllegalArgumentException("already an item '" + name + "'");

Check warning on line 442 in src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 442 is not covered by tests
}
itemsPut(item.getName(), item);
}

/**
* {@inheritDoc}
*/
@Override
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);
init();
if (reloadingThis.get()) {

Check warning on line 454 in src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 454 is only partially covered, one branch is missing
LOGGER.fine(() -> this + " skipping the rest of onLoad");
return;

Check warning on line 456 in src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 455-456 are not covered by tests
}
final Thread t = Thread.currentThread();
String n = t.getName();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,7 @@ public DescriptorImpl getDescriptor() {
if (!canAdd(item)) {
throw new IllegalArgumentException();
}
if (items.containsKey(name)) {
throw new IllegalArgumentException("already an item '" + name + "'");
}
itemsPut(item.getName(), item);
addLoadedChild(item, name);
return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public void onCreatedFromScratch() {
@Override
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);
if (reloadingThis.get()) {

Check warning on line 212 in src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 212 is only partially covered, one branch is missing
LOGGER.fine(() -> this + " skipping the rest of onLoad");
return;

Check warning on line 214 in src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 213-214 are not covered by tests
}
try {
FileUtils.forceMkdir(getComputationDir());
} catch (IOException x) {
Expand Down

0 comments on commit 7c78021

Please sign in to comment.