Skip to content

Commit 66ac3f7

Browse files
authored
Reduce use of workflow-durable-task-step dep through SubTask.getOwnerExecutable (#197)
1 parent ef682b2 commit 66ac3f7

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ THE SOFTWARE.
113113
<dependency>
114114
<groupId>org.jenkins-ci.plugins.workflow</groupId>
115115
<artifactId>workflow-durable-task-step</artifactId>
116+
<!-- TODO switch to test scope once PlaceholderTask.getNode call can be abstracted -->
116117
</dependency>
117118
<dependency>
118119
<groupId>org.jenkins-ci.plugins.workflow</groupId>

src/main/java/hudson/plugins/throttleconcurrents/ThrottleQueueTaskDispatcher.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,9 @@ public List<ParameterValue> getParametersFromWorkUnit(WorkUnit unit) {
407407
paramsList = ((ParametersAction) action).getParameters();
408408
}
409409
}
410-
} else if (unit.context.task instanceof PlaceholderTask) {
411-
PlaceholderTask placeholderTask = (PlaceholderTask) unit.context.task;
412-
Run<?, ?> run = placeholderTask.run();
413-
if (run != null) {
410+
} else {
411+
Queue.Executable ownerExecutable = unit.context.task.getOwnerExecutable();
412+
if (ownerExecutable instanceof Run<?, ?> run) {
414413
List<ParametersAction> actions = run.getActions(ParametersAction.class);
415414
for (ParametersAction action : actions) {
416415
paramsList = action.getParameters();
@@ -435,11 +434,11 @@ public List<ParameterValue> getParametersFromQueueItem(Queue.Item item) {
435434

436435
@NonNull
437436
private List<String> categoriesForPipeline(Task task) {
438-
if (task instanceof PlaceholderTask) {
439-
PlaceholderTask placeholderTask = (PlaceholderTask) task;
440-
Run<?, ?> r = placeholderTask.run();
441-
if (r != null) {
442-
Map<String, List<String>> categoriesByFlowNode = ThrottleJobProperty.getCategoriesForRunByFlowNode(r);
437+
// TODO avoid casting to PlaceholderTask; could task.node.id be replaced with task.affinityKey?
438+
if (task instanceof PlaceholderTask placeholderTask) {
439+
Queue.Executable ownerExecutable = task.getOwnerExecutable();
440+
if (ownerExecutable instanceof Run<?, ?> run) {
441+
Map<String, List<String>> categoriesByFlowNode = ThrottleJobProperty.getCategoriesForRunByFlowNode(run);
443442
if (!categoriesByFlowNode.isEmpty()) {
444443
try (Timeout t = Timeout.limit(100, TimeUnit.MILLISECONDS)) {
445444
FlowNode firstThrottle = firstThrottleStartNode(placeholderTask.getNode());
@@ -463,8 +462,7 @@ private List<String> categoriesForPipeline(Task task) {
463462

464463
@CheckForNull
465464
private ThrottleJobProperty getThrottleJobProperty(Task task) {
466-
if (task instanceof Job) {
467-
Job<?, ?> p = (Job<?, ?>) task;
465+
if (task instanceof Job<?, ?> p) {
468466
if (task instanceof MatrixConfiguration) {
469467
p = ((MatrixConfiguration) task).getParent();
470468
}
@@ -601,10 +599,10 @@ private int pipelinesOnExecutor(@NonNull Run<?, ?> run, @NonNull Executor exec,
601599
final Queue.Executable currentExecutable = exec.getCurrentExecutable();
602600
if (currentExecutable != null) {
603601
SubTask parent = currentExecutable.getParent();
604-
if (parent instanceof PlaceholderTask) {
605-
PlaceholderTask task = (PlaceholderTask) parent;
606-
if (run.equals(task.run())) {
607-
if (isTaskThrottledPipeline(task, flowNodes)) {
602+
if (parent instanceof Task) {
603+
Queue.Executable ownerExecutable = parent.getOwnerExecutable();
604+
if (ownerExecutable instanceof Run<?, ?> run2 && run.equals(run2)) {
605+
if (isTaskThrottledPipeline((Task) parent, flowNodes)) {
608606
return 1;
609607
}
610608
}
@@ -615,8 +613,7 @@ private int pipelinesOnExecutor(@NonNull Run<?, ?> run, @NonNull Executor exec,
615613
}
616614

617615
private boolean isTaskThrottledPipeline(Task origTask, List<FlowNode> flowNodes) {
618-
if (origTask instanceof PlaceholderTask) {
619-
PlaceholderTask task = (PlaceholderTask) origTask;
616+
if (origTask instanceof PlaceholderTask task) { // TODO as in categoriesForPipeline
620617
try {
621618
FlowNode firstThrottle = firstThrottleStartNode(task.getNode());
622619
return firstThrottle != null && flowNodes.contains(firstThrottle);

0 commit comments

Comments
 (0)