Skip to content

Commit

Permalink
Merge pull request #85 from jglick/PingThread-JENKINS-35190
Browse files Browse the repository at this point in the history
[JENKINS-35190] PingThread fixes
  • Loading branch information
jglick committed May 31, 2016
2 parents c0cf331 + 7ff0a9a commit 59e33a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/hudson/remoting/Launcher.java
Expand Up @@ -76,6 +76,8 @@
import java.security.KeyManagementException;
import java.security.SecureRandom;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

Expand Down Expand Up @@ -521,14 +523,22 @@ public static void main(InputStream is, OutputStream os, Mode mode, boolean perf
long timeout = 1000 * Long.parseLong(
System.getProperty("hudson.remoting.Launcher.pingTimeoutSec", "240")),
interval = 1000 * Long.parseLong(
System.getProperty("hudson.remoting.Launcher.pingIntervalSec", "600"));
System.getProperty("hudson.remoting.Launcher.pingIntervalSec", /* was "600" but this duplicates ChannelPinger */ "0"));
Logger.getLogger(PingThread.class.getName()).log(Level.FINE, "performPing={0} timeout={1} interval={2}", new Object[] {performPing, timeout, interval});
if (performPing && timeout > 0 && interval > 0) {
new PingThread(channel, timeout, interval) {
@Deprecated
@Override
protected void onDead() {
System.err.println("Ping failed. Terminating");
System.exit(-1);
}
@Override
protected void onDead(Throwable cause) {
System.err.println("Ping failed. Terminating");
cause.printStackTrace();
System.exit(-1);
}
}.start();
}
channel.join();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hudson/remoting/PingThread.java
Expand Up @@ -32,6 +32,7 @@
import java.util.logging.Logger;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.logging.Level;

/**
* Periodically perform a ping.
Expand Down Expand Up @@ -101,15 +102,18 @@ public void run() {
}

private void ping() throws IOException, InterruptedException {
LOGGER.log(Level.FINE, "pinging {0}", channel.getName());
Future<?> f = channel.callAsync(new Ping());
long start = System.currentTimeMillis();

long end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout);
long remaining = end - System.nanoTime();

do {
LOGGER.log(Level.FINE, "waiting {0}s on {1}", new Object[] {TimeUnit.NANOSECONDS.toSeconds(remaining), channel.getName()});
try {
f.get(Math.max(1,remaining),TimeUnit.NANOSECONDS);
LOGGER.log(Level.FINE, "ping succeeded on {0}", channel.getName());
return;
} catch (ExecutionException e) {
if (e.getCause() instanceof RequestAbortedException)
Expand Down

0 comments on commit 59e33a4

Please sign in to comment.