/** * report cause of interruption and record it to the build, if available. * * @since 1.425 */ public void recordCauseOfInterruption(Run<?,?> build, TaskListener listener) { List<CauseOfInterruption> r; // atomically get&clear causes. lock.writeLock().lock(); try { if (causes.isEmpty()) return; r = new ArrayList<CauseOfInterruption>(causes); causes.clear(); } finally { lock.writeLock().unlock(); } build.addAction(new InterruptedBuildAction(r)); for (CauseOfInterruption c : r) c.print(listener); }
/** * If a build catches this exception, it should use this method to report it. * @param run * @param listener */ public void handle(Run<?,?> run, TaskListener listener) { Set<CauseOfInterruption> boundCauses = new HashSet<>(); for (InterruptedBuildAction a : run.getActions(InterruptedBuildAction.class)) { boundCauses.addAll(a.getCauses()); } Collection<CauseOfInterruption> diff = Sets.difference(new LinkedHashSet<>(causes), boundCauses); if (!diff.isEmpty()) { run.addAction(new InterruptedBuildAction(diff)); for (CauseOfInterruption cause : diff) { cause.print(listener); } } print(getCause(), listener); for (Throwable t : getSuppressed()) { print(t, listener); } } private static void print(@CheckForNull Throwable t, @Nonnull TaskListener listener) {
@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); }
/** * If a build catches this exception, it should use this method to report it. * @param run * @param listener */ public void handle(Run<?,?> run, TaskListener listener) { Set<CauseOfInterruption> boundCauses = new HashSet<>(); for (InterruptedBuildAction a : run.getActions(InterruptedBuildAction.class)) { boundCauses.addAll(a.getCauses()); } Collection<CauseOfInterruption> diff = Sets.difference(new LinkedHashSet<>(causes), boundCauses); if (!diff.isEmpty()) { run.addAction(new InterruptedBuildAction(diff)); for (CauseOfInterruption cause : diff) { cause.print(listener); } } print(getCause(), listener); for (Throwable t : getSuppressed()) { print(t, listener); } } private static void print(@CheckForNull Throwable t, @Nonnull TaskListener listener) {
/** * report cause of interruption and record it to the build, if available. * * @since 1.425 */ public void recordCauseOfInterruption(Run<?,?> build, TaskListener listener) { List<CauseOfInterruption> r; // atomically get&clear causes. lock.writeLock().lock(); try { if (causes.isEmpty()) return; r = new ArrayList<CauseOfInterruption>(causes); causes.clear(); } finally { lock.writeLock().unlock(); } build.addAction(new InterruptedBuildAction(r)); for (CauseOfInterruption c : r) c.print(listener); }