Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Honor skip body block.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Nov 19, 2014
1 parent 2307543 commit e24a64a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
Expand Up @@ -18,12 +18,14 @@
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.BodyExecution;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;

import javax.annotation.CheckForNull;
import javax.annotation.concurrent.GuardedBy;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -32,10 +34,10 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.PROGRAM;
import static java.util.logging.Level.*;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;

/**
* {@link BodyExecution} impl for CPS.
Expand Down Expand Up @@ -80,9 +82,15 @@ class CpsBodyExecution extends BodyExecution {
@GuardedBy("this")
private Outcome outcome;

public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> callbacks) {
/**
* @see CpsBodyInvoker#createBodyBlockNode
*/
private final boolean createBodyBlockNode;

public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> callbacks, boolean createBodyBlockNode) {
this.context = context;
this.callbacks = callbacks;
this.createBodyBlockNode = createBodyBlockNode;
}

/**
Expand All @@ -102,7 +110,6 @@ public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> call
if (a!=null)
sn.addAction(a);
}
head.setNewHead(sn);
fireOnStart(sn);

try {
Expand All @@ -124,18 +131,6 @@ public CpsBodyExecution(CpsStepContext context, List<BodyExecutionCallback> call
}
}

/**
* Inserts the flow node that indicates the beginning of the body invocation.
*
* @see CpsBodyExecution#addBodyEndFlowNode()
*/
private StepStartNode addBodyStartFlowNode(FlowHead head) {
StepStartNode start = new StepStartNode(head.getExecution(),
context.getStepDescriptor(), head.get());
start.addAction(new BodyInvocationAction());
return start;
}

/**
* Creates {@link Continuable} that executes the given invocation and pass its result to {@link FutureCallback}.
*
Expand Down Expand Up @@ -194,7 +189,7 @@ public void onSuccess(CpsThreadGroup g) {
try {
s.stop(stopped);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to stop " + s, e);
LOGGER.log(WARNING, "Failed to stop " + s, e);
}
}

Expand Down Expand Up @@ -271,7 +266,7 @@ public boolean isDone() {
}

/*package*/ void fireOnStart(StepStartNode sn) {
CpsBodySubContext sc = new CpsBodySubContext(context,sn);
StepContext sc = subContext(sn);
for (BodyExecutionCallback c : callbacks) {
c.setContext(sc);
c.onStart();
Expand All @@ -287,7 +282,7 @@ public Next receive(Object o) {
en.addAction(new ErrorAction(t));

setOutcome(new Outcome(null,t));
CpsBodySubContext sc = new CpsBodySubContext(context,en);
StepContext sc = subContext(en);
for (BodyExecutionCallback c : callbacks) {
c.setContext(sc);
c.onFailure(t);
Expand All @@ -305,7 +300,7 @@ public Next receive(Object o) {
StepEndNode en = addBodyEndFlowNode();

setOutcome(new Outcome(o,null));
CpsBodySubContext sc = new CpsBodySubContext(context,en);
StepContext sc = subContext(en);
for (BodyExecutionCallback c : callbacks) {
c.setContext(sc);
c.onSuccess(o);
Expand All @@ -317,28 +312,64 @@ public Next receive(Object o) {
private static final long serialVersionUID = 1L;
}

/**
* Creates a sub-context to call {@link BodyExecutionCallback}.
* If {@link #createBodyBlockNode} is false, then we don't have distinctive
* {@link FlowNode}, so we just hand out the master context.
*/
private StepContext subContext(FlowNode n) {
if (n==null)
return context;
else
return new CpsBodySubContext(context,n);
}

/**
* Inserts the flow node that indicates the beginning of the body invocation.
*
* @see #addBodyStartFlowNode(FlowHead)
* @see #addBodyEndFlowNode()
*/
private StepEndNode addBodyEndFlowNode() {
try {
FlowHead head = CpsThread.current().head;

StepEndNode end = new StepEndNode(head.getExecution(),
getBodyStartNode(), head.get());
end.addAction(new BodyInvocationAction());
head.setNewHead(end);
private @CheckForNull StepStartNode addBodyStartFlowNode(FlowHead head) {
if (createBodyBlockNode) {
StepStartNode start = new StepStartNode(head.getExecution(),
context.getStepDescriptor(), head.get());
this.startNodeId = start.getId();
start.addAction(new BodyInvocationAction());
head.setNewHead(start);
return start;
} else {
return null;
}
}

return end;
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to grow the flow graph", e);
throw new Error(e);
/**
* Inserts the flow node that indicates the beginning of the body invocation.
*
* @see #addBodyStartFlowNode(FlowHead)
*/
private @CheckForNull StepEndNode addBodyEndFlowNode() {
if (createBodyBlockNode) {
try {
FlowHead head = CpsThread.current().head;

StepEndNode end = new StepEndNode(head.getExecution(),
getBodyStartNode(), head.get());
end.addAction(new BodyInvocationAction());
head.setNewHead(end);

return end;
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to grow the flow graph", e);
throw new Error(e);
}
} else {
return null;
}
}

public StepStartNode getBodyStartNode() throws IOException {
if (startNodeId==null)
throw new IllegalStateException("StepStartNode is not yet created");
return (StepStartNode) thread.getExecution().getNode(startNodeId);
}

Expand Down
Expand Up @@ -26,6 +26,9 @@

import com.google.common.util.concurrent.FutureCallback;
import hudson.model.Action;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
Expand Down Expand Up @@ -63,9 +66,10 @@ public final class CpsBodyInvoker extends BodyInvoker {

private String displayName;

private boolean createBodyStartNode = true;

private boolean started;
/**
* If false, do not create inner {@link StepStartNode}/{@link StepEndNode}.
*/
private boolean createBodyBlockNode = true;

/**
* Set to non-null once {@linkplain #start() started}.
Expand Down Expand Up @@ -101,7 +105,7 @@ public CpsBodyInvoker withCallback(BodyExecutionCallback callback) {
@Override
public CpsBodyInvoker withDisplayName(@Nullable String name) {
this.displayName = name;
createBodyStartNode = (name==null);
createBodyBlockNode = (name==null);
return this;
}

Expand All @@ -113,7 +117,15 @@ public CpsBodyInvoker withDisplayName(@Nullable String name) {
@Override
public CpsBodyExecution start() {
if (execution!=null) throw new IllegalStateException("Already started");
execution = new CpsBodyExecution(owner, callbacks);
execution = new CpsBodyExecution(owner, callbacks, createBodyBlockNode);

if (displayName!=null)
startNodeActions.add(new LabelAction(displayName));

if (!createBodyBlockNode) {
if (!startNodeActions.isEmpty())
throw new IllegalStateException("Can't specify Actions if there will be no StepStartNode");
}

if (owner.isSyncMode()) {
// we call 'launch' later from DSL.ThreadTaskImpl
Expand Down
Expand Up @@ -23,6 +23,9 @@ final class CpsBodySubContext extends DefaultStepContext {
*/
private final CpsStepContext base;

/**
* Node that this sub-context points to.
*/
private final FlowNode node;

CpsBodySubContext(CpsStepContext base, FlowNode node) {
Expand Down

0 comments on commit e24a64a

Please sign in to comment.