for (Queue.BuildableItem item : Queue.getInstance().getBuildableItems()) {
Queue queue = Queue.getInstance(); if (this instanceof Queue.Task) {
@Override @CheckForNull public Queue.Item itemInQueue() { return Queue.getInstance().getItem(id); } }
NodeAvailabilityMonitor(Computer computer) { this.computer = computer; queue = Queue.getInstance(); jenkins = Jenkins.getActiveInstance(); }
protected void cancelItem(Queue.Item item, String builderName, String label) { LOGGER.info("Cancelling Item "); try { if (item != null) { Queue queue = Queue.getInstance(); boolean canceled = queue.cancel(item); LOGGER.warning("Build " + label + " " + builderName + " has been canceled"); } } catch (Exception e) { LOGGER.log(Level.SEVERE, "Exception caught trying to terminate slave", e); } }
@Override public boolean cancel(boolean mayInterruptIfRunning) { Queue.Item item = Queue.getInstance().getItem(itemID); if (item != null) { if (Queue.getInstance().cancel(item)) { LOGGER.log(Level.FINE, "canceled {0}", item); } else { LOGGER.log(Level.WARNING, "failed to cancel {0}", item); } } else { LOGGER.log(Level.FINE, "no such item {0} to cancel", itemID); } return super.cancel(mayInterruptIfRunning); } @Override public String toString() {
@Override public void run() { Queue.Item item = Queue.getInstance().getItem(task); if (item != null) { PrintStream logger; try { logger = listener.getLogger(); } catch (Exception x) { // IOException, InterruptedException LOGGER.log(WARNING, null, x); return; } logger.println("Still waiting to schedule task"); String why = item.getWhy(); if (why != null) { logger.println(why); } } } }, 15, TimeUnit.SECONDS);
@Override public void run() { Queue.Item item = Queue.getInstance().getItem(task); if (item != null) { PrintStream logger; try { logger = listener.getLogger(); } catch (Exception x) { // IOException, InterruptedException LOGGER.log(WARNING, null, x); return; } logger.println("Still waiting to schedule task"); String why = item.getWhy(); if (why != null) { logger.println(why); } } } }, 15, TimeUnit.SECONDS);
@Override protected void printWaitingMessage(TaskListener listener) { Queue.Item item = Queue.getInstance().getItem(itemID); String message = Messages.ExecutorPickle_waiting_to_resume(task.getFullDisplayName()); if (item == null) { // ??? listener.getLogger().println(message); return; } CauseOfBlockage causeOfBlockage = item.getCauseOfBlockage(); if (causeOfBlockage != null) { listener.getLogger().print(message + ": "); causeOfBlockage.print(listener); // note that in case of Messages.Queue_Unknown for WaitingItem this is not very helpful } else { listener.getLogger().println(message); } } @Override public boolean cancel(boolean mayInterruptIfRunning) {
/** * Duck-types {@link ParameterizedJobMixIn#scheduleBuild2(int, Action...)}. * @param quietPeriod seconds to wait before starting (normally 0) * @param actions various actions to associate with the scheduling, such as {@link ParametersAction} or * {@link CauseAction} * @return a handle by which you may wait for the build to complete (or just start); or null if the build was not * actually scheduled for some reason */ @CheckForNull public Queue.Item scheduleBuild2(int quietPeriod, Action... actions) { if (!isBuildable()) { return null; } return Queue.getInstance().schedule2(this, quietPeriod, Arrays.asList(actions)).getItem(); }
@Override public void run() { Queue.Item item = Queue.getInstance().getItem(task); if (item != null) { PrintStream logger; try { logger = getContext().get(TaskListener.class).getLogger(); } catch (Exception x) { // IOException, InterruptedException LOGGER.log(WARNING, null, x); return; } logger.println("Still waiting to schedule task"); String why = item.getWhy(); if (why != null) { logger.println(why); } } } }, 15, TimeUnit.SECONDS);
@Override public String toString() { Queue.Item item = Queue.getInstance().getItem(itemID); if (item != null) { return "Trying to schedule " + task.getFullDisplayName() + "; blockage: " + item.getCauseOfBlockage(); } else { return "Trying to locate queue item #" + itemID; } } };
/** * Process RefReplicated events. * @param refReplicated the event */ public void gerritEvent(RefReplicated refReplicated) { replicationCache.put(refReplicated); boolean queueMaintenanceRequired = false; for (BlockedItem blockedItem : blockedItems.values()) { if (!blockedItem.canRun) { blockedItem.processRefReplicatedEvent(refReplicated); if (blockedItem.canRun) { queueMaintenanceRequired = true; } } } if (queueMaintenanceRequired) { // force a maintenance of the queue to unblock builds Queue.getInstance().maintain(); } }
/** * Checks if there are any builds in queue that can only be built * by the argument computer. * Note: This method locks the queue; don't use excessively. * @param computer the computer to check assignment for * @return true if there are builds that can only be build by argument computer, otherwise false */ public static boolean hasNodeExclusiveItemInQueue(Computer computer) { boolean hasExclusive = false; Queue.Item[] queueItems = Queue.getInstance().getItems(); for (Queue.Item item : queueItems) { if (!canOtherNodeBuild(item, computer.getNode())) { hasExclusive = true; break; } } return hasExclusive; } }
@Override public void stop(Throwable cause) throws Exception { for (Queue.Item item : Queue.getInstance().getItems()) { // if we are still in the queue waiting to be scheduled, just retract that if (item.task instanceof PlaceholderTask && ((PlaceholderTask) item.task).context.equals(getContext())) { Queue.getInstance().cancel(item); break; } } Jenkins j = Jenkins.getInstance(); if (j != null) { // if we are already running, kill the ongoing activities, which releases PlaceholderExecutable from its sleep loop // Similar to Executor.of, but distinct since we do not have the Executable yet: COMPUTERS: for (Computer c : j.getComputers()) { for (Executor e : c.getExecutors()) { Queue.Executable exec = e.getCurrentExecutable(); if (exec instanceof PlaceholderTask.PlaceholderExecutable && ((PlaceholderTask.PlaceholderExecutable) exec).getParent().context.equals(getContext())) { PlaceholderTask.finish(((PlaceholderTask.PlaceholderExecutable) exec).getParent().cookie); break COMPUTERS; } } } } // Whether or not either of the above worked (and they would not if for example our item were canceled), make sure we die. super.stop(cause); }
@Override public void evaluate() throws Throwable { rebuildContext(story.j); assertThatWorkflowIsSuspended(); for (int i = 0; i < 600 && !Queue.getInstance().isEmpty(); i++) { Thread.sleep(100); } liveness(); SemaphoreStep.success("wait/1", null); story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)); } });
@Issue("JENKINS-50561") @Test public void rateLimitBuilds() throws Exception { WorkflowRun b = expect("rateLimitBuilds") .go(); WorkflowJob p = b.getParent(); RateLimitBranchProperty.JobPropertyImpl prop = p.getProperty(RateLimitBranchProperty.JobPropertyImpl.class); assertNotNull(prop); assertEquals(1, prop.getCount()); assertEquals("day", prop.getDurationName()); assertFalse(prop.isUserBoost()); QueueTaskFuture<WorkflowRun> inQueue = p.scheduleBuild2(0); while (!Queue.getInstance().contains(p)) { Thread.yield(); } Queue.getInstance().maintain(); Queue.Item queued = Queue.getInstance().getItem(p); assertThat(queued.isBlocked(), is(true)); assertThat(queued.getCauseOfBlockage().getShortDescription().toLowerCase(), containsString("throttle")); inQueue.cancel(true); }
@Override public String getStatus() { // Yet another copy of the same logic; perhaps this should be factored into some method returning a union of Queue.Item and PlaceholderExecutable? for (Queue.Item item : Queue.getInstance().getItems()) { if (item.task instanceof PlaceholderTask && ((PlaceholderTask) item.task).context.equals(getContext())) { return "waiting for " + item.task.getFullDisplayName() + " to be scheduled; blocked: " + item.getWhy(); } } Jenkins j = Jenkins.getInstance(); if (j != null) { COMPUTERS: for (Computer c : j.getComputers()) { for (Executor e : c.getExecutors()) { Queue.Executable exec = e.getCurrentExecutable(); if (exec instanceof PlaceholderTask.PlaceholderExecutable && ((PlaceholderTask.PlaceholderExecutable) exec).getParent().context.equals(getContext())) { return "running on " + c.getName(); } } } } return "node block appears to be neither running nor scheduled"; }
@Override public String getStatus() { // Yet another copy of the same logic; perhaps this should be factored into some method returning a union of Queue.Item and PlaceholderExecutable? for (Queue.Item item : Queue.getInstance().getItems()) { if (item.task instanceof PlaceholderTask && ((PlaceholderTask) item.task).context.equals(getContext())) { return "waiting for " + item.task.getFullDisplayName() + " to be scheduled; blocked: " + item.getWhy(); } } Jenkins j = Jenkins.getInstance(); if (j != null) { COMPUTERS: for (Computer c : j.getComputers()) { for (Executor e : c.getExecutors()) { Queue.Executable exec = e.getCurrentExecutable(); if (exec instanceof PlaceholderTask.PlaceholderExecutable && ((PlaceholderTask.PlaceholderExecutable) exec).getParent().context.equals(getContext())) { return "running on " + c.getName(); } } } } return "node block appears to be neither running nor scheduled"; }
@Override public String getStatus() { // Yet another copy of the same logic; perhaps this should be factored into some method returning a union of Queue.Item and PlaceholderExecutable? for (Queue.Item item : Queue.getInstance().getItems()) { if (item.task instanceof PlaceholderTask && ((PlaceholderTask) item.task).context.equals(getContext())) { return "waiting for " + item.task.getFullDisplayName() + " to be scheduled; blocked: " + item.getWhy(); } } Jenkins j = Jenkins.getInstance(); if (j != null) { COMPUTERS: for (Computer c : j.getComputers()) { for (Executor e : c.getExecutors()) { Queue.Executable exec = e.getCurrentExecutable(); if (exec instanceof PlaceholderTask.PlaceholderExecutable && ((PlaceholderTask.PlaceholderExecutable) exec).getParent().context.equals(getContext())) { return "running on " + c.getName(); } } } } return "node block appears to be neither running nor scheduled"; }