From db07f253fc17b78fd5119b9354591477b2efdc14 Mon Sep 17 00:00:00 2001 From: stephenconnolly Date: Mon, 19 May 2008 03:50:45 +0000 Subject: [PATCH] better to have the demand start utility method in computer... it keeps the retention strategies simpler git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@9446 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Computer.java | 12 ++++++ .../java/hudson/slaves/RetentionStrategy.java | 37 +++++++------------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index e3c95d7a38..5179999b68 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -1,6 +1,7 @@ package hudson.model; import hudson.EnvVars; +import hudson.Util; import hudson.slaves.ComputerLauncher; import hudson.slaves.RetentionStrategy; import hudson.node_monitors.NodeMonitor; @@ -280,6 +281,17 @@ public abstract class Computer extends AbstractModelObject { return firstIdle; } + /** + * Returns the time when this computer first became in demand. + */ + public final long getDemandStartMilliseconds() { + long firstDemand = Long.MAX_VALUE; + for (Queue.Item item : Hudson.getInstance().getQueue().getBuildableItems(this)) { + firstDemand = Math.min(item.buildableStartMilliseconds, firstDemand); + } + return firstDemand; + } + /** * Called by {@link Executor} to kill excessive executors from this computer. */ diff --git a/core/src/main/java/hudson/slaves/RetentionStrategy.java b/core/src/main/java/hudson/slaves/RetentionStrategy.java index 09af34bb2a..ae1a798931 100644 --- a/core/src/main/java/hudson/slaves/RetentionStrategy.java +++ b/core/src/main/java/hudson/slaves/RetentionStrategy.java @@ -149,30 +149,21 @@ public abstract class RetentionStrategy implements Describab */ public synchronized long check(SlaveComputer c) { if (c.isOffline()) { - final Queue queue = Hudson.getInstance().getQueue(); - for (Queue.Item item : queue.getBuildableItems(c)) { - final long demandMilliseconds = System.currentTimeMillis() - item.buildableStartMilliseconds; - if (demandMilliseconds > - TimeUnit.MILLISECONDS.convert(inDemandDelay, TimeUnit.MINUTES)) { - // we've been in demand for long enough - logger.log(Level.INFO, "Launching computer {0} as it has been in demand for {1}", - new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(demandMilliseconds)}); - if (c.isLaunchSupported()) - c.launch(); - - break; - } + final long demandMilliseconds = System.currentTimeMillis() - c.getDemandStartMilliseconds(); + if (demandMilliseconds > TimeUnit.MILLISECONDS.convert(inDemandDelay, TimeUnit.MINUTES)) { + // we've been in demand for long enough + logger.log(Level.INFO, "Launching computer {0} as it has been in demand for {1}", + new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(demandMilliseconds)}); + if (c.isLaunchSupported()) + c.launch(); } - } else { - if (c.isIdle()) { - final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds(); - if (idleMilliseconds > - TimeUnit.MILLISECONDS.convert(inDemandDelay, TimeUnit.MINUTES)) { - // we've been idle for long enough - logger.log(Level.INFO, "Disconnecting computer {0} as it has been idle for {1}", - new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(idleMilliseconds)}); - c.disconnect(); - } + } else if (c.isIdle()) { + final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds(); + if (idleMilliseconds > TimeUnit.MILLISECONDS.convert(inDemandDelay, TimeUnit.MINUTES)) { + // we've been idle for long enough + logger.log(Level.INFO, "Disconnecting computer {0} as it has been idle for {1}", + new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(idleMilliseconds)}); + c.disconnect(); } } return 1;