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

AbstractFolder.reloadThis & .addLoadedChild #378

Merged
merged 1 commit into from
Feb 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,13 +95,15 @@
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.acegisecurity.AccessDeniedException;
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 @@
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 @@
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 @@ -347,10 +347,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 @@
@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