package hudson.model;

import hudson.model.Slave.ComputerImpl;
import hudson.triggers.SafeTimerTask;

/**
 * Periodically checks the slaves and try to reconnect dead slaves.
 *
 * @author Kohsuke Kawaguchi
 */
public class SlaveReconnectionWork extends SafeTimerTask {
    protected void doRun() {
        for(Slave s : Hudson.getInstance().getSlaves()) {
            ComputerImpl c = s.getComputer();
            if(c==null) // shouldn't happen, but let's be defensive
                continue;
            if(c.isOffline() && c.isStartSupported())
                c.tryReconnect();
        }
    }
}

@@ -67,37 +67,146 @@
 ort 
-hudson.triggers.SafeTimerTask
+static hudson.model.SlaveAvailabilityStrategy.*;%0Aimport hudson.triggers.SafeTimerTask;%0A%0Aimport java.util.Map;%0Aimport java.util.WeakHashMap
 ;%0A%0A/
@@ -396,24 +396,123 @@
 d doRun() %7B%0A
+        // use a weak hashmap%0A        Map%3CSlave, Long%3E nextCheck = new WeakHashMap%3CSlave, Long%3E();%0A
         for(
@@ -510,16 +510,17 @@
      for
+ 
 (Slave s
@@ -574,135 +574,735 @@
     
-ComputerImpl c = s.getComputer();%0A            if(c==null) // shouldn't happen, but let's be defensive%0A                cont
+if (!nextCheck.containsKey(s) %7C%7C System.currentTimeMillis() %3E nextCheck.get(s)) %7B%0A                final Queue queue = Hudson.getInstance().getQueue();%0A                boolean hasJob = false;%0A                for (Executor exec: s.getComputer().getExecutors()) %7B%0A                    if (!exec.isIdle()) %7B%0A                        hasJob = true;%0A                        break;%0A                    %7D%0A                %7D%0A                // TODO get only the items from the queue that can apply to this slave%0A                State state = new State(queue.getItems().length %3E 0, hasJob);%0A                // at the moment I don't trust strategies to wait more than 60 minutes%0A                // strategies need to wait at least one m
 inu
+t
 e
-;
 %0A   
@@ -1314,83 +1314,208 @@
     
-if(c.isOffline() && c.isStartSupported())%0A                c.tryReconnect();
+    final long waitInMins = Math.min(1, Math.max(60, s.getAvailabilityStrategy().check(s, state)));%0A                nextCheck.put(s, System.currentTimeMillis() + 60 * 1000 * waitInMins);%0A            %7D
 %0A   

Make the slave availability strategy actually do stuff!