Search in sources :

Example 1 with BodyExecutionCallback

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

the class CpsBodyExecution method launch.

/**
 * Starts evaluating the body.
 *
 * If the body is a synchronous closure, this method evaluates the closure synchronously.
 * Otherwise, the body is asynchronous and the method schedules another thread to evaluate the body.
 *
 * @param currentThread
 *      The thread whose context the new thread will inherit.
 */
@CpsVmThreadOnly
/*package*/
void launch(CpsBodyInvoker params, CpsThread currentThread, FlowHead head) {
    if (isLaunched())
        throw new IllegalStateException("Already launched");
    StepStartNode sn = addBodyStartFlowNode(head);
    for (Action a : params.startNodeActions) {
        if (a != null)
            sn.addAction(a);
    }
    head.setNewHead(sn);
    CpsFlowExecution.maybeAutoPersistNode(sn);
    StepContext sc = new CpsBodySubContext(context, sn);
    for (BodyExecutionCallback c : callbacks) {
        c.onStart(sc);
    }
    try {
        // TODO: handle arguments to closure
        Object x = params.body.getBody(currentThread).call();
        // pointless synchronization to make findbugs happy. This is already done, so there's no cancelling this anyway.
        synchronized (this) {
            this.thread = currentThread;
        }
        onSuccess.receive(x);
    } catch (CpsCallableInvocation e) {
        // execute this closure asynchronously
        // TODO: does it make sense that the new thread shares the same head?
        CpsThread t = currentThread.group.addThread(createContinuable(currentThread, e), head, ContextVariableSet.from(currentThread.getContextVariables(), params.contextOverrides));
        // due to earlier cancellation
        synchronized (this) {
            t.resume(new Outcome(null, stopped));
            assert this.thread == null;
            this.thread = t;
        }
    } catch (Throwable t) {
        // body has completed synchronously and abnormally
        synchronized (this) {
            this.thread = currentThread;
        }
        onFailure.receive(t);
    }
}
Also used : ErrorAction(org.jenkinsci.plugins.workflow.actions.ErrorAction) Action(hudson.model.Action) BodyInvocationAction(org.jenkinsci.plugins.workflow.actions.BodyInvocationAction) StepStartNode(org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode) StepContext(org.jenkinsci.plugins.workflow.steps.StepContext) CpsCallableInvocation(com.cloudbees.groovy.cps.impl.CpsCallableInvocation) Outcome(com.cloudbees.groovy.cps.Outcome) BodyExecutionCallback(org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback)

Aggregations

Outcome (com.cloudbees.groovy.cps.Outcome)1 CpsCallableInvocation (com.cloudbees.groovy.cps.impl.CpsCallableInvocation)1 Action (hudson.model.Action)1 BodyInvocationAction (org.jenkinsci.plugins.workflow.actions.BodyInvocationAction)1 ErrorAction (org.jenkinsci.plugins.workflow.actions.ErrorAction)1 StepStartNode (org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode)1 BodyExecutionCallback (org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback)1 StepContext (org.jenkinsci.plugins.workflow.steps.StepContext)1