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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
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.io.IOException;
27 import java.util.logging.Logger;
28
29 import static hudson.util.TimeUnit2.*;
30 import static java.util.logging.Level.*;
31
32 /**
33 * {@link RetentionStrategy} implementation for {@link AbstractCloudComputer} that terminates
34 * it if it remains idle for X minutes.
35 *
36 * @author Kohsuke Kawaguchi
37 * @since 1.382
38 */
39 public class CloudRetentionStrategy extends RetentionStrategy<AbstractCloudComputer> {
40 private int idleMinutes;
41
42 public CloudRetentionStrategy(int idleMinutes) {
43 this.idleMinutes = idleMinutes;
44 }
45
46 public synchronized long check(AbstractCloudComputer c) {
47 if (c.isIdle() && !disabled) {
48 final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds();
49 if (idleMilliseconds > MINUTES.toMillis(idleMinutes)) {
50 LOGGER.info("Disconnecting "+c.getName());
51 try {
52 c.getNode().terminate();
53 } catch (InterruptedException e) {
54 LOGGER.log(WARNING,"Failed to terminate "+c.getName(),e);
55 } catch (IOException e) {
56 LOGGER.log(WARNING,"Failed to terminate "+c.getName(),e);
57 }
58 }
59 }
60 return 1;
61 }
62
63 /**
64 * Try to connect to it ASAP.
65 */
66 @Override
67 public void start(AbstractCloudComputer c) {
68 c.connect(false);
69 }
70
71 private static final Logger LOGGER = Logger.getLogger(CloudRetentionStrategy.class.getName());
72
73 public static boolean disabled = Boolean.getBoolean(CloudRetentionStrategy.class.getName()+".disabled");
74 }
75