/
ThrottleStepExecution.java
86 lines (69 loc) · 2.81 KB
/
ThrottleStepExecution.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
package hudson.plugins.throttleconcurrents.pipeline;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.throttleconcurrents.ThrottleJobProperty;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
public class ThrottleStepExecution extends StepExecution {
private final ThrottleStep step;
public ThrottleStepExecution(@Nonnull ThrottleStep step, StepContext context) {
super(context);
this.step = step;
}
@Nonnull
public List<String> getCategories() {
return step.getCategories();
}
@Override
public boolean start() throws Exception {
Run<?, ?> r = getContext().get(Run.class);
TaskListener listener = getContext().get(TaskListener.class);
FlowNode flowNode = getContext().get(FlowNode.class);
ThrottleJobProperty.DescriptorImpl descriptor = ThrottleJobProperty.fetchDescriptor();
String runId = null;
String flowNodeId = null;
if (r != null && flowNode != null) {
runId = r.getExternalizableId();
flowNodeId = flowNode.getId();
for (String category : getCategories()) {
descriptor.addThrottledPipelineForCategory(runId, flowNodeId, category, listener);
}
}
getContext().newBodyInvoker()
.withCallback(new Callback(runId, flowNodeId, getCategories()))
.start();
return false;
}
@Override
public void stop(Throwable cause) throws Exception {
}
private static final class Callback extends BodyExecutionCallback.TailCall {
@CheckForNull
private String runId;
@CheckForNull
private String flowNodeId;
private List<String> categories = new ArrayList<>();
private static final long serialVersionUID = 1;
Callback(@CheckForNull String runId, @CheckForNull String flowNodeId, @Nonnull List<String> categories) {
this.runId = runId;
this.flowNodeId = flowNodeId;
this.categories.addAll(categories);
}
@Override protected void finished(StepContext context) throws Exception {
if (runId != null && flowNodeId != null) {
for (String category : categories) {
ThrottleJobProperty.fetchDescriptor().removeThrottledPipelineForCategory(runId,
flowNodeId,
category,
context.get(TaskListener.class));
}
}
}
}
}