File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\slaves\ChannelPinger.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Nathan Parry
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.slaves;
25
26 import hudson.Extension;
27 import hudson.FilePath;
28 import hudson.model.Computer;
29 import hudson.model.Slave;
30 import hudson.model.TaskListener;
31 import hudson.remoting.Callable;
32 import hudson.remoting.Channel;
33 import hudson.remoting.PingThread;
34
35 import java.io.IOException;
36 import java.util.concurrent.atomic.AtomicBoolean;
37 import java.util.logging.Logger;
38
39 import static java.util.logging.Level.*;
40
41 /**
42 * Establish a periodic ping to keep connections between {@link Slave slaves}
43 * and the main Jenkins node alive. This prevents network proxies from
44 * terminating connections that are idle for too long.
45 *
46 * @since 1.405
47 */
48 @Extension
49 public class ChannelPinger extends ComputerListener {
50 private static final Logger LOGGER = Logger.getLogger(ChannelPinger.class.getName());
51 private static final String SYS_PROPERTY_NAME = ChannelPinger.class.getName() + ".pingInterval";
52
53 /**
54 * Interval for the ping in minutes.
55 */
56 private int pingInterval = 5;
57
58 public ChannelPinger() {
59 String interval = System.getProperty(SYS_PROPERTY_NAME);
60 if (interval != null) {
61 try {
62 pingInterval = Integer.valueOf(interval);
63 } catch (NumberFormatException e) {
64 LOGGER.warning("Ignoring invalid " + SYS_PROPERTY_NAME + "=" + interval);
65 }
66 }
67 }
68
69 @Override
70 public void preOnline(Computer c, Channel channel, FilePath root, TaskListener listener) {
71 if (pingInterval < 1) {
72 LOGGER.fine("Slave ping is disabled");
73 return;
74 }
75
76 try {
77 channel.call(new SetUpRemotePing(pingInterval));
78 LOGGER.fine("Set up a remote ping for " + c.getName());
79 } catch (Exception e) {
80 LOGGER.severe("Failed to set up a ping for " + c.getName());
81 }
82
83 // set up ping from both directions, so that in case of a router dropping a connection,
84 // both sides can notice it and take compensation actions.
85 setUpPingForChannel(channel, pingInterval);
86 }
87
88 private static class SetUpRemotePing implements Callable<Void, IOException> {
89 private static final long serialVersionUID = -2702219700841759872L;
90 private int pingInterval;
91 public SetUpRemotePing(int pingInterval) {
92 this.pingInterval = pingInterval;
93 }
94
95 public Void call() throws IOException {
96 setUpPingForChannel(Channel.current(), pingInterval);
97 return null;
98 }
99 }
100
101 private static void setUpPingForChannel(final Channel channel, int interval) {
102 final AtomicBoolean isInClosed = new AtomicBoolean(false);
103 final PingThread t = new PingThread(channel, interval * 60 * 1000) {
104 protected void onDead(Throwable cause) {
105 try {
106 if (isInClosed.get()) {
107 LOGGER.log(FINE,"Ping failed after the channel is already partially closed",cause);
108 } else {
109 LOGGER.log(INFO,"Ping failed. Terminating the channel.",cause);
110 channel.close(cause);
111 }
112 } catch (IOException e) {
113 LOGGER.log(SEVERE,"Failed to terminate the channel: ",e);
114 }
115 }
116 protected void onDead() {
117 onDead(null);
118 }
119 };
120
121 channel.addListener(new Channel.Listener() {
122 @Override
123 public void onClosed(Channel channel, IOException cause) {
124 LOGGER.fine("Terminating ping thread for " + channel);
125 isInClosed.set(true);
126 t.interrupt(); // make sure the ping thread is terminated
127 }
128 });
129
130 t.start();
131 LOGGER.fine("Ping thread started for " + channel + " with a " + interval + " minute interval");
132 }
133 }
134