From 0b43abb7b3b0a884cc6fcdb99bf03f882d07b74d Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 1 Apr 2014 15:15:23 -0700 Subject: [PATCH] run this asynchronously so that slaves can be used right away --- .../JnlpSlaveRestarterInstaller.java | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/jenkins/slaves/restarter/JnlpSlaveRestarterInstaller.java b/core/src/main/java/jenkins/slaves/restarter/JnlpSlaveRestarterInstaller.java index f69db22336..a22a868d70 100644 --- a/core/src/main/java/jenkins/slaves/restarter/JnlpSlaveRestarterInstaller.java +++ b/core/src/main/java/jenkins/slaves/restarter/JnlpSlaveRestarterInstaller.java @@ -9,6 +9,7 @@ import hudson.remoting.EngineListener; import hudson.remoting.EngineListenerAdapter; import hudson.remoting.VirtualChannel; import hudson.slaves.ComputerListener; +import jenkins.model.Jenkins.MasterComputer; import java.io.IOException; import java.io.Serializable; @@ -31,62 +32,68 @@ import static java.util.logging.Level.*; @Extension public class JnlpSlaveRestarterInstaller extends ComputerListener implements Serializable { @Override - public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException { - final List restarters = new ArrayList(SlaveRestarter.all()); + public void onOnline(final Computer c, final TaskListener listener) throws IOException, InterruptedException { + MasterComputer.threadPoolForRemoting.submit(new java.util.concurrent.Callable() { + @Override + public Object call() throws Exception { + final List restarters = new ArrayList(SlaveRestarter.all()); - VirtualChannel ch = c.getChannel(); - if (ch==null) return; // defensive check + VirtualChannel ch = c.getChannel(); + if (ch==null) return null; // defensive check - List effective = null; - try { - effective = ch.call(new Callable, IOException>() { - public List call() throws IOException { - Engine e = Engine.current(); - if (e == null) return null; // not running under Engine + List effective = null; + try { + effective = ch.call(new Callable, IOException>() { + public List call() throws IOException { + Engine e = Engine.current(); + if (e == null) return null; // not running under Engine - try { - Engine.class.getMethod("addListener", EngineListener.class); - } catch (NoSuchMethodException _) { - return null; // running with older version of remoting that doesn't support adding listener - } + try { + Engine.class.getMethod("addListener", EngineListener.class); + } catch (NoSuchMethodException _) { + return null; // running with older version of remoting that doesn't support adding listener + } - // filter out ones that doesn't apply - for (Iterator itr = restarters.iterator(); itr.hasNext(); ) { - SlaveRestarter r = itr.next(); - if (!r.canWork()) - itr.remove(); - } + // filter out ones that doesn't apply + for (Iterator itr = restarters.iterator(); itr.hasNext(); ) { + SlaveRestarter r = itr.next(); + if (!r.canWork()) + itr.remove(); + } - e.addListener(new EngineListenerAdapter() { - @Override - public void onDisconnect() { - try { - for (SlaveRestarter r : restarters) { + e.addListener(new EngineListenerAdapter() { + @Override + public void onDisconnect() { try { - LOGGER.info("Restarting slave via "+r); - r.restart(); - } catch (Exception x) { - LOGGER.log(SEVERE, "Failed to restart slave with "+r, x); + for (SlaveRestarter r : restarters) { + try { + LOGGER.info("Restarting slave via "+r); + r.restart(); + } catch (Exception x) { + LOGGER.log(SEVERE, "Failed to restart slave with "+r, x); + } + } + } finally { + // if we move on to the reconnection without restart, + // don't let the current implementations kick in when the slave loses connection again + restarters.clear(); } } - } finally { - // if we move on to the reconnection without restart, - // don't let the current implementations kick in when the slave loses connection again - restarters.clear(); - } + }); + + return restarters; } }); - - return restarters; + } catch (IOException e) { + e.printStackTrace(listener.error("Failed to install restarter")); + // don't let this fail the slave connection } - }); - } catch (IOException e) { - e.printStackTrace(listener.error("Failed to install restarter")); - // don't let this fail the slave connection - } - // TODO: report this to GUI - listener.getLogger().println("Effective SlaveRestarter on "+c.getDisplayName()+": "+effective); + // TODO: report this to GUI + listener.getLogger().println("Effective SlaveRestarter on " + c.getDisplayName() + ": " + effective); + return null; + } + }); } private static final Logger LOGGER = Logger.getLogger(JnlpSlaveRestarterInstaller.class.getName()); -- GitLab