public void doEndOfflineAgentJobs(final StaplerRequest request, final StaplerResponse response) { Jenkins j; if ((j = Jenkins.getInstance()) != null) { Queue queue = j.getQueue(); if (queue != null) { for (Item job : queue.getItems()) { if (job.getCauseOfBlockage() instanceof BecauseNodeIsOffline || job.getCauseOfBlockage() instanceof BecauseLabelIsOffline) { queue.cancel(job); } } } } try { response.sendRedirect2(request.getRootPath()); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Helper method to determine the template from a given item. * * @param item Item which includes a template. * @return If the item includes a template then the template will be returned. Otherwise <code>null</code>. */ private DockerJobTemplateProperty getJobTemplate(Item item) { if (item.task instanceof Project) { Project<?, ?> project = (Project<?, ?>) item.task; if (project != null) { final DockerJobTemplateProperty p = project.getProperty(DockerJobTemplateProperty.class); if (p != null) return p; // backward compatibility. DockerJobTemplateProperty used to be a nested object in DockerJobProperty DockerJobProperty property = project.getProperty(DockerJobProperty.class); if (property != null) { return property.getDockerJobTemplate(); } } } return null; }
public void cancelSubBuilds(final PrintStream logger) { final Queue q = getJenkins().getQueue(); synchronized (q) { final int n = this.dynamicBuild.getNumber(); for (final Item i : q.getItems()) { final ParentBuildAction parentBuildAction = i.getAction(ParentBuildAction.class); if (parentBuildAction != null && this.dynamicBuild.equals(parentBuildAction.getParent())) { q.cancel(i); } } for (final DynamicSubProject c : this.dynamicBuild.getAllSubProjects()) { final DynamicSubBuild b = c.getBuildByNumber(n); if (b != null && b.isBuilding()) { final Executor exe = b.getExecutor(); if (exe != null) { logger.println(Messages.MatrixBuild_Interrupting(ModelHyperlinkNote.encodeTo(b))); exe.interrupt(); } } } } }
@Test public void testDeclineOffersWithBuildsInQueue() throws Exception { Protos.Offer offer = createOfferWithVariableRanges(31000, 32000); ArrayList<Protos.Offer> offers = new ArrayList<Protos.Offer>(); offers.add(offer); Queue queue = Mockito.mock(Queue.class); Mockito.when(jenkins.getQueue()).thenReturn(queue); Item item = Mockito.mock(Item.class); Item [] items = {item}; Mockito.when(queue.getItems()).thenReturn(items); Mockito.when(mesosCloud.canProvision(null)).thenReturn(true); SchedulerDriver driver = Mockito.mock(SchedulerDriver.class); Mockito.when(mesosCloud.getDeclineOfferDurationDouble()).thenReturn((double) 120000); jenkinsScheduler.resourceOffers(driver, offers); Mockito.verify(driver).declineOffer(offer.getId()); Mockito.verify(driver, Mockito.never()).declineOffer(offer.getId(), Protos.Filters.newBuilder().setRefuseSeconds(120000).build()); }
public CurrentBuildState waitForCompletion(final DynamicSubProject c, final TaskListener listener) throws InterruptedException { // wait for the completion int appearsCancelledCount = 0; while (true) { Thread.sleep(1000); final CurrentBuildState b = c.getCurrentStateByNumber(this.dynamicBuild.getNumber()); if (b != null) { // its building or is done if (b.isBuilding()) { continue; } else { final Result buildResult = b.getResult(); if (buildResult != null) { return b; } } } else { // not building or done, check queue final Queue.Item qi = c.getQueueItem(); if (qi == null) { appearsCancelledCount++; listener.getLogger().println(c.getName() + " appears cancelled: " + appearsCancelledCount); } else { appearsCancelledCount = 0; } if (appearsCancelledCount >= 5) { listener.getLogger().println(Messages.MatrixBuild_AppearsCancelled(ModelHyperlinkNote.encodeTo(c))); return new CurrentBuildState("COMPLETED", Result.ABORTED); } } } }
@Override public synchronized Item getQueueItem() { final Queue queue = Jenkins.getInstance().getQueue(); final Item[] items = queue.getItems(); for (int i = 0; i < items.length; i++) { if (items[i].task != null && items[i].task.equals(this)) { return items[i]; } } return super.getQueueItem(); }
@Override public CauseOfBlockage canRun(Queue.Item item) { initPython(); if (pexec.isImplemented(2)) { return (CauseOfBlockage) pexec.execPython("can_run", item); } else { return super.canRun(item); } }
@Test public void testAcceptOfferAndLaunchTasksWithPastUnavailability() throws Exception { Date now = new Date(); Date startDate = new Date(now.getTime() - TimeUnit.HOURS.toMillis(1)); Date endDate = new Date(startDate.getTime() + TimeUnit.MINUTES.toMillis(30)); Protos.Offer matchingOffer = createOfferWithUnavailability(startDate, endDate); ArrayList<Protos.Offer> offers = new ArrayList<Protos.Offer>(); offers.add(matchingOffer); Mesos.SlaveRequest request = mockSlaveRequest(false, false, null); jenkinsScheduler.requestJenkinsSlave(request, null); Queue queue = Mockito.mock(Queue.class); Mockito.when(jenkins.getQueue()).thenReturn(queue); MesosComputer computer = Mockito.mock(MesosComputer.class); Mockito.when(jenkins.getComputers()).thenReturn(new Computer[] { computer }); Item item = Mockito.mock(Item.class); Item [] items = {item}; Mockito.when(queue.getItems()).thenReturn(items); Mockito.when(mesosCloud.canProvision(null)).thenReturn(true); SchedulerDriver driver = Mockito.mock(SchedulerDriver.class); jenkinsScheduler.setDriver(driver); jenkinsScheduler.resourceOffers(driver, offers); Mockito.verify(driver, never()).declineOffer(matchingOffer.getId()); Mockito.verify(driver).launchTasks(eq(matchingOffer.getId()), anyListOf(Protos.TaskInfo.class), eq(Protos.Filters.newBuilder().setRefuseSeconds(1).build())); assertEquals(0, jenkinsScheduler.getUnmatchedLabels().size()); }
@Test public void testAcceptOfferAndLaunchTasksWithFutureUnavailability() throws Exception { // set it up Date now = new Date(); Date startDate = new Date(now.getTime() + TimeUnit.HOURS.toMillis(1)); Date endDate = new Date(startDate.getTime() + TimeUnit.MINUTES.toMillis(30)); Protos.Offer matchingOffer = createOfferWithUnavailability(startDate, endDate); ArrayList<Protos.Offer> offers = new ArrayList<Protos.Offer>(); offers.add(matchingOffer); Mesos.SlaveRequest request = mockSlaveRequest(false, false, null); jenkinsScheduler.requestJenkinsSlave(request, null); Queue queue = Mockito.mock(Queue.class); Mockito.when(jenkins.getQueue()).thenReturn(queue); MesosComputer computer = Mockito.mock(MesosComputer.class); Mockito.when(jenkins.getComputers()).thenReturn(new Computer[] { computer }); Item item = Mockito.mock(Item.class); Item [] items = { item }; Mockito.when(queue.getItems()).thenReturn(items); Mockito.when(mesosCloud.canProvision(null)).thenReturn(true); SchedulerDriver driver = Mockito.mock(SchedulerDriver.class); jenkinsScheduler.setDriver(driver); // test it jenkinsScheduler.resourceOffers(driver, offers); // verify it Mockito.verify(driver, never()).declineOffer(matchingOffer.getId()); Mockito.verify(driver).launchTasks(eq(matchingOffer.getId()), anyListOf(Protos.TaskInfo.class), eq(Protos.Filters.newBuilder().setRefuseSeconds(1).build())); assertEquals(0, jenkinsScheduler.getUnmatchedLabels().size()); }
@Test public void testDeclineOfferWithCurrentUnavailability() throws Exception { // set it up Date now = new Date(); Date startDate = new Date(now.getTime() - TimeUnit.HOURS.toMillis(1)); Date endDate = new Date(startDate.getTime() + TimeUnit.HOURS.toMillis(2)); Protos.Offer matchingOffer = createOfferWithUnavailability(startDate, endDate); ArrayList<Protos.Offer> offers = new ArrayList<Protos.Offer>(); offers.add(matchingOffer); Mesos.SlaveRequest request = mockSlaveRequest(false, false, null); jenkinsScheduler.requestJenkinsSlave(request, null); Queue queue = Mockito.mock(Queue.class); Mockito.when(jenkins.getQueue()).thenReturn(queue); Item item = Mockito.mock(Item.class); Item [] items = { item }; Mockito.when(queue.getItems()).thenReturn(items); Mockito.when(mesosCloud.canProvision(null)).thenReturn(true); SchedulerDriver driver = Mockito.mock(SchedulerDriver.class); jenkinsScheduler.setDriver(driver); // test it jenkinsScheduler.resourceOffers(driver, offers); // verify it // make sure it does not call "matches()" and gets declined because of a non-matching offer Mockito.verify(mesosCloud, never()).getRole(); Mockito.verify(driver).declineOffer(matchingOffer.getId()); assertEquals(1, jenkinsScheduler.getUnmatchedLabels().size()); }
@Override public void onLoad(final ItemGroup<? extends hudson.model.Item> parent, final String name) throws IOException { initRepos(); super.onLoad(parent, name); }
public CauseOfBlockage superCanRun(Queue.Item item) { return super.canRun(item); }