提交 0b43abb7 编写于 作者: K Kohsuke Kawaguchi

run this asynchronously so that slaves can be used right away

上级 62aa9b91
......@@ -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<SlaveRestarter> restarters = new ArrayList<SlaveRestarter>(SlaveRestarter.all());
public void onOnline(final Computer c, final TaskListener listener) throws IOException, InterruptedException {
MasterComputer.threadPoolForRemoting.submit(new java.util.concurrent.Callable<Object>() {
@Override
public Object call() throws Exception {
final List<SlaveRestarter> restarters = new ArrayList<SlaveRestarter>(SlaveRestarter.all());
VirtualChannel ch = c.getChannel();
if (ch==null) return; // defensive check
VirtualChannel ch = c.getChannel();
if (ch==null) return null; // defensive check
List<SlaveRestarter> effective = null;
try {
effective = ch.call(new Callable<List<SlaveRestarter>, IOException>() {
public List<SlaveRestarter> call() throws IOException {
Engine e = Engine.current();
if (e == null) return null; // not running under Engine
List<SlaveRestarter> effective = null;
try {
effective = ch.call(new Callable<List<SlaveRestarter>, IOException>() {
public List<SlaveRestarter> 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<SlaveRestarter> itr = restarters.iterator(); itr.hasNext(); ) {
SlaveRestarter r = itr.next();
if (!r.canWork())
itr.remove();
}
// filter out ones that doesn't apply
for (Iterator<SlaveRestarter> 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());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册