static boolean blocksRestart(WorkflowRun b) throws Exception { if (b.getExecutor().getAsynchronousExecution().blocksRestart()) { return true; } // TODO delete when implemented in workflow-cps: return b.getExecution().getCurrentExecutions(false).get(1, TimeUnit.SECONDS).stream().anyMatch(StepExecution::blocksRestart); }
@Override public void evaluate() throws Throwable { Logger LOGGER = Logger.getLogger("org.jenkinsci.plugins.workflow"); LOGGER.setLevel(Level.FINE); Handler handler = new ConsoleHandler(); handler.setLevel(Level.ALL); LOGGER.addHandler(handler); WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class); WorkflowRun b = p.getLastBuild(); assertTrue(b.isBuilding()); story.j.waitForMessage("Cannot restore BadThing", b); b.getExecutor().interrupt(); story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b)); } });
public static void cancelBuild(Job job, Build build) { String buildUid = build.getMetadata().getUid(); Jenkins jenkins = Jenkins.getInstance(); if (jenkins != null) { Queue buildQueue = jenkins.getQueue(); for (Queue.Item item : buildQueue.getItems()) { for (Cause cause : item.getCauses()) { if (cause instanceof BuildCause && ((BuildCause) cause).getUid().equals(buildUid)) { buildQueue.cancel(item); cancelOpenShiftBuild(build); return; } } } for (Object obj : job.getNewBuilds()) { if (obj instanceof WorkflowRun) { WorkflowRun b = (WorkflowRun) obj; BuildCause cause = b.getCause(BuildCause.class); if (cause != null && cause.getUid().equals(buildUid)) { Executor e = b.getExecutor(); if (e != null) { e.interrupt(ABORTED); break; } } } } cancelOpenShiftBuild(build); } }
b.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b)); startExit.release(); startExit.release(); SemaphoreStep.waitForStart("wait/1", b); b.getExecutor().interrupt(); endEnter.acquire(); endExit.release(); SemaphoreStep.success("wait/2", null); endEnter.acquire(); b.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b)); endExit.release();
private void liveness() { assertFalse(jenkins().toComputer().isIdle()); Executor e = b.getOneOffExecutor(); assertNotNull(e); assertEquals(e, b.getExecutor()); assertTrue(e.isActive()); /* TODO seems flaky: assertFalse(e.isAlive()); */ }
@Test public void stop() throws Exception { WorkflowJob p = r.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("unkillable()", true)); final WorkflowRun b = p.scheduleBuild2(0).waitForStart(); r.waitForMessage("unkillable", b); try (ACLContext context = ACL.as(Jenkins.ANONYMOUS)) { b.getExecutor().interrupt(); }; r.waitForCompletion(b); r.assertBuildStatus(Result.ABORTED, b); InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class); assertNotNull(iba); List<CauseOfInterruption> causes = iba.getCauses(); assertEquals(1, causes.size()); assertEquals(CauseOfInterruption.UserInterruption.class, causes.get(0).getClass()); r.waitForMessage("Finished: ABORTED", b); // TODO JENKINS-46076 WorkflowRun.isBuilding() can go to false before .finish has completed r.assertLogContains("never going to stop", b); r.assertLogNotContains("\tat ", b); }
@Test public void interruptedTest() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("node {\n" + "echo 'First message'\n" + "try { syncnonblocking 'wait' } catch(InterruptedException e) { echo 'Interrupted!' }\n" + "echo 'Second message'\n" + "}", true)); WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get(); // Wait for syncnonblocking to be started System.out.println("Waiting to syncnonblocking to start..."); SynchronousNonBlockingStep.waitForStart("wait", b); // At this point syncnonblocking is waiting for an interruption // Let's force a call to stop. This will try to send an interruption to the run Thread b.getExecutor().interrupt(); System.out.println("Looking for interruption received log message"); j.waitForMessage("Interrupted!", b); j.waitForCompletion(b); j.assertBuildStatus(Result.ABORTED, b); // Also check that timeouts produce the right status. p.setDefinition(new CpsFlowDefinition("timeout(time: 1, unit: 'SECONDS') {syncnonblocking 'wait2'}", true)); j.assertLogContains(new TimeoutStepExecution.ExceededTimeout().getShortDescription(), j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0))); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("parallel a: {def r = semaphore 'a'; echo r}, b: {semaphore 'b'}", true)); WorkflowRun b1 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("a/1", b1); SemaphoreStep.waitForStart("b/1", b1); b1.getExecutor().interrupt(); story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b1)); story.j.assertLogContains("Failed in branch a", b1); story.j.assertLogContains("Failed in branch b", b1); WorkflowRun b2 = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("a/2", b2); SemaphoreStep.waitForStart("b/2", b2); SemaphoreStep.success("a/2", "finished branch a"); story.j.waitForMessage("finished branch a", b2); b2.getExecutor().interrupt(); story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b2)); story.j.assertLogNotContains("Failed in branch a", b2); story.j.assertLogContains("Failed in branch b", b2); } });