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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
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 java.util.Map;
27 import java.util.WeakHashMap;
28
29 import hudson.model.Computer;
30 import jenkins.model.Jenkins;
31 import hudson.model.Node;
32 import hudson.model.PeriodicWork;
33 import hudson.Extension;
34
35 /**
36 * Periodically checks the slaves and try to reconnect dead slaves.
37 *
38 * @author Kohsuke Kawaguchi
39 * @author Stephen Connolly
40 */
41 @Extension
42 public class ComputerRetentionWork extends PeriodicWork {
43
44 /**
45 * Use weak hash map to avoid leaking {@link Computer}.
46 */
47 private final Map<Computer, Long> nextCheck = new WeakHashMap<Computer, Long>();
48
49 public long getRecurrencePeriod() {
50 return MIN;
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 @SuppressWarnings("unchecked")
57 protected void doRun() {
58 final long startRun = System.currentTimeMillis();
59 for (Computer c : Jenkins.getInstance().getComputers()) {
60 Node n = c.getNode();
61 if (n!=null && n.isHoldOffLaunchUntilSave())
62 continue;
63 if (!nextCheck.containsKey(c) || startRun > nextCheck.get(c)) {
64 // at the moment I don't trust strategies to wait more than 60 minutes
65 // strategies need to wait at least one minute
66 final long waitInMins = Math.min(1, Math.max(60, c.getRetentionStrategy().check(c)));
67 nextCheck.put(c, startRun + waitInMins*1000*60 /*MINS->MILLIS*/);
68 }
69 }
70 }
71 }
72