Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
[JENKINS-25504]
Browse files Browse the repository at this point in the history
If the step is marked as failed while the body is still running, try to interrupt the body execution.
This would speed up the step as a whole completing as a failure.
  • Loading branch information
kohsuke committed Nov 19, 2014
1 parent 1e843f1 commit 17206dc
Showing 1 changed file with 40 additions and 0 deletions.
Expand Up @@ -40,9 +40,11 @@
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
import org.jenkinsci.plugins.workflow.graph.BlockStartNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.support.DefaultStepContext;
import org.jenkinsci.plugins.workflow.support.concurrent.Futures;

Expand All @@ -60,6 +62,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.logging.Level.WARNING;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;

/**
Expand Down Expand Up @@ -313,6 +316,43 @@ public synchronized void onFailure(Throwable t) {
this.outcome = new Outcome(null,t);

scheduleNextRun();

if (!isCompleted()) {
// if the body is still running, make some effort to try to speed up the termination of the body.
tryToCancelBody();
}
}

/**
* Try to stop interrupt the body execution on the best effort basis.
*/
private void tryToCancelBody() {
try {
getFlowExecution().runInCpsVmThread(new FutureCallback<CpsThreadGroup>() {
@CpsVmThreadOnly
@Override
public void onSuccess(CpsThreadGroup g) {
CpsThread thread = getThread(g);
try {
StepExecution s = thread.getStep();
if (s!=null)
// TODO: better cause
s.stop(new FlowInterruptedException(Result.FAILURE));
} catch (Exception e) {
LOGGER.log(WARNING, "Failed to stop the body execution in response to the failure of the parent");
}
}

/**
* Program state failed to load.
*/
@Override
public void onFailure(Throwable t) {
}
});
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to stop the body execution in response to the failure of the parent", e);
}
}

public synchronized void onSuccess(Object returnValue) {
Expand Down

0 comments on commit 17206dc

Please sign in to comment.