Skip to content

Commit 6ecacd8

Browse files
authored
Merge pull request #124 from jglick/retry-unconditionally-JENKINS-49707
[JENKINS-49707] Recommend `BodyExecution.cancel` overload taking `Throwable`
2 parents 6735578 + dd28011 commit 6ecacd8

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/main/java/org/jenkinsci/plugins/workflow/steps/BodyExecution.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package org.jenkinsci.plugins.workflow.steps;
2525

26+
import hudson.Util;
27+
import hudson.model.Result;
2628
import jenkins.model.CauseOfInterruption;
2729

2830
import java.io.Serializable;
@@ -49,31 +51,41 @@ public abstract class BodyExecution implements Future<Object>, Serializable {
4951
public abstract Collection<StepExecution> getCurrentExecutions();
5052

5153
/**
52-
* Attempts to cancel an executing body block.
53-
*
54-
* <p>
55-
* If the body has finished executing, or is cancelled already, the attempt will
56-
* fail. This method is asynchronous. There's no guarantee that the cancellation
57-
* has happened or completed before this method returns.
58-
*
59-
* @return false if the task cannot be cancelled.
54+
* @deprecated use {@link #cancel(Throwable)} with {@link FlowInterruptedException}
6055
*/
61-
public abstract boolean cancel(CauseOfInterruption... causes);
56+
@Deprecated
57+
public boolean cancel(CauseOfInterruption... causes) {
58+
if (Util.isOverridden(BodyExecution.class, getClass(), "cancel", Throwable.class)) {
59+
return cancel(new FlowInterruptedException(Result.ABORTED, true, causes));
60+
} else {
61+
throw new AbstractMethodError("Override cancel(Throwable) from " + getClass());
62+
}
63+
}
6264

6365
/**
64-
* @deprecated
65-
* Use other overloaded forms of the cancel method to provide richer context.
66+
* @deprecated use {@link #cancel(Throwable)} to provide richer context
6667
*/
68+
@Deprecated
6769
public boolean cancel(boolean b) {
68-
return cancel(new Exception());
70+
return cancel(new FlowInterruptedException(Result.ABORTED, true));
6971
}
7072

7173
/**
72-
* Convenience method around {@link #cancel(CauseOfInterruption...)} in case
73-
* the cause is a random exception.
74+
* Attempts to cancel an executing body block.
75+
*
76+
* <p>
77+
* If the body has finished executing, or is cancelled already, the attempt will
78+
* fail. This method is asynchronous. There's no guarantee that the cancellation
79+
* has happened or completed before this method returns.
80+
* @param t reason for cancellation; typically a {@link FlowInterruptedException}
81+
* @return false if the task cannot be cancelled.
7482
*/
7583
public boolean cancel(Throwable t) {
76-
return cancel(new ExceptionCause(t));
84+
if (Util.isOverridden(BodyExecution.class, getClass(), "cancel", CauseOfInterruption[].class)) {
85+
return cancel(new ExceptionCause(t));
86+
} else {
87+
throw new AbstractMethodError("Override cancel(Throwable) from " + getClass());
88+
}
7789
}
7890

7991
private static final long serialVersionUID = 1L;

src/main/java/org/jenkinsci/plugins/workflow/steps/FlowInterruptedException.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,11 @@ public final class FlowInterruptedException extends InterruptedException {
6767
private Boolean actualInterruption = true;
6868

6969
/**
70-
* Creates a new exception.
71-
* @param result the desired result for the flow, typically {@link Result#ABORTED}
72-
* @param causes any indications
70+
* @deprecated use {@link #FlowInterruptedException(Result, boolean, CauseOfInterruption...)}
7371
*/
72+
@Deprecated
7473
public FlowInterruptedException(@NonNull Result result, @NonNull CauseOfInterruption... causes) {
75-
this.result = result;
76-
this.causes = Arrays.asList(causes);
77-
this.actualInterruption = true;
74+
this(result, true, causes);
7875
}
7976

8077
/**

src/main/java/org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void stop(Throwable cause) throws Exception {
5151
Thread e = executing; // capture
5252
if (e!=null) {
5353
if (e instanceof Executor) {
54+
// TODO if cause instanceof FlowInterruptedException, unpack result & causes
5455
((Executor) e).interrupt(ABORTED, new ExceptionCause(cause));
5556
} else {
5657
e.interrupt();

0 commit comments

Comments
 (0)