Search in sources :

Example 11 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method onLoad.

@Override
protected void onLoad() {
    super.onLoad();
    if (completed != null) {
        throw new IllegalStateException("double onLoad of " + this);
    }
    FlowExecution fetchedExecution = execution;
    if (fetchedExecution != null) {
        try {
            if (getParent().isResumeBlocked() && execution instanceof BlockableResume) {
                ((BlockableResume) execution).setResumeBlocked(true);
            }
            fetchedExecution.onLoad(new Owner(this));
        } catch (Exception x) {
            LOGGER.log(Level.WARNING, null, x);
            // probably too broken to use
            execution = null;
        }
    }
    fetchedExecution = execution;
    if (fetchedExecution != null) {
        fetchedExecution.addListener(new GraphL());
        executionPromise.set(fetchedExecution);
        if (!fetchedExecution.isComplete()) {
            // we've been restarted while we were running. let's get the execution going again.
            FlowExecutionListener.fireResumed(fetchedExecution);
            try {
                OutputStream logger = new FileOutputStream(getLogFile(), true);
                listener = new StreamBuildListener(logger, Charset.defaultCharset());
                listener.getLogger().println("Resuming build at " + new Date() + " after Jenkins restart");
            } catch (IOException x) {
                LOGGER.log(Level.WARNING, null, x);
                listener = new StreamBuildListener(new NullStream());
            }
            completed = new AtomicBoolean();
            Timer.get().submit(new // JENKINS-31614
            Runnable() {

                @Override
                public void run() {
                    Queue.getInstance().schedule(new AfterRestartTask(WorkflowRun.this), 0);
                }
            });
        }
    }
    // only for diagnostics
    checkouts(null);
    synchronized (LOADING_RUNS) {
        // or could just make the value type be WeakReference<WorkflowRun>
        LOADING_RUNS.remove(key());
        LOADING_RUNS.notifyAll();
    }
}
Also used : FlowExecutionOwner(org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner) NullStream(hudson.util.NullStream) LineTransformationOutputStream(hudson.console.LineTransformationOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) StreamBuildListener(hudson.model.StreamBuildListener) AbortException(hudson.AbortException) IOException(java.io.IOException) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) Date(java.util.Date) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FileOutputStream(java.io.FileOutputStream) BlockableResume(org.jenkinsci.plugins.workflow.flow.BlockableResume)

Example 12 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method finish.

/**
 * Handles normal build completion (including errors) but also handles the case that the flow did not even start correctly, for example due to an error in {@link FlowExecution#start}.
 */
private void finish(@Nonnull Result r, @CheckForNull Throwable t) {
    setResult(r);
    duration = Math.max(0, System.currentTimeMillis() - getStartTimeInMillis());
    LOGGER.log(Level.INFO, "{0} completed: {1}", new Object[] { toString(), getResult() });
    if (listener == null) {
        LOGGER.log(Level.WARNING, this + " failed to start", t);
    } else {
        RunListener.fireCompleted(WorkflowRun.this, listener);
        if (t instanceof AbortException) {
            listener.error(t.getMessage());
        } else if (t instanceof FlowInterruptedException) {
            ((FlowInterruptedException) t).handle(this, listener);
        } else if (t != null) {
            // TODO 2.43+ use Functions.printStackTrace
            listener.getLogger().println(Functions.printThrowable(t).trim());
        }
        listener.finished(getResult());
        listener.closeQuietly();
    }
    logsToCopy = null;
    try {
        save();
    } catch (Exception x) {
        LOGGER.log(Level.WARNING, "failed to save " + this, x);
    }
    Timer.get().submit(() -> {
        try {
            getParent().logRotate();
        } catch (Exception x) {
            LOGGER.log(Level.WARNING, "failed to perform log rotation after " + this, x);
        }
    });
    onEndBuilding();
    if (completed != null) {
        synchronized (completed) {
            completed.set(true);
        }
    }
    FlowExecutionList.get().unregister(new Owner(this));
    try {
        StashManager.maybeClearAll(this);
    } catch (IOException x) {
        LOGGER.log(Level.WARNING, "failed to clean up stashes from " + this, x);
    }
    FlowExecution exec = getExecution();
    if (exec != null) {
        FlowExecutionListener.fireCompleted(exec);
    }
}
Also used : FlowExecutionOwner(org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) IOException(java.io.IOException) AbortException(hudson.AbortException) IOException(java.io.IOException) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) AbortException(hudson.AbortException)

