Skip to content

Commit fc6eb22

Browse files
authored
[NO-ISSUE] Add wait to DSL (#1294)
* Add wait to DSL Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Use LifeCycleEventsTest to test DSL Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Add DSL test to ForkWaitTest class Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Refactor test and to allow named branches Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Update FuncForkTaskBuilder Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * FuncForkTaskBuilder#branch method Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Remove dead code Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> --------- Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 6128414 commit fc6eb22

15 files changed

Lines changed: 418 additions & 48 deletions

File tree

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncForkTaskBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func;
1717

18+
import io.serverlessworkflow.api.types.DoTask;
1819
import io.serverlessworkflow.api.types.ForkTask;
1920
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
2021
import io.serverlessworkflow.api.types.Task;
@@ -59,6 +60,17 @@ public <T, V> FuncForkTaskBuilder branch(
5960
return branch(name, function, argParam, null);
6061
}
6162

63+
@Override
64+
public FuncForkTaskBuilder branch(String name, Consumer<FuncTaskItemListBuilder> branchConsumer) {
65+
if (name == null || name.isBlank()) {
66+
name = "branch-" + this.items.size();
67+
}
68+
final FuncTaskItemListBuilder branchItems = new FuncTaskItemListBuilder(this.items);
69+
this.items.add(
70+
new TaskItem(name, new Task().withDoTask(new DoTask().withDo(branchItems.build()))));
71+
return this;
72+
}
73+
6274
public <T, V> FuncForkTaskBuilder branch(
6375
String name, Function<T, V> function, Class<T> argParam, Class<V> returnClass) {
6476
if (name == null || name.isBlank()) {

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/BaseTaskItemListBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public abstract class BaseTaskItemListBuilder<SELF extends BaseTaskItemListBuild
4242
protected final String TYPE_LISTEN = "listen";
4343
protected final String TYPE_EMIT = "emit";
4444
protected final String TYPE_TRY = "try";
45+
protected final String TYPE_WAIT = "wait";
4546
protected final String TYPE_HTTP = "http";
4647
protected final String TYPE_OPENAPI = "openapi";
4748
protected final String TYPE_WORKFLOW = "workflow";

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/DoTaskBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public DoTaskBuilder set(String name, String expr) {
7979
return this;
8080
}
8181

82+
@Override
83+
public DoTaskBuilder wait(String name, Consumer<WaitTaskBuilder> itemsConfigurer) {
84+
this.listBuilder().wait(name, itemsConfigurer);
85+
return this;
86+
}
87+
8288
@Override
8389
public DoTaskBuilder switchCase(String name, Consumer<SwitchTaskBuilder> itemsConfigurer) {
8490
this.listBuilder().switchCase(name, itemsConfigurer);

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/ForkTaskBuilder.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package io.serverlessworkflow.fluent.spec;
1717

18+
import io.serverlessworkflow.api.types.DoTask;
1819
import io.serverlessworkflow.api.types.ForkTask;
1920
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
21+
import io.serverlessworkflow.api.types.Task;
2022
import io.serverlessworkflow.api.types.TaskItem;
2123
import io.serverlessworkflow.fluent.spec.spi.ForkTaskFluent;
24+
import java.util.ArrayList;
2225
import java.util.List;
26+
import java.util.Objects;
2327
import java.util.function.Consumer;
2428

2529
public class ForkTaskBuilder extends TaskBaseBuilder<ForkTaskBuilder>
@@ -58,14 +62,39 @@ public ForkTaskBuilder branches(Consumer<TaskItemListBuilder> branchesConsumer)
5862
if (existingBranches == null || existingBranches.isEmpty()) {
5963
this.forkTaskConfiguration.setBranches(newBranches);
6064
} else {
61-
List<TaskItem> merged = new java.util.ArrayList<>(existingBranches);
65+
List<TaskItem> merged = new ArrayList<>(existingBranches);
6266
merged.addAll(newBranches);
6367
this.forkTaskConfiguration.setBranches(merged);
6468
}
6569

6670
return this;
6771
}
6872

73+
@Override
74+
public ForkTaskBuilder branch(String name, Consumer<TaskItemListBuilder> branchConsumer) {
75+
Objects.requireNonNull(branchConsumer, "Branch consumer must not be null");
76+
77+
List<TaskItem> existingBranches = this.forkTaskConfiguration.getBranches();
78+
int currentOffset = (existingBranches == null) ? 0 : existingBranches.size();
79+
String branchName = (name == null || name.isBlank()) ? "branch-" + currentOffset : name;
80+
81+
TaskItemListBuilder branchItems = new TaskItemListBuilder(0);
82+
branchConsumer.accept(branchItems);
83+
84+
TaskItem branchItem =
85+
new TaskItem(branchName, new Task().withDoTask(new DoTask().withDo(branchItems.build())));
86+
87+
if (existingBranches == null || existingBranches.isEmpty()) {
88+
this.forkTaskConfiguration.setBranches(List.of(branchItem));
89+
} else {
90+
List<TaskItem> merged = new ArrayList<>(existingBranches);
91+
merged.add(branchItem);
92+
this.forkTaskConfiguration.setBranches(merged);
93+
}
94+
95+
return this;
96+
}
97+
6998
@Override
7099
public ForkTask build() {
71100
return this.forkTask.withFork(this.forkTaskConfiguration);

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/TaskItemListBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public TaskItemListBuilder set(String name, final String expr) {
5656
return this.set(name, s -> s.expr(expr));
5757
}
5858

59+
@Override
60+
public TaskItemListBuilder wait(String name, Consumer<WaitTaskBuilder> itemsConfigurer) {
61+
name = defaultNameAndRequireConfig(name, itemsConfigurer, TYPE_WAIT);
62+
final WaitTaskBuilder waitBuilder = new WaitTaskBuilder();
63+
itemsConfigurer.accept(waitBuilder);
64+
return addTaskItem(new TaskItem(name, new Task().withWaitTask(waitBuilder.build())));
65+
}
66+
5967
@Override
6068
public TaskItemListBuilder forEach(
6169
String name, Consumer<ForEachTaskBuilder<TaskItemListBuilder>> itemsConfigurer) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.spec;
17+
18+
import io.serverlessworkflow.api.types.DurationInline;
19+
import io.serverlessworkflow.api.types.TimeoutAfter;
20+
import io.serverlessworkflow.api.types.WaitTask;
21+
import java.time.Duration;
22+
import java.util.Objects;
23+
import java.util.function.Consumer;
24+
25+
public class WaitTaskBuilder extends TaskBaseBuilder<WaitTaskBuilder> {
26+
27+
private final WaitTask waitTask;
28+
29+
public WaitTaskBuilder() {
30+
this.waitTask = new WaitTask();
31+
setTask(this.waitTask);
32+
}
33+
34+
@Override
35+
protected WaitTaskBuilder self() {
36+
return this;
37+
}
38+
39+
public WaitTaskBuilder wait(Consumer<TimeoutBuilder> waitConsumer) {
40+
final TimeoutBuilder timeoutBuilder = new TimeoutBuilder();
41+
waitConsumer.accept(timeoutBuilder);
42+
this.waitTask.setWait(timeoutBuilder.build().getAfter());
43+
return this;
44+
}
45+
46+
public WaitTaskBuilder wait(String durationExpression) {
47+
this.waitTask.setWait(new TimeoutAfter().withDurationExpression(durationExpression));
48+
return this;
49+
}
50+
51+
public WaitTaskBuilder wait(Duration duration) {
52+
Objects.requireNonNull(duration, "duration must not be null");
53+
if (duration.isNegative()) {
54+
throw new IllegalArgumentException("duration must not be negative");
55+
}
56+
57+
long millis = duration.toMillis();
58+
59+
int days = Math.toIntExact(millis / 86_400_000L);
60+
millis %= 86_400_000L;
61+
int hours = Math.toIntExact(millis / 3_600_000L);
62+
millis %= 3_600_000L;
63+
int minutes = Math.toIntExact(millis / 60_000L);
64+
millis %= 60_000L;
65+
int seconds = Math.toIntExact(millis / 1_000L);
66+
int milliseconds = Math.toIntExact(millis % 1_000L);
67+
68+
this.waitTask.setWait(
69+
new TimeoutAfter()
70+
.withDurationInline(
71+
new DurationInline()
72+
.withDays(days)
73+
.withHours(hours)
74+
.withMinutes(minutes)
75+
.withSeconds(seconds)
76+
.withMilliseconds(milliseconds)));
77+
return this;
78+
}
79+
80+
public WaitTask build() {
81+
return this.waitTask;
82+
}
83+
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/dsl/DSL.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,52 @@ public static TasksConfigurer listen(String name, ListenConfigurer configurer) {
797797
return list -> list.listen(name, configurer);
798798
}
799799

800+
/**
801+
* Create a {@link TasksConfigurer} that adds a {@code wait} task configured with an inline
802+
* duration builder.
803+
*
804+
* @param duration timeout builder consumer
805+
* @return a {@link TasksConfigurer} that adds a WaitTask
806+
*/
807+
public static TasksConfigurer wait(Consumer<TimeoutBuilder> duration) {
808+
return list -> list.wait(w -> w.wait(duration));
809+
}
810+
811+
/**
812+
* Create a {@link TasksConfigurer} that adds a named {@code wait} task configured with an inline
813+
* duration builder.
814+
*
815+
* @param name task name
816+
* @param duration timeout builder consumer
817+
* @return a {@link TasksConfigurer} that adds a WaitTask
818+
*/
819+
public static TasksConfigurer wait(String name, Consumer<TimeoutBuilder> duration) {
820+
return list -> list.wait(name, w -> w.wait(duration));
821+
}
822+
823+
/**
824+
* Create a {@link TasksConfigurer} that adds a {@code wait} task configured with a duration
825+
* expression.
826+
*
827+
* @param durationExpression duration expression
828+
* @return a {@link TasksConfigurer} that adds a WaitTask
829+
*/
830+
public static TasksConfigurer wait(String durationExpression) {
831+
return list -> list.wait(w -> w.wait(durationExpression));
832+
}
833+
834+
/**
835+
* Create a {@link TasksConfigurer} that adds a named {@code wait} task configured with a duration
836+
* expression.
837+
*
838+
* @param name task name
839+
* @param durationExpression duration expression
840+
* @return a {@link TasksConfigurer} that adds a WaitTask
841+
*/
842+
public static TasksConfigurer wait(String name, String durationExpression) {
843+
return list -> list.wait(name, w -> w.wait(durationExpression));
844+
}
845+
800846
/**
801847
* Create a {@link TasksConfigurer} that adds a {@code forEach} task.
802848
*

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/DoFluent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.serverlessworkflow.fluent.spec.SwitchTaskBuilder;
2727
import io.serverlessworkflow.fluent.spec.TaskItemListBuilder;
2828
import io.serverlessworkflow.fluent.spec.TryTaskBuilder;
29+
import io.serverlessworkflow.fluent.spec.WaitTaskBuilder;
2930
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
3031

3132
/**
@@ -44,6 +45,7 @@ public interface DoFluent<T>
4445
ForEachFluent<ForEachTaskBuilder<TaskItemListBuilder>, T>,
4546
ForkFluent<ForkTaskBuilder, T>,
4647
ListenFluent<ListenTaskBuilder, T>,
48+
WaitFluent<WaitTaskBuilder, T>,
4749
RaiseFluent<RaiseTaskBuilder, T>,
4850
CallOpenAPIFluent<CallOpenAPITaskBuilder, T>,
4951
WorkflowFluent<WorkflowTaskBuilder, T> {}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/ForkTaskFluent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ public interface ForkTaskFluent<
2727

2828
SELF branches(Consumer<L> branchesConsumer);
2929

30+
default SELF branch(Consumer<L> branchConsumer) {
31+
return branch(null, branchConsumer);
32+
}
33+
34+
SELF branch(String name, Consumer<L> branchConsumer);
35+
3036
ForkTask build();
3137
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.spec.spi;
17+
18+
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
19+
import java.util.function.Consumer;
20+
21+
public interface WaitFluent<SELF extends TaskBaseBuilder<?>, LIST> {
22+
23+
LIST wait(String name, Consumer<SELF> itemsConfigurer);
24+
25+
default LIST wait(Consumer<SELF> itemsConfigurer) {
26+
return this.wait(null, itemsConfigurer);
27+
}
28+
}

0 commit comments

Comments
 (0)