Search in sources :

Example 1 with Timeout

use of org.jenkinsci.plugins.workflow.support.concurrent.Timeout in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method suspendAll.

@Restricted(DoNotUse.class)
@Terminator
public static void suspendAll() {
    CpsFlowExecution exec = null;
    try (Timeout t = Timeout.limit(3, TimeUnit.MINUTES)) {
        // TODO some complicated sequence of calls to Futures could allow all of them to run in parallel
        LOGGER.fine("starting to suspend all executions");
        for (FlowExecution execution : FlowExecutionList.get()) {
            if (execution instanceof CpsFlowExecution) {
                LOGGER.log(Level.FINE, "waiting to suspend {0}", execution);
                exec = (CpsFlowExecution) execution;
                // Like waitForSuspension but with a timeout:
                if (exec.programPromise != null) {
                    exec.programPromise.get(1, TimeUnit.MINUTES).scheduleRun().get(1, TimeUnit.MINUTES);
                }
            }
        }
        LOGGER.fine("finished suspending all executions");
    } catch (Exception x) {
        LOGGER.log(Level.WARNING, "problem suspending " + exec, x);
    }
}
Also used : Timeout(org.jenkinsci.plugins.workflow.support.concurrent.Timeout) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) AbortException(hudson.AbortException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) Restricted(org.kohsuke.accmod.Restricted) Terminator(hudson.init.Terminator)

Example 2 with Timeout

use of org.jenkinsci.plugins.workflow.support.concurrent.Timeout in project workflow-cps-plugin by jenkinsci.

the class CpsThread method runNextChunk.

/**
 * Executes CPS code synchronously a little bit more, until it hits
 * the point the workflow needs to be dehydrated.
 */
@SuppressWarnings("rawtypes")
@Nonnull
Outcome runNextChunk() {
    assert program != null;
    Outcome outcome;
    final CpsThread old = CURRENT.get();
    CURRENT.set(this);
    try (Timeout timeout = Timeout.limit(5, TimeUnit.MINUTES)) {
        LOGGER.log(FINE, "runNextChunk on {0}", resumeValue);
        final Outcome o = resumeValue;
        resumeValue = null;
        outcome = program.run0(o, categories);
        if (outcome.getAbnormal() != null) {
            LOGGER.log(FINE, "ran and produced error", outcome.getAbnormal());
        } else {
            LOGGER.log(FINE, "ran and produced {0}", outcome);
        }
        if (outcome.getNormal() instanceof ThreadTask) {
            // if an execution in the thread safepoint is requested, deliver that
            ThreadTask sc = (ThreadTask) outcome.getNormal();
            ThreadTaskResult r = sc.eval(this);
            if (r.resume != null) {
                // yield, then keep evaluating the CPS code
                resumeValue = r.resume;
            } else {
                // break but with a different value
                outcome = r.suspend;
            }
        }
    } finally {
        CURRENT.set(old);
    }
    if (promise != null) {
        if (outcome.isSuccess())
            promise.set(outcome.getNormal());
        else {
            try {
                promise.setException(outcome.getAbnormal());
            } catch (Error e) {
                if (e == outcome.getAbnormal()) {
                    // affects other places that use SettableFuture
                    ;
                } else {
                    throw e;
                }
            }
        }
        promise = null;
    }
    return outcome;
}
Also used : Outcome(com.cloudbees.groovy.cps.Outcome) Timeout(org.jenkinsci.plugins.workflow.support.concurrent.Timeout) Nonnull(javax.annotation.Nonnull)

Aggregations

Timeout (org.jenkinsci.plugins.workflow.support.concurrent.Timeout)2 Outcome (com.cloudbees.groovy.cps.Outcome)1 AbortException (hudson.AbortException)1 Terminator (hudson.init.Terminator)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Nonnull (javax.annotation.Nonnull)1 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)1 Restricted (org.kohsuke.accmod.Restricted)1