use of io.jenkins.blueocean.rest.model.BlueQueueItem in project blueocean-plugin by jenkinsci.
the class QueueContainerImpl method getQueuedItems.
/**
* This function gets gets a list of all queued items if the job is a buildable item.
*
* Note the estimated build number calculation is a guess - job types need not return
* sequential build numbers.
*
* @return List of items newest first
*/
public static List<BlueQueueItem> getQueuedItems(Job job) {
Link pipelineLink = LinkResolver.resolveLink(job);
if (job instanceof BuildableItem) {
BuildableItem task = (BuildableItem) job;
List<Queue.Item> items = Jenkins.getInstance().getQueue().getItems(task);
List<BlueQueueItem> items2 = Lists.newArrayList();
for (int i = 0; i < items.size(); i++) {
Link self = pipelineLink.rel("queue").rel(Long.toString(items.get(i).getId()));
items2.add(0, new QueueItemImpl(items.get(i), job.getName(), (items.size() == 1 ? job.getNextBuildNumber() : job.getNextBuildNumber() + i), self, pipelineLink));
}
return items2;
} else {
throw new ServiceException.UnexpectedErrorException("This pipeline is not buildable and therefore does not have a queue.");
}
}
use of io.jenkins.blueocean.rest.model.BlueQueueItem in project blueocean-plugin by jenkinsci.
the class BlueMessageEnricher method maybeEnrichMessage.
private void maybeEnrichMessage(@Nonnull Message message) {
String channelName = message.getChannelName();
if (channelName.equals(Events.JobChannel.NAME) && message instanceof JobChannelMessage) {
JobChannelMessage jobChannelMessage = (JobChannelMessage) message;
Item jobChannelItem = jobChannelMessage.getJobChannelItem();
if (jobChannelItem == null) {
return;
}
Link jobUrl = LinkResolver.resolveLink(jobChannelItem);
if (jobUrl == null) {
return;
}
BlueOrganization org = OrganizationFactory.getInstance().getContainingOrg(jobChannelItem);
if (org != null) {
message.set(EventProps.Jenkins.jenkins_org, org.getName());
}
jobChannelMessage.set(BlueEventProps.blueocean_job_rest_url, jobUrl.getHref());
jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, AbstractPipelineImpl.getFullName(org, jobChannelItem));
if (jobChannelItem instanceof WorkflowJob) {
ItemGroup<? extends Item> parent = jobChannelItem.getParent();
if (parent instanceof WorkflowMultiBranchProject) {
String multiBranchProjectName = AbstractPipelineImpl.getFullName(org, (WorkflowMultiBranchProject) parent);
jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, multiBranchProjectName);
jobChannelMessage.set(BlueEventProps.blueocean_job_branch_name, jobChannelItem.getName());
}
}
if (message.containsKey("job_run_queueId") && jobChannelItem instanceof hudson.model.Job) {
String queueIdStr = message.get("job_run_queueId");
if (queueIdStr == null) {
return;
}
final long queueId = Long.parseLong(queueIdStr);
Queue.Item queueItem = Jenkins.get().getQueue().getItem(queueId);
if (queueItem == null) {
return;
}
hudson.model.Job job = (hudson.model.Job) jobChannelItem;
BlueQueueItem blueQueueItem = QueueUtil.getQueuedItem(null, queueItem, job);
if (blueQueueItem != null) {
jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, Integer.toString(blueQueueItem.getExpectedBuildNumber()));
} else {
// If build is already running, we simply take the run id and pass it on
if (message.get("job_run_status") != null) {
String buildNumberStr = message.get("jenkins_object_id");
if (StringUtils.isNotBlank(buildNumberStr)) {
jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, message.get("jenkins_object_id"));
}
}
}
}
}
}
use of io.jenkins.blueocean.rest.model.BlueQueueItem in project blueocean-plugin by jenkinsci.
the class MultiBranchTest method testMultiBranchPipelineQueueContainer.
@Test
public void testMultiBranchPipelineQueueContainer() throws Exception {
j.jenkins.setQuietPeriod(0);
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
sampleRepo1.init();
sampleRepo1.write("Jenkinsfile", "stage 'build'\n " + "node {echo 'Building'}\n" + "stage 'test'\nnode { echo 'Testing'}\n" + "sleep 10000 \n" + "stage 'deploy'\nnode { echo 'Deploying'}\n");
sampleRepo1.write("file", "initial content");
sampleRepo1.git("add", "Jenkinsfile");
sampleRepo1.git("commit", "--all", "--message=flow");
// create feature branch
sampleRepo1.git("checkout", "-b", "abc");
sampleRepo1.write("Jenkinsfile", "echo \"branch=${env.BRANCH_NAME}\"; " + "node {" + " stage ('Build'); " + " echo ('Building'); " + " stage ('Test'); sleep 10000; " + " echo ('Testing'); " + " stage ('Deploy'); " + " echo ('Deploying'); " + "}");
ScriptApproval.get().approveSignature("method java.lang.String toUpperCase");
sampleRepo1.write("file", "subsequent content1");
sampleRepo1.git("commit", "--all", "--message=tweaked1");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo1.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
for (SCMSource source : mp.getSCMSources()) {
assertEquals(mp, source.getOwner());
}
scheduleAndFindBranchProject(mp);
Resource r = BluePipelineFactory.resolve(mp);
assertTrue(r instanceof MultiBranchPipelineImpl);
for (WorkflowJob job : mp.getItems()) {
Queue.Item item = job.getQueueItem();
job.setConcurrentBuild(false);
job.scheduleBuild2(0);
job.scheduleBuild2(0);
}
Queue.Item[] queueItems = Jenkins.get().getQueue().getItems();
MultiBranchPipelineQueueContainer mbpQueueContainer = new MultiBranchPipelineQueueContainer((MultiBranchPipelineImpl) r);
Iterator<BlueQueueItem> blueQueueItems = mbpQueueContainer.iterator(0, 100);
if (queueItems.length > 0) {
assertTrue(mbpQueueContainer.iterator().hasNext());
assertEquals("/blue/rest/organizations/jenkins/pipelines/p/queue/", mbpQueueContainer.getLink().getHref());
BlueQueueItem blueQueueItem = mbpQueueContainer.get(String.valueOf(queueItems[0].getId()));
assertNotNull(blueQueueItem);
assertTrue(blueQueueItems.hasNext());
}
}
use of io.jenkins.blueocean.rest.model.BlueQueueItem in project blueocean-plugin by jenkinsci.
the class MultiBranchPipelineQueueContainer method iterator.
@Override
public Iterator<BlueQueueItem> iterator(int start, int limit) {
List<BluePipeline> branches = StreamSupport.stream(multiBranchPipeline.getBranches().spliterator(), false).sorted((o1, o2) -> PipelineRunImpl.LATEST_RUN_START_TIME_COMPARATOR.compare(o1.getLatestRun(), o2.getLatestRun())).collect(Collectors.toList());
int l = branches.size() > 0 && limit / branches.size() > 0 ? limit / branches.size() : 1;
int s = 0;
if (start > 0) {
s = Math.max(start - l, 0);
}
List<BlueQueueItem> c = new ArrayList<>();
int count = 0;
int retry = 0;
while (retry < 5 && count < limit) {
for (BluePipeline b : branches) {
Iterator<BlueQueueItem> it = b.getQueue().iterator(s, l);
while (it.hasNext()) {
count++;
c.add(it.next());
}
}
retry++;
}
c.sort((o1, o2) -> o2.getQueuedTime().compareTo(o1.getQueuedTime()));
return c.iterator();
}
use of io.jenkins.blueocean.rest.model.BlueQueueItem in project blueocean-plugin by jenkinsci.
the class PipelineNodeImpl method restart.
public HttpResponse restart(StaplerRequest request) {
try {
JSONObject body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
boolean restart = body.getBoolean("restart");
if (restart && isRestartable()) {
LOGGER.debug("submitInputStep, restart: {}, step: {}", restart, this.getDisplayName());
RestartDeclarativePipelineAction restartDeclarativePipelineAction = this.run.getAction(RestartDeclarativePipelineAction.class);
Queue.Item item = restartDeclarativePipelineAction.run(this.getDisplayName());
BluePipeline bluePipeline = BluePipelineFactory.getPipelineInstance(this.run.getParent(), this.parent);
BlueQueueItem queueItem = QueueUtil.getQueuedItem(bluePipeline.getOrganization(), item, run.getParent());
if (queueItem != null) {
// If the item is still queued
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(queueItem.toRun()));
};
}
final WorkflowRun restartRun = getRun(run.getParent(), item.getId());
if (restartRun != null) {
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(new PipelineRunImpl(restartRun, parent, bluePipeline.getOrganization())));
};
} else {
// For some reason could not be added to the queue
throw new ServiceException.UnexpectedErrorException("Run was not added to queue.");
}
}
// ISE cant happen if stage not restartable or anything else :)
} catch (Exception e) {
LOGGER.warn("error restarting stage: " + e.getMessage(), e);
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
return null;
}
Aggregations