This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
/
ParallelStepExecution.java
92 lines (74 loc) · 2.89 KB
/
ParallelStepExecution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.jenkinsci.plugins.workflow.cps.steps;
import groovy.lang.Closure;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
import org.jenkinsci.plugins.workflow.cps.CpsStepContext;
import org.jenkinsci.plugins.workflow.cps.CpsThread;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.cps.steps.ParallelStep.ResultHandler;
import org.jenkinsci.plugins.workflow.steps.BodyExecution;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.FLOW_NODE;
/**
* {@link StepExecution} for {@link ParallelStep}.
*
* @author Kohsuke Kawaguchi
*/
class ParallelStepExecution extends StepExecution {
private transient ParallelStep parallelStep;
private final List<BodyExecution> bodies = new ArrayList<BodyExecution>();
public ParallelStepExecution(ParallelStep parallelStep, StepContext context) {
super(context);
this.parallelStep = parallelStep;
}
@Override
public boolean start() throws Exception {
CpsStepContext cps = (CpsStepContext) getContext();
if (parallelStep.closures.isEmpty()) {
cps.get(TaskListener.class).getLogger().println("No branches to run");
cps.onSuccess(Collections.<String,Object>emptyMap());
return true;
}
CpsThread t = CpsThread.current();
ResultHandler r = new ResultHandler(cps, this, parallelStep.isFailFast());
for (Entry<String,Closure> e : parallelStep.closures.entrySet()) {
BodyExecution body = cps.newBodyInvoker(t.getGroup().export(e.getValue()))
.withStartAction(new ParallelLabelAction(e.getKey()))
.withCallback(r.callbackFor(e.getKey()))
.start();
bodies.add(body);
}
return false;
}
@Override
public void stop(Throwable cause) throws Exception {
for (BodyExecution body : bodies) {
body.cancel(cause);
}
}
private static final long serialVersionUID = 1L;
@PersistIn(FLOW_NODE)
private static class ParallelLabelAction extends LabelAction implements ThreadNameAction {
private final String branchName;
ParallelLabelAction(String branchName) {
super(null);
this.branchName = branchName;
}
@Override
public String getDisplayName() {
return "Parallel branch: "+branchName;
}
@Nonnull
@Override
public String getThreadName() {
return branchName;
}
}
}