Skip to content

Commit 9819808

Browse files
authored
feat: add workflow schedules (#1908)
* feat: add workflow schedules Add the workflow schedule API (dapr/dapr#11095) to DaprWorkflowClient: createWorkflowSchedule (by workflow name or class), getWorkflowSchedule, listWorkflowSchedules (paged), deleteWorkflowSchedule and triggerWorkflowSchedule. WorkflowScheduleOptions covers input, due and expiration times, repeats, target app ID and namespace, overwrite and skip missed. Results are WorkflowSchedule, WorkflowScheduleStatus, WorkflowScheduleRun, WorkflowScheduleState and WorkflowSchedulePage. listInstanceIds takes an instance ID prefix, which lists a schedule's runs. DurableTaskClient and DurableTaskGrpcClient get the same calls. Pin durabletask-protobuf to v0.7.0 and download workflow_schedule.proto. Tests: unit tests for both clients, and WorkflowSchedulesIT (dapr-head) on the daprd, placement and scheduler edge images. Add a workflows/schedule example, validated with mechanical-markdown in a new validate.yml leg built from dapr master (continue-on-error). That leg needs Go 1.26. Requires Dapr 1.20 or later. Signed-off-by: joshvanl <me@joshvanl.dev> * Review comments Signed-off-by: joshvanl <me@joshvanl.dev> * Fix port conflict Signed-off-by: joshvanl <me@joshvanl.dev> --------- Signed-off-by: joshvanl <me@joshvanl.dev>
1 parent f8c4c28 commit 9819808

21 files changed

Lines changed: 1947 additions & 10 deletions

‎.github/workflows/validate.yml‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ jobs:
3131
fail-fast: false # Keep running if one leg fails.
3232
matrix:
3333
java: [ 17, 21, 25 ]
34+
dapr_ref: [ '' ]
35+
include:
36+
- java: 17
37+
dapr_ref: master
38+
# The dapr master leg tracks a moving target, so it must not block the release legs.
39+
continue-on-error: ${{ matrix.dapr_ref != '' }}
3440
env:
35-
GOVER: "1.20"
41+
GOVER: "1.26"
3642
GOOS: linux
3743
GOARCH: amd64
3844
GOPROXY: https://proxy.golang.org
@@ -41,7 +47,7 @@ jobs:
4147
DAPR_RUNTIME_VER: 1.19.1
4248
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.19.0-rc.1/install/install.sh
4349
DAPR_CLI_REF:
44-
DAPR_REF:
50+
DAPR_REF: ${{ matrix.dapr_ref }}
4551
steps:
4652
- uses: actions/checkout@v7
4753
- name: Set up OpenJDK ${{ env.JDK_VER }}
@@ -103,7 +109,13 @@ jobs:
103109
run: |
104110
docker stop dapr_placement
105111
cd dapr
106-
./dist/linux_amd64/release/placement &
112+
./dist/linux_amd64/release/placement --healthz-port 58080 --metrics-port 59090 &
113+
- name: Override scheduler service.
114+
if: env.DAPR_REF != ''
115+
run: |
116+
docker stop dapr_scheduler
117+
cd dapr
118+
./dist/linux_amd64/release/scheduler --etcd-data-dir /tmp/dapr-scheduler --override-broadcast-host-port=localhost:50006 --etcd-client-listen-address=0.0.0.0 --healthz-port 58081 --metrics-port 59091 &
107119
- name: Install utilities dependencies
108120
run: |
109121
echo "PATH=$PATH:$HOME/.local/bin" >> $GITHUB_ENV
@@ -143,3 +155,8 @@ jobs:
143155
(cd examples && mm.py ./src/main/java/io/dapr/examples/actors/README.md)
144156
(cd examples && mm.py ./src/main/java/io/dapr/examples/querystate/README.md)
145157
(cd examples && mm.py ./src/main/java/io/dapr/examples/pubsub/stream/README.md)
158+
- name: Validate examples that need dapr master
159+
if: env.DAPR_REF != ''
160+
run: |
161+
set -e
162+
(cd examples && mm.py ./src/main/java/io/dapr/examples/workflows/schedule/README.md)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2026 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.schedule;
15+
16+
import io.dapr.examples.workflows.utils.PropertyUtils;
17+
import io.dapr.workflows.client.DaprWorkflowClient;
18+
import io.dapr.workflows.client.WorkflowScheduleOptions;
19+
import io.dapr.workflows.client.WorkflowScheduleState;
20+
import io.dapr.workflows.client.WorkflowScheduleStatus;
21+
22+
public class DemoScheduleClient {
23+
private static final String SCHEDULE_NAME = "daily-report";
24+
25+
/**
26+
* The main method to start the client.
27+
*
28+
* @param args Input arguments (unused).
29+
* @throws InterruptedException if interrupted while waiting for the schedule to complete.
30+
*/
31+
public static void main(String[] args) throws InterruptedException {
32+
try (DaprWorkflowClient client = new DaprWorkflowClient(PropertyUtils.getProperties(args))) {
33+
client.createWorkflowSchedule(SCHEDULE_NAME, DemoScheduleWorkflow.class, "@every 5s",
34+
new WorkflowScheduleOptions().setInput("sales").setRepeats(3).setOverwrite(true));
35+
System.out.printf("Created schedule '%s'%n", SCHEDULE_NAME);
36+
37+
// A triggered run counts towards the repeats, so two scheduled runs follow it.
38+
client.triggerWorkflowSchedule(SCHEDULE_NAME);
39+
System.out.println("Triggered run");
40+
41+
WorkflowScheduleStatus status;
42+
do {
43+
Thread.sleep(2000);
44+
status = client.getWorkflowSchedule(SCHEDULE_NAME).getStatus();
45+
System.out.printf("Schedule state: %s runs=%d%n", status.getState(), status.getRunCount());
46+
} while (status.getState() == WorkflowScheduleState.ACTIVE);
47+
48+
// Every run is a workflow instance whose ID starts with the schedule name.
49+
int runs = client.listInstanceIds(null, null, SCHEDULE_NAME + "_").getInstanceIds().size();
50+
System.out.printf("Runs: %d%n", runs);
51+
52+
client.deleteWorkflowSchedule(SCHEDULE_NAME);
53+
System.out.printf("Deleted schedule '%s'%n", SCHEDULE_NAME);
54+
}
55+
}
56+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2026 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.schedule;
15+
16+
import io.dapr.examples.workflows.utils.PropertyUtils;
17+
import io.dapr.workflows.runtime.WorkflowRuntime;
18+
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
19+
20+
public class DemoScheduleWorker {
21+
/**
22+
* The main method of this app.
23+
*
24+
* @param args The port the app will listen on.
25+
* @throws Exception An Exception.
26+
*/
27+
public static void main(String[] args) throws Exception {
28+
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder(PropertyUtils.getProperties(args))
29+
.registerWorkflow(DemoScheduleWorkflow.class);
30+
31+
WorkflowRuntime runtime = builder.build();
32+
System.out.println("Start workflow runtime");
33+
runtime.start();
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2026 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.schedule;
15+
16+
import io.dapr.workflows.Workflow;
17+
import io.dapr.workflows.WorkflowStub;
18+
19+
public class DemoScheduleWorkflow implements Workflow {
20+
@Override
21+
public WorkflowStub create() {
22+
return ctx -> ctx.complete("Report '" + ctx.getInput(String.class) + "' by " + ctx.getInstanceId());
23+
}
24+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
## Workflow Schedules
2+
3+
A workflow schedule starts a new instance of a workflow repeatedly, on a cron expression or an
4+
`@every <duration>` interval. This example creates a schedule, triggers a run by hand, watches the
5+
schedule until its repeats are used up, counts the runs and deletes the schedule.
6+
7+
### Using the Workflow Schedules API
8+
9+
The `DaprWorkflowClient` exposes these methods:
10+
* `client.createWorkflowSchedule(...)` to create or overwrite a schedule.
11+
* `client.getWorkflowSchedule(...)` to read a schedule with its status and last run.
12+
* `client.listWorkflowSchedules(...)` to page through the app's schedules.
13+
* `client.triggerWorkflowSchedule(...)` to start a run now.
14+
* `client.deleteWorkflowSchedule(...)` to delete a schedule.
15+
* `client.listInstanceIds(null, null, "<name>_")` to list the runs of a schedule.
16+
17+
## Pre-requisites
18+
19+
* Dapr 1.20 or later (dapr master until 1.20.0-rc.1 is released).
20+
* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/).
21+
* Java JDK 17 (or greater):
22+
* [Microsoft JDK 17](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-17)
23+
* [Oracle JDK 17](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK17)
24+
* [OpenJDK 17](https://jdk.java.net/17/)
25+
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.
26+
27+
### Checking out the code
28+
29+
Clone this repository:
30+
31+
```sh
32+
git clone https://github.com/dapr/java-sdk.git
33+
cd java-sdk
34+
```
35+
36+
Then build the Maven project:
37+
38+
```sh
39+
# make sure you are in the `java-sdk` directory.
40+
mvn install
41+
```
42+
43+
Then get into the examples directory:
44+
45+
```sh
46+
cd examples
47+
```
48+
49+
### Initialize Dapr
50+
51+
Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized.
52+
53+
### Running the example
54+
55+
<!-- STEP
56+
name: Run Workflow Schedule Worker
57+
match_order: none
58+
output_match_mode: substring
59+
expected_stdout_lines:
60+
- "Start workflow runtime"
61+
background: true
62+
sleep: 20
63+
timeout_seconds: 45
64+
-->
65+
66+
Run the worker:
67+
68+
```sh
69+
dapr run --app-id scheduleworker --resources-path ./components/workflows --dapr-grpc-port 50007 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.schedule.DemoScheduleWorker 50007
70+
```
71+
72+
<!-- END_STEP -->
73+
74+
<!-- STEP
75+
name: Run Workflow Schedule Client
76+
match_order: none
77+
output_match_mode: substring
78+
expected_stdout_lines:
79+
- "Created schedule 'daily-report'"
80+
- "Triggered run"
81+
- "Schedule state: COMPLETED"
82+
- "Runs:"
83+
- "Deleted schedule 'daily-report'"
84+
timeout_seconds: 90
85+
-->
86+
87+
In a separate terminal, run the client. It connects to the worker's sidecar on
88+
gRPC port 50007, so every run of the schedule executes on the worker:
89+
90+
```sh
91+
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.schedule.DemoScheduleClient 50007
92+
dapr stop --app-id scheduleworker
93+
```
94+
95+
<!-- END_STEP -->
96+
97+
The client creates `daily-report` with three repeats, triggers one run by hand and then polls the
98+
schedule every two seconds. The triggered run counts towards the repeats, so two scheduled runs
99+
follow it and the schedule completes. The output shows the schedule state and run count on each
100+
poll, the number of run instances and the deletion.

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<grpc.version>1.84.0</grpc.version>
1818
<protobuf.version>3.25.5</protobuf.version>
1919
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.19.0/dapr/proto</dapr.proto.baseurl>
20-
<durabletask.proto.baseurl>https://raw.githubusercontent.com/dapr/durabletask-protobuf/v0.6.0/protos</durabletask.proto.baseurl>
20+
<durabletask.proto.baseurl>https://raw.githubusercontent.com/dapr/durabletask-protobuf/v0.7.0/protos</durabletask.proto.baseurl>
2121
<dapr.sdk.version>1.20.0-SNAPSHOT</dapr.sdk.version>
2222
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
2323
<maven-dependency-plugin.version>3.8.1</maven-dependency-plugin.version>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2026 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package io.dapr.it.testcontainers.workflows;
15+
16+
import io.dapr.workflows.Workflow;
17+
import io.dapr.workflows.WorkflowStub;
18+
19+
public class ScheduleTestWorkflow implements Workflow {
20+
21+
@Override
22+
public WorkflowStub create() {
23+
return ctx -> ctx.complete(ctx.getInput(String.class));
24+
}
25+
}

0 commit comments

Comments
 (0)