Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1950 from jglick/WebAppMain.contextDestroyed-logging
[JENKINS-24696] WebAppMain.contextDestroyed produces weird errors
  • Loading branch information
jglick committed Jan 11, 2016
2 parents dda00aa + d3ebb8a commit 8cfa1e8
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 129 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/hudson/WebAppMain.java
Expand Up @@ -396,8 +396,10 @@ public void contextDestroyed(ServletContextEvent event) {
if(instance!=null)
instance.cleanUp();
Thread t = initThread;
if (t!=null)
if (t != null && t.isAlive()) {
LOGGER.log(Level.INFO, "Shutting down a Jenkins instance that was still starting up", new Throwable("reason"));
t.interrupt();
}

// Logger is in the system classloader, so if we don't do this
// the whole web app will never be undepoyed.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/init/impl/GroovyInitScript.java
Expand Up @@ -38,6 +38,6 @@
public class GroovyInitScript {
@Initializer(after=JOB_LOADED)
public static void init(Jenkins j) {
new GroovyHookScript("init").run();
new GroovyHookScript("init", j.servletContext, j.getRootDir(), j.getPluginManager().uberClassLoader).run();
}
}
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/util/BootFailure.java
Expand Up @@ -40,7 +40,10 @@ public void publish(ServletContext context, @CheckForNull File home) {
LOGGER.log(Level.SEVERE, "Failed to initialize Jenkins",this);

WebApp.get(context).setApp(this);
new GroovyHookScript("boot-failure")
if (home == null) {
return;
}
new GroovyHookScript("boot-failure", context, home, BootFailure.class.getClassLoader())
.bind("exception",this)
.bind("home",home)
.bind("servletContext", context)
Expand Down
36 changes: 25 additions & 11 deletions core/src/main/java/jenkins/util/groovy/GroovyHookScript.java
Expand Up @@ -3,18 +3,18 @@
import groovy.lang.Binding;
import groovy.lang.GroovyCodeSource;
import groovy.lang.GroovyShell;
import jenkins.model.Jenkins;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;

import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.servlet.ServletContext;
import jenkins.model.Jenkins;

/**
* A collection of Groovy scripts that are executed as various hooks.
Expand All @@ -40,9 +40,24 @@
public class GroovyHookScript {
private final String hook;
private final Binding bindings = new Binding();
private final ServletContext servletContext;
private final File home;
private final ClassLoader loader;

@Deprecated
public GroovyHookScript(String hook) {
this(hook, Jenkins.getActiveInstance());
}

private GroovyHookScript(String hook, Jenkins j) {
this(hook, j.servletContext, j.getRootDir(), j.getPluginManager().uberClassLoader);
}

public GroovyHookScript(String hook, @Nonnull ServletContext servletContext, @Nonnull File home, @Nonnull ClassLoader loader) {
this.hook = hook;
this.servletContext = servletContext;
this.home = home;
this.loader = loader;
}

public GroovyHookScript bind(String name, Object o) {
Expand All @@ -55,34 +70,33 @@ public Binding getBindings() {
}

public void run() {
Jenkins j = Jenkins.getInstance();
final String hookGroovy = hook+".groovy";
final String hookGroovyD = hook+".groovy.d";

try {
URL bundled = j.servletContext.getResource("/WEB-INF/"+ hookGroovy);
URL bundled = servletContext.getResource("/WEB-INF/"+ hookGroovy);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute /WEB-INF/"+hookGroovy,e);
}

Set<String> resources = j.servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
Set<String> resources = servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
if (resources!=null) {
// sort to execute them in a deterministic order
for (String res : new TreeSet<String>(resources)) {
try {
URL bundled = j.servletContext.getResource(res);
URL bundled = servletContext.getResource(res);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute " + res, e);
}
}
}

File script = new File(j.getRootDir(), hookGroovy);
File script = new File(home, hookGroovy);
execute(script);

File scriptD = new File(j.getRootDir(), hookGroovyD);
File scriptD = new File(home, hookGroovyD);
if (scriptD.isDirectory()) {
File[] scripts = scriptD.listFiles(new FileFilter() {
public boolean accept(File f) {
Expand Down Expand Up @@ -129,7 +143,7 @@ protected void execute(GroovyCodeSource s) {
* Can be used to customize the environment in which the script runs.
*/
protected GroovyShell createShell() {
return new GroovyShell(Jenkins.getInstance().getPluginManager().uberClassLoader, bindings);
return new GroovyShell(loader, bindings);
}

private static final Logger LOGGER = Logger.getLogger(GroovyHookScript.class.getName());
Expand Down
115 changes: 0 additions & 115 deletions test/src/test/groovy/hudson/util/BootFailureTest.groovy

This file was deleted.

0 comments on commit 8cfa1e8

Please sign in to comment.