Search in sources :

Example 6 with FlowInterruptedException

use of org.jenkinsci.plugins.workflow.steps.FlowInterruptedException in project workflow-cps-plugin by jenkinsci.

the class CpsBodyExecution method cancel.

@Override
public boolean cancel(final CauseOfInterruption... causes) {
    // 'stopped' and 'thread' are updated atomically
    CpsThread t;
    synchronized (this) {
        // already complete
        if (isDone())
            return false;
        // TODO should perhaps rather override cancel(Throwable) and make this overload just delegate to that one
        stopped = new FlowInterruptedException(Result.ABORTED, causes);
        t = this.thread;
    }
    if (t != null) {
        t.getExecution().runInCpsVmThread(new FutureCallback<CpsThreadGroup>() {

            @Override
            public void onSuccess(CpsThreadGroup g) {
                // Similar to getCurrentExecutions but we want the raw CpsThread, not a StepExecution; cf. CpsFlowExecution.interrupt
                Map<FlowHead, CpsThread> m = new LinkedHashMap<>();
                for (CpsThread t : thread.group.threads.values()) {
                    m.put(t.head, t);
                }
                for (CpsThread t : Iterators.reverse(ImmutableList.copyOf(m.values()))) {
                    LinearBlockHoppingScanner scanner = new LinearBlockHoppingScanner();
                    scanner.setup(t.head.get());
                    for (FlowNode node : scanner) {
                        if (node.getId().equals(startNodeId)) {
                            t.stop(stopped);
                            break;
                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                LOGGER.log(Level.WARNING, "could not cancel " + context + " with " + Arrays.toString(causes), t);
            }
        });
    } else {
    // if it hasn't begun executing, we'll stop it when
    // it begins.
    }
    return true;
}
Also used : LinearBlockHoppingScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 7 with FlowInterruptedException

use of org.jenkinsci.plugins.workflow.steps.FlowInterruptedException in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method doKill.

/**
 * Immediately kills the build.
 */
@RequirePOST
public void doKill() {
    checkPermission(Item.CANCEL);
    if (!isBuilding() || /* probably redundant, but just to be sure */
    execution == null) {
        return;
    }
    if (listener != null) {
        listener.getLogger().println("Hard kill!");
    }
    // ensures isInProgress returns false
    execution = null;
    FlowInterruptedException suddenDeath = new FlowInterruptedException(Result.ABORTED);
    finish(Result.ABORTED, suddenDeath);
    executionPromise.setException(suddenDeath);
// TODO CpsFlowExecution.onProgramEnd does some cleanup which we cannot access here; perhaps need a FlowExecution.halt(Throwable) API?
}
Also used : FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Aggregations

FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)7 IOException (java.io.IOException)4 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)3 AbortException (hudson.AbortException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Outcome (com.cloudbees.groovy.cps.Outcome)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 Result (hudson.model.Result)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeoutException (java.util.concurrent.TimeoutException)1 CauseOfInterruption (jenkins.model.CauseOfInterruption)1 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)1 InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)1