Search in sources :

Example 1 with SandboxInvoker

use of com.cloudbees.groovy.cps.sandbox.SandboxInvoker in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method start.

@Override
public void start() throws IOException {
    final CpsScript s = parseScript();
    scriptClass = s.getClass();
    s.$initialize();
    final FlowHead h = new FlowHead(this);
    synchronized (this) {
        heads.put(h.getId(), h);
    }
    h.newStartNode(new FlowStartNode(this, iotaStr()));
    final CpsThreadGroup g = new CpsThreadGroup(this);
    g.register(s);
    final SettableFuture<CpsThreadGroup> f = SettableFuture.create();
    programPromise = f;
    // Ensures we've saves the WorkFlowRun at least once with initial state
    saveOwner();
    g.runner.submit(new Runnable() {

        @Override
        public void run() {
            CpsThread t = g.addThread(new Continuable(s, createInitialEnv()), h, null);
            t.resume(new Outcome(null, null));
            f.set(g);
        }

        /**
         * Environment to start executing the script in.
         * During sandbox execution, we need to call sandbox interceptor while executing asynchronous code.
         */
        private Env createInitialEnv() {
            return Envs.empty(isSandbox() ? new SandboxInvoker() : new DefaultInvoker());
        }
    });
}
Also used : FlowStartNode(org.jenkinsci.plugins.workflow.graph.FlowStartNode) DefaultInvoker(com.cloudbees.groovy.cps.sandbox.DefaultInvoker) Env(com.cloudbees.groovy.cps.Env) SandboxInvoker(com.cloudbees.groovy.cps.sandbox.SandboxInvoker) Outcome(com.cloudbees.groovy.cps.Outcome) Continuable(com.cloudbees.groovy.cps.Continuable)

Example 2 with SandboxInvoker

use of com.cloudbees.groovy.cps.sandbox.SandboxInvoker in project workflow-cps-plugin by jenkinsci.

the class CpsBodyExecution method createContinuable.

/**
 * Creates {@link Continuable} that executes the given invocation and pass its result to {@link FutureCallback}.
 *
 * The {@link Continuable} itself will just yield null. {@link CpsThreadGroup} considers the whole
 * execution a failure if any of the threads fail, so this behaviour ensures that a problem in the closure
 * body won't terminate the workflow.
 */
private Continuable createContinuable(CpsThread currentThread, CpsCallableInvocation inv) {
    // we need FunctionCallEnv that acts as the back drop of try/catch block.
    // TODO: we need to capture the surrounding calling context to capture variables, and switch to ClosureCallEnv
    FunctionCallEnv caller = new FunctionCallEnv(null, onSuccess, null, null);
    if (currentThread.getExecution().isSandbox())
        caller.setInvoker(new SandboxInvoker());
    // catch an exception thrown from body and treat that as a failure
    TryBlockEnv env = new TryBlockEnv(caller, null);
    env.addHandler(Throwable.class, onFailure);
    return new Continuable(// perhaps at some point in the future we'll let the Step implementation control this.
    inv.invoke(env, null, onSuccess));
}
Also used : FunctionCallEnv(com.cloudbees.groovy.cps.impl.FunctionCallEnv) Continuable(com.cloudbees.groovy.cps.Continuable) SandboxInvoker(com.cloudbees.groovy.cps.sandbox.SandboxInvoker) TryBlockEnv(com.cloudbees.groovy.cps.impl.TryBlockEnv)

Aggregations

Continuable (com.cloudbees.groovy.cps.Continuable)2 SandboxInvoker (com.cloudbees.groovy.cps.sandbox.SandboxInvoker)2 Env (com.cloudbees.groovy.cps.Env)1 Outcome (com.cloudbees.groovy.cps.Outcome)1 FunctionCallEnv (com.cloudbees.groovy.cps.impl.FunctionCallEnv)1 TryBlockEnv (com.cloudbees.groovy.cps.impl.TryBlockEnv)1 DefaultInvoker (com.cloudbees.groovy.cps.sandbox.DefaultInvoker)1 FlowStartNode (org.jenkinsci.plugins.workflow.graph.FlowStartNode)1