Skip to content

Commit

Permalink
Merge pull request #2931 from jglick/ReloadConfigurationCommand
Browse files Browse the repository at this point in the history
[JENKINS-45256] The reload-configuration CLI command ought to wait until the reload is finished

(cherry picked from commit cc1615b)
  • Loading branch information
oleg-nenashev authored and olivergondza committed Jul 20, 2017
1 parent 8cd6fd8 commit de4f285
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
25 changes: 23 additions & 2 deletions core/src/main/java/hudson/cli/ReloadConfigurationCommand.java
Expand Up @@ -25,7 +25,10 @@
package hudson.cli;

import hudson.Extension;
import hudson.util.HudsonIsLoading;
import hudson.util.JenkinsReloadFailed;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.WebApp;

/**
* Reload everything from the file system.
Expand All @@ -43,8 +46,26 @@ public String getShortDescription() {

@Override
protected int run() throws Exception {
Jenkins.getActiveInstance().doReload();
return 0;
Jenkins j = Jenkins.getInstance();
// Or perhaps simpler to inline the thread body of doReload?
j.doReload();
Object app;
while ((app = WebApp.get(j.servletContext).getApp()) instanceof HudsonIsLoading) {
Thread.sleep(100);
}
if (app instanceof Jenkins) {
return 0;
} else if (app instanceof JenkinsReloadFailed) {
Throwable t = ((JenkinsReloadFailed) app).cause;
if (t instanceof Exception) {
throw (Exception) t;
} else {
throw new RuntimeException(t);
}
} else {
stderr.println("Unexpected status " + app);
return 1; // could throw JenkinsReloadFailed.cause if it were not deprecated
}
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/JenkinsReloadFailed.java
Expand Up @@ -9,7 +9,7 @@
* @author Kohsuke Kawaguchi
*/
public class JenkinsReloadFailed extends BootFailure {
@Restricted(NoExternalUse.class) @Deprecated
@Restricted(NoExternalUse.class)
public final Throwable cause;

public JenkinsReloadFailed(Throwable cause) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -3988,7 +3988,7 @@ public synchronized HttpResponse doReload() throws IOException {
LOGGER.log(Level.WARNING, "Reloading Jenkins as requested by {0}", getAuthentication().getName());

// engage "loading ..." UI and then run the actual task in a separate thread
servletContext.setAttribute("app", new HudsonIsLoading());
WebApp.get(servletContext).setApp(new HudsonIsLoading());

new Thread("Jenkins config reload thread") {
@Override
Expand Down Expand Up @@ -4027,7 +4027,7 @@ public void reload() throws IOException, InterruptedException, ReactorException

User.reload();
queue.load();
servletContext.setAttribute("app", this);
WebApp.get(servletContext).setApp(this);
}

/**
Expand Down
Expand Up @@ -173,13 +173,6 @@ private void reloadJenkinsConfigurationViaCliAndWait() throws Exception {
final CLICommandInvoker.Result result = command.invoke();

assertThat(result, succeededSilently());

// reload-configuration is performed in a separate thread
// we have to wait until it finishes
while (!(j.jenkins.servletContext.getAttribute("app") instanceof Jenkins)) {
System.out.println("Jenkins reload operation is performing, sleeping 1s...");
Thread.sleep(1000);
}
}

private void replace(String path, String search, String replace) {
Expand Down

0 comments on commit de4f285

Please sign in to comment.