/** Returns the number of idle {@link Executor}s that can start working immediately. */ public int countIdle() { int n = 0; for (Executor e : executors) { if (e.isIdle()) n++; } return n; }
/** * Returns the time when this computer last became idle. * * <p>If this computer is already idle, the return value will point to the time in the past since * when this computer has been idle. * * <p>If this computer is busy, the return value will point to the time in the future where this * computer will be expected to become free. */ public final long getIdleStartMilliseconds() { long firstIdle = Long.MIN_VALUE; for (Executor e : oneOffExecutors) { firstIdle = Math.max(firstIdle, e.getIdleStartMilliseconds()); } for (Executor e : executors) { firstIdle = Math.max(firstIdle, e.getIdleStartMilliseconds()); } return firstIdle; }
private void addNewExecutorIfNecessary() { Set<Integer> availableNumbers = new HashSet<Integer>(); for (int i = 0; i < numExecutors; i++) availableNumbers.add(i); for (Executor executor : executors) availableNumbers.remove(executor.getNumber()); for (Integer number : availableNumbers) { Executor e = new Executor(this, number); e.start(); executors.add(e); } }
@Override protected void performDelete() throws IOException, InterruptedException { // if a build is in progress. Cancel it. RunT lb = getLastBuild(); if (lb != null) { Executor e = lb.getExecutor(); if (e != null) { e.interrupt(); // should we block until the build is cancelled? } } super.performDelete(); }
private synchronized void setNumExecutors(int n) { if (numExecutors == n) return; // no-op int diff = n - numExecutors; this.numExecutors = n; if (diff < 0) { // send signal to all idle executors to potentially kill them off for (Executor e : executors) if (e.isIdle()) e.interrupt(); } else { // if the number is increased, add new ones addNewExecutorIfNecessary(); } }
/** Interrupt all {@link Executor}s. */ public void interrupt() { for (Executor e : executors) { e.interrupt(); } }
/** * Returns true if any of the executors are functioning. * * <p>Note that if an executor dies, we'll leave it in {@link #executors} until the administrator * yanks it out, so that we can see why it died. */ private boolean isAlive() { for (Executor e : executors) if (e.isAlive()) return true; return false; }
/** Returns true if this computer has some idle executors that can take more workload. */ public final boolean isPartiallyIdle() { for (Executor e : executors) if (e.isIdle()) return true; return false; }
/** Returns true if all the executors of this computer are idle. */ @Exported public final boolean isIdle() { if (!oneOffExecutors.isEmpty()) return false; for (Executor e : executors) if (!e.isIdle()) return false; return true; }
/** * Gets the current {@link Computer} that the build is running. This method only works when called * during a build, such as by {@link Publisher}, {@link BuildWrapper}, etc. */ public static Computer currentComputer() { Executor e = Executor.currentExecutor(); // If no executor then must be on master node return e != null ? e.getOwner() : Jenkins.getInstance().toComputer(); }