Example 13 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project blueocean-plugin by jenkinsci.

the class DownstreamJobListener method onStarted.

@Override
public void onStarted(Run<?, ?> run, TaskListener listener) {
    for (BuildUpstreamNodeAction action : run.getActions(BuildUpstreamNodeAction.class)) {
        Run triggerRun = Run.fromExternalizableId(action.getUpstreamRunId());
        if (triggerRun instanceof WorkflowRun) {
            FlowExecution execution = ((WorkflowRun) triggerRun).getExecution();
            FlowNode node;
            if (execution == null) {
                LOGGER.warning("Could not retrieve upstream FlowExecution");
                continue;
            }
            try {
                node = execution.getNode(action.getUpstreamNodeId());
            } catch (IOException e) {
                LOGGER.warning("Could not retrieve upstream node: " + e);
                continue;
            }
            if (node == null) {
                LOGGER.warning("Could not retrieve upstream node (null)");
                continue;
            }
            // Add an action on the triggerRun node pointing to the currently executing run
            String description = run.getDescription();
            if (description == null) {
                description = run.getFullDisplayName();
            }
            Link link = LinkResolver.resolveLink(run);
            if (link != null) {
                try {
                    // Also add to the actual trigger node so we can find it later by step
                    node.addAction(new NodeDownstreamBuildAction(link, description));
                    node.save();
                } catch (IOException e) {
                    LOGGER.severe("Could not persist node: " + e);
                }
            }
        }
    }
}
Also used : FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Run(hudson.model.Run) IOException(java.io.IOException) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Link(io.jenkins.blueocean.rest.hal.Link) BuildUpstreamNodeAction(org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamNodeAction) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 14 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project blueocean-plugin by jenkinsci.

the class PipelineNodeGraphVisitor method getPipelineNodeSteps.

@Override
public List<BluePipelineStep> getPipelineNodeSteps(Link parent) {
    FlowExecution execution = run.getExecution();
    if (execution == null) {
        return Collections.emptyList();
    }
    PipelineStepVisitor visitor = new PipelineStepVisitor(run, null);
    ForkScanner.visitSimpleChunks(execution.getCurrentHeads(), visitor, new StageChunkFinder());
    List<BluePipelineStep> steps = new ArrayList<>();
    for (FlowNodeWrapper node : visitor.getSteps()) {
        steps.add(new PipelineStepImpl(node, parent));
    }
    return steps;
}
Also used : StageChunkFinder(org.jenkinsci.plugins.workflow.pipelinegraphanalysis.StageChunkFinder) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) ArrayList(java.util.ArrayList) BluePipelineStep(io.jenkins.blueocean.rest.model.BluePipelineStep)

Example 15 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method maybeAutoPersistNode.

/**
 * Invoke me to toggle autopersist back on for steps that delay it.
 */
public static void maybeAutoPersistNode(@Nonnull FlowNode node) {
    try {
        FlowExecution exec = node.getExecution();
        if (exec instanceof CpsFlowExecution) {
            if (exec.getDurabilityHint().isPersistWithEveryStep()) {
                FlowNodeStorage exc = ((CpsFlowExecution) exec).getStorage();
                exc.autopersist(node);
            }
        }
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING, "Attempt to persist triggered IOException for node " + node.getId(), ioe);
    }
}
Also used : FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) IOException(java.io.IOException) BulkFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.BulkFlowNodeStorage) FlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.FlowNodeStorage) SimpleXStreamFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage)

Aggregations

FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)28 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)10 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 Statement (org.junit.runners.model.Statement)7 Jenkins (jenkins.model.Jenkins)6 FlowExecutionOwner (org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner)6 AbortException (hudson.AbortException)5 IOException (java.io.IOException)5 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)4 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)4 FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)3 LineTransformationOutputStream (hudson.console.LineTransformationOutputStream)2 StreamBuildListener (hudson.model.StreamBuildListener)2 CopyOnWriteList (hudson.util.CopyOnWriteList)2 BluePipelineStep (io.jenkins.blueocean.rest.model.BluePipelineStep)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2