Java Code Examples for hudson.model.Queue#getItems()

The following examples show how to use hudson.model.Queue#getItems() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Reaper.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(@NonNull Action action, @NonNull KubernetesSlave node, @NonNull Pod pod) throws IOException, InterruptedException {
    List<ContainerStatus> backOffContainers = PodUtils.getContainers(pod, cs -> {
        ContainerStateWaiting waiting = cs.getState().getWaiting();
        return waiting != null && waiting.getMessage() != null && waiting.getMessage().contains("Back-off pulling image");
    });
    if (backOffContainers.isEmpty()) {
        return;
    }
    backOffContainers.forEach(cs -> {
        TaskListener runListener = node.getTemplate().getListener();
        runListener.error("Unable to pull Docker image \""+cs.getImage()+"\". Check if image tag name is spelled correctly.");
    });
    Queue q = Jenkins.get().getQueue();
    String runUrl = pod.getMetadata().getAnnotations().get("runUrl");
    for (Queue.Item item: q.getItems()) {
        if (item.task.getUrl().equals(runUrl)) {
            q.cancel(item);
            break;
        }
    }
    node.terminate();
}
 
Example 2
Source File: JobRunnerForBranchCause.java    From github-integration-plugin with MIT License 6 votes vote down vote up
/**
 * Cancel previous builds for specified PR id.
 */
private static boolean cancelQueuedBuildByBranchName(final String branch) {
    Queue queue = getJenkinsInstance().getQueue();

    for (Queue.Item item : queue.getItems()) {
        Optional<Cause> cause = from(item.getAllActions())
                .filter(instanceOf(CauseAction.class))
                .transformAndConcat(new CausesFromAction())
                .filter(instanceOf(GitHubBranchCause.class))
                .firstMatch(new CauseHasBranch(branch));

        if (cause.isPresent()) {
            queue.cancel(item);
            return true;
        }
    }

    return false;
}
 
Example 3
Source File: PendingBuildsHandler.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
public void cancelPendingBuilds(Job<?, ?> job, Integer projectId, String branch) {
    Queue queue = Jenkins.getInstance().getQueue();
    for (Queue.Item item : queue.getItems()) {
        if (!job.getName().equals(item.task.getName())) {
            continue;
        }
        GitLabWebHookCause queueItemGitLabWebHookCause = getGitLabWebHookCauseData(item);
        if (queueItemGitLabWebHookCause == null) {
            continue;
        }
        CauseData queueItemCauseData = queueItemGitLabWebHookCause.getData();
        if (!projectId.equals(queueItemCauseData.getSourceProjectId())) {
            continue;
        }
        if (branch.equals(queueItemCauseData.getBranch())) {
            cancel(item, queue, branch);
            setCommitStatusCancelledIfNecessary(queueItemCauseData, job);
        }
    }
}
 
Example 4
Source File: SubBuildScheduler.java    From DotCi with MIT License 6 votes vote down vote up
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();
                }
            }
        }
    }
}
 
Example 5
Source File: PendingBuildsHandlerTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@After
public void clearQueue() {
    Queue queue = jenkins.getInstance().getQueue();
    for (Queue.Item item : queue.getItems()) {
        queue.cancel(item);
    }
}
 
Example 6
Source File: DbBackedProject.java    From DotCi with MIT License 5 votes vote down vote up
@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();
}
 
Example 7
Source File: JobRunnerForCause.java    From github-integration-plugin with MIT License 4 votes vote down vote up
/**
 * Cancel previous builds for specified PR id.
 */
public int cancelQueuedBuildByPrNumber(final int id) {
    int canceled = 0;
    SecurityContext old = impersonate(ACL.SYSTEM);
    try {
        final Queue queue = getJenkinsInstance().getQueue();
        final Queue.Item[] items = queue.getItems();

        //todo replace with stream?
        for (Queue.Item item : items) {
            if (!(item.task instanceof Job)) {
                LOGGER.debug("Item {} not instanceof job", item);
                continue;
            }

            final Job<?, ?> jobTask = (Job<?, ?>) item.task;
            if (!jobTask.getFullName().equals(job.getFullName())) {
                LOGGER.debug("{} != {}", jobTask.getFullName(), job.getFullName());
                continue;
            }

            final CauseAction action = item.getAction(CauseAction.class);
            if (isNull(action)) {
                LOGGER.debug("Cause action is null for {}", jobTask.getFullName());
                continue;
            }

            Optional<Cause> cause = from(action.getCauses())
                    .filter(instanceOf(GitHubPRCause.class))
                    .firstMatch(new CauseHasPRNum(id));

            if (cause.isPresent()) {
                LOGGER.debug("Cancelling {}", item);
                queue.cancel(item);
                canceled++;
            }
        }
    } finally {
        SecurityContextHolder.setContext(old);
    }

    return canceled;
}