Skip to content

Commit e0e57bf

Browse files
committed
fix #1251 - Add timers and scheduler to the DSL
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent ae47e0a commit e0e57bf

13 files changed

Lines changed: 1190 additions & 39 deletions

File tree

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java

Lines changed: 284 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.cloudevents.CloudEventData;
1919
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
20+
import io.serverlessworkflow.api.types.OAuth2AuthenticationData;
2021
import io.serverlessworkflow.api.types.func.ContextFunction;
2122
import io.serverlessworkflow.api.types.func.FilterFunction;
2223
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
@@ -27,7 +28,13 @@
2728
import io.serverlessworkflow.fluent.func.configurers.FuncCallOpenAPIConfigurer;
2829
import io.serverlessworkflow.fluent.func.configurers.FuncTaskConfigurer;
2930
import io.serverlessworkflow.fluent.func.configurers.SwitchCaseConfigurer;
31+
import io.serverlessworkflow.fluent.spec.AbstractEventConsumptionStrategyBuilder;
32+
import io.serverlessworkflow.fluent.spec.EventFilterBuilder;
33+
import io.serverlessworkflow.fluent.spec.ScheduleBuilder;
34+
import io.serverlessworkflow.fluent.spec.TimeoutBuilder;
3035
import io.serverlessworkflow.fluent.spec.configurers.AuthenticationConfigurer;
36+
import io.serverlessworkflow.fluent.spec.dsl.DSL;
37+
import io.serverlessworkflow.fluent.spec.dsl.UseSpec;
3138
import io.serverlessworkflow.impl.TaskContextData;
3239
import io.serverlessworkflow.impl.WorkflowContextData;
3340
import java.net.URI;
@@ -1039,13 +1046,7 @@ public static FuncTaskConfigurer call(String name, FuncCallHttpConfigurer config
10391046
* <p>This overload creates an unnamed HTTP task.
10401047
*
10411048
* <pre>{@code
1042-
* tasks(
1043-
* FuncDSL.call(
1044-
* FuncDSL.http()
1045-
* .GET()
1046-
* .endpoint("http://service/api")
1047-
* )
1048-
* );
1049+
* tasks(FuncDSL.call(FuncDSL.http().GET().endpoint("http://service/api")));
10491050
* }</pre>
10501051
*
10511052
* @param spec fluent HTTP spec built via {@link #http()}
@@ -1059,13 +1060,7 @@ public static FuncTaskConfigurer call(FuncCallHttpStep spec) {
10591060
* HTTP call using a fluent {@link FuncCallHttpStep} with explicit task name.
10601061
*
10611062
* <pre>{@code
1062-
* tasks(
1063-
* FuncDSL.call("fetchUsers",
1064-
* FuncDSL.http()
1065-
* .GET()
1066-
* .endpoint("http://service/users")
1067-
* )
1068-
* );
1063+
* tasks(FuncDSL.call("fetchUsers", FuncDSL.http().GET().endpoint("http://service/users")));
10691064
* }</pre>
10701065
*
10711066
* @param name task name, or {@code null} for an anonymous task
@@ -1084,12 +1079,7 @@ public static FuncTaskConfigurer call(String name, FuncCallHttpStep spec) {
10841079
*
10851080
* <pre>{@code
10861081
* FuncWorkflowBuilder.workflow("openapi-call")
1087-
* .tasks(
1088-
* FuncDSL.call(
1089-
* FuncDSL.openapi()
1090-
* .document("https://petstore.swagger.io/v2/swagger.json", DSL.auth("openapi-auth"))
1091-
* .operation("getPetById")
1092-
* )
1082+
* .tasks(call(openapi().document("https://petstore.swagger.io/v2/swagger.json", auth("openapi-auth")).operation("getPetById"))
10931083
* )
10941084
* .build();
10951085
* }</pre>
@@ -1162,12 +1152,7 @@ public static FuncTaskConfigurer call(String name, FuncCallOpenAPIConfigurer con
11621152
* <p>Typical usage:
11631153
*
11641154
* <pre>{@code
1165-
* FuncDSL.call(
1166-
* FuncDSL.openapi()
1167-
* .document("https://petstore.swagger.io/v2/swagger.json", DSL.auth("openapi-auth"))
1168-
* .operation("getPetById")
1169-
* .parameter("id", 123)
1170-
* );
1155+
* FuncDSL.call(openapi().document("https://petstore.swagger.io/v2/swagger.json", DSL.auth("openapi-auth")).operation("getPetById").parameter("id", 123));
11711156
* }</pre>
11721157
*
11731158
* <p>The returned spec is a fluent builder that records operations (document, operation,
@@ -1197,12 +1182,7 @@ public static FuncCallOpenAPIStep openapi(String name) {
11971182
* <p>Typical usage:
11981183
*
11991184
* <pre>{@code
1200-
* FuncDSL.call(
1201-
* FuncDSL.http()
1202-
* .GET()
1203-
* .endpoint("http://service/api")
1204-
* .acceptJSON()
1205-
* );
1185+
* FuncDSL.call(http().GET().endpoint("http://service/api").acceptJSON());
12061186
* }</pre>
12071187
*
12081188
* @return a new {@link FuncCallHttpStep}
@@ -1474,8 +1454,8 @@ public static <T> T input(WorkflowContextData context, Class<T> inputClass) {
14741454
*
14751455
* <pre>{@code
14761456
* inputFrom((Object obj, TaskContextData taskContextData) -> {
1477-
* OrderRequest order = input(taskContextData, OrderRequest.class);
1478-
* return order;
1457+
* OrderRequest order = input(taskContextData, OrderRequest.class);
1458+
* return order;
14791459
* });
14801460
* }</pre>
14811461
*
@@ -1509,9 +1489,9 @@ public static <T> T input(TaskContextData taskContextData, Class<T> inputClass)
15091489
*
15101490
* <pre>{@code
15111491
* .exportAs((object, workflowContext, taskContextData) -> {
1512-
* Long output = output(taskContextData, Long.class);
1513-
* return output * 2;
1514-
* })
1492+
* Long output = output(taskContextData, Long.class);
1493+
* return output * 2;
1494+
* })
15151495
* }</pre>
15161496
*
15171497
* @param <T> the type to deserialize the task output into
@@ -1530,4 +1510,271 @@ public static <T> T output(TaskContextData taskContextData, Class<T> outputClass
15301510
+ outputClass.getName()
15311511
+ " when calling FuncDSL.output(TaskContextData, Class<T>)."));
15321512
}
1513+
1514+
// ---------------------------------------------------------------------------
1515+
// Facades to base DSL (Timeouts, Schedules, Event Strategies)
1516+
// ---------------------------------------------------------------------------
1517+
1518+
/**
1519+
* Shortcut to configure a timeout in days.
1520+
*
1521+
* @see DSL#timeoutDays(int)
1522+
*/
1523+
public static Consumer<TimeoutBuilder> timeoutDays(int days) {
1524+
return DSL.timeoutDays(days);
1525+
}
1526+
1527+
/**
1528+
* Shortcut to configure a timeout in hours.
1529+
*
1530+
* @see DSL#timeoutHours(int)
1531+
*/
1532+
public static Consumer<TimeoutBuilder> timeoutHours(int hours) {
1533+
return DSL.timeoutHours(hours);
1534+
}
1535+
1536+
/**
1537+
* Shortcut to configure a timeout in minutes.
1538+
*
1539+
* @see DSL#timeoutMinutes(int)
1540+
*/
1541+
public static Consumer<TimeoutBuilder> timeoutMinutes(int minutes) {
1542+
return DSL.timeoutMinutes(minutes);
1543+
}
1544+
1545+
/**
1546+
* Shortcut to configure a timeout in seconds.
1547+
*
1548+
* @see DSL#timeoutSeconds(int)
1549+
*/
1550+
public static Consumer<TimeoutBuilder> timeoutSeconds(int seconds) {
1551+
return DSL.timeoutSeconds(seconds);
1552+
}
1553+
1554+
/**
1555+
* Shortcut to configure a timeout in milliseconds.
1556+
*
1557+
* @see DSL#timeoutMillis(int)
1558+
*/
1559+
public static Consumer<TimeoutBuilder> timeoutMillis(int milliseconds) {
1560+
return DSL.timeoutMillis(milliseconds);
1561+
}
1562+
1563+
// ---- Schedules ----//
1564+
1565+
/**
1566+
* @see DSL#every(Consumer)
1567+
*/
1568+
public static Consumer<ScheduleBuilder> every(Consumer<TimeoutBuilder> duration) {
1569+
return DSL.every(duration);
1570+
}
1571+
1572+
/**
1573+
* @see DSL#every(String)
1574+
*/
1575+
public static Consumer<ScheduleBuilder> every(String durationExpression) {
1576+
return DSL.every(durationExpression);
1577+
}
1578+
1579+
/**
1580+
* @see DSL#cron(String)
1581+
*/
1582+
public static Consumer<ScheduleBuilder> cron(String cron) {
1583+
return DSL.cron(cron);
1584+
}
1585+
1586+
/**
1587+
* @see DSL#after(Consumer)
1588+
*/
1589+
public static Consumer<ScheduleBuilder> after(Consumer<TimeoutBuilder> duration) {
1590+
return DSL.after(duration);
1591+
}
1592+
1593+
/**
1594+
* @see DSL#after(String)
1595+
*/
1596+
public static Consumer<ScheduleBuilder> after(String durationExpression) {
1597+
return DSL.after(durationExpression);
1598+
}
1599+
1600+
/**
1601+
* @see DSL#on(Consumer)
1602+
*/
1603+
public static Consumer<ScheduleBuilder> on(
1604+
Consumer<AbstractEventConsumptionStrategyBuilder<?, ?, ?>> strategy) {
1605+
return DSL.on(strategy);
1606+
}
1607+
1608+
// ---- Schedule Event Strategies ----//
1609+
1610+
/**
1611+
* @see DSL#one(String)
1612+
*/
1613+
public static Consumer<AbstractEventConsumptionStrategyBuilder<?, ?, ?>> one(String eventType) {
1614+
return DSL.one(eventType);
1615+
}
1616+
1617+
/**
1618+
* @see DSL#one(Consumer)
1619+
*/
1620+
public static Consumer<AbstractEventConsumptionStrategyBuilder<?, ?, ?>> one(
1621+
Consumer<EventFilterBuilder> filter) {
1622+
return DSL.one(filter);
1623+
}
1624+
1625+
/**
1626+
* @see DSL#all(Consumer[])
1627+
*/
1628+
@SafeVarargs
1629+
public static Consumer<AbstractEventConsumptionStrategyBuilder<?, ?, ?>> all(
1630+
Consumer<EventFilterBuilder>... filters) {
1631+
return DSL.all(filters);
1632+
}
1633+
1634+
/**
1635+
* @see DSL#any(Consumer[])
1636+
*/
1637+
@SafeVarargs
1638+
public static Consumer<AbstractEventConsumptionStrategyBuilder<?, ?, ?>> any(
1639+
Consumer<EventFilterBuilder>... filters) {
1640+
return DSL.any(filters);
1641+
}
1642+
1643+
// ---------------------------------------------------------------------------
1644+
// Facades to base DSL (Use, Secrets, and Authentication)
1645+
// ---------------------------------------------------------------------------
1646+
1647+
/**
1648+
* @see DSL#secret(String)
1649+
*/
1650+
public static UseSpec secret(String secret) {
1651+
return DSL.secret(secret);
1652+
}
1653+
1654+
/**
1655+
* @see DSL#secrets(String...)
1656+
*/
1657+
public static UseSpec secrets(String... secret) {
1658+
return DSL.secrets(secret);
1659+
}
1660+
1661+
/**
1662+
* @see DSL#auth(String, AuthenticationConfigurer)
1663+
*/
1664+
public static UseSpec auth(String name, AuthenticationConfigurer auth) {
1665+
return DSL.auth(name, auth);
1666+
}
1667+
1668+
/**
1669+
* @see DSL#use()
1670+
*/
1671+
public static UseSpec use() {
1672+
return DSL.use();
1673+
}
1674+
1675+
/**
1676+
* @see DSL#use(String)
1677+
*/
1678+
public static AuthenticationConfigurer use(String authName) {
1679+
return DSL.use(authName);
1680+
}
1681+
1682+
/**
1683+
* @see DSL#basic(String, String)
1684+
*/
1685+
public static AuthenticationConfigurer basic(String username, String password) {
1686+
return DSL.basic(username, password);
1687+
}
1688+
1689+
/**
1690+
* @see DSL#basic(String)
1691+
*/
1692+
public static AuthenticationConfigurer basic(String secret) {
1693+
return DSL.basic(secret);
1694+
}
1695+
1696+
/**
1697+
* @see DSL#bearer(String)
1698+
*/
1699+
public static AuthenticationConfigurer bearer(String token) {
1700+
return DSL.bearer(token);
1701+
}
1702+
1703+
/**
1704+
* @see DSL#bearerUse(String)
1705+
*/
1706+
public static AuthenticationConfigurer bearerUse(String secret) {
1707+
return DSL.bearerUse(secret);
1708+
}
1709+
1710+
/**
1711+
* @see DSL#digest(String, String)
1712+
*/
1713+
public static AuthenticationConfigurer digest(String username, String password) {
1714+
return DSL.digest(username, password);
1715+
}
1716+
1717+
/**
1718+
* @see DSL#digest(String)
1719+
*/
1720+
public static AuthenticationConfigurer digest(String secret) {
1721+
return DSL.digest(secret);
1722+
}
1723+
1724+
/**
1725+
* @see DSL#oidc(String, OAuth2AuthenticationData.OAuth2AuthenticationDataGrant)
1726+
*/
1727+
public static AuthenticationConfigurer oidc(
1728+
String authority, OAuth2AuthenticationData.OAuth2AuthenticationDataGrant grant) {
1729+
return DSL.oidc(authority, grant);
1730+
}
1731+
1732+
/**
1733+
* @see DSL#oidc(String, OAuth2AuthenticationData.OAuth2AuthenticationDataGrant, String, String)
1734+
*/
1735+
public static AuthenticationConfigurer oidc(
1736+
String authority,
1737+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant grant,
1738+
String clientId,
1739+
String clientSecret) {
1740+
return DSL.oidc(authority, grant, clientId, clientSecret);
1741+
}
1742+
1743+
/**
1744+
* @see DSL#oidc(String)
1745+
*/
1746+
public static AuthenticationConfigurer oidc(String secret) {
1747+
return DSL.oidc(secret);
1748+
}
1749+
1750+
/**
1751+
* @see DSL#oauth2(String,
1752+
* io.serverlessworkflow.api.types.OAuth2AuthenticationData.OAuth2AuthenticationDataGrant)
1753+
*/
1754+
public static AuthenticationConfigurer oauth2(
1755+
String authority,
1756+
io.serverlessworkflow.api.types.OAuth2AuthenticationData.OAuth2AuthenticationDataGrant
1757+
grant) {
1758+
return DSL.oauth2(authority, grant);
1759+
}
1760+
1761+
/**
1762+
* @see DSL#oauth2(String,
1763+
* io.serverlessworkflow.api.types.OAuth2AuthenticationData.OAuth2AuthenticationDataGrant,
1764+
* String, String)
1765+
*/
1766+
public static AuthenticationConfigurer oauth2(
1767+
String authority,
1768+
io.serverlessworkflow.api.types.OAuth2AuthenticationData.OAuth2AuthenticationDataGrant grant,
1769+
String clientId,
1770+
String clientSecret) {
1771+
return DSL.oauth2(authority, grant, clientId, clientSecret);
1772+
}
1773+
1774+
/**
1775+
* @see DSL#oauth2(String)
1776+
*/
1777+
public static AuthenticationConfigurer oauth2(String secret) {
1778+
return DSL.oauth2(secret);
1779+
}
15331780
}

0 commit comments

Comments
 (0)