-
Notifications
You must be signed in to change notification settings - Fork 217
Compensation example for Workflows #1333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cicoyle
merged 31 commits into
dapr:master
from
cicoyle:feat-wf-compensating-activities
May 29, 2025
+584
−4
Merged
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
de243ff
add basic compensation example for wf
cicoyle 1d74fbc
update commands to run + wf id
cicoyle 312c99c
update readme + add mechanical markdown
cicoyle 8f648b2
Merge branch 'master' into feat-wf-compensating-activities
cicoyle 417a0af
fix import
cicoyle 3f8cab0
fix mechanical markdown + add how to test it locally
cicoyle 457dd76
move compensation example readme to workflows readme
cicoyle 7fbc50e
Merge branch 'master' into feat-wf-compensating-activities
cicoyle b5829bf
Update BookCarActivity.java
artur-ciocanu 94ed1a9
Update BookFlightActivity.java
artur-ciocanu 5eba626
Update BookHotelActivity.java
artur-ciocanu 5f227ad
Update BookTripClient.java
artur-ciocanu 2e4448b
Update BookTripWorker.java
artur-ciocanu 4beb458
Update BookTripWorkflow.java
artur-ciocanu e272c14
Update CancelCarActivity.java
artur-ciocanu cd80b1a
Update CancelFlightActivity.java
artur-ciocanu 5dbf3dc
Update CancelHotelActivity.java
artur-ciocanu 828f5eb
Merge branch 'master' into feat-wf-compensating-activities
artur-ciocanu c6b507f
Merge branch 'master' into feat-wf-compensating-activities
artur-ciocanu 2b0c5a0
Merge branch 'master' into feat-wf-compensating-activities
artur-ciocanu 7ecf00e
Merge branch 'master' into feat-wf-compensating-activities
cicoyle 6d49de0
Merge branch 'master' into feat-wf-compensating-activities
artur-ciocanu 732d4f3
add retry IT tests and catch TaskFailedException
cicoyle 8a27e79
add test for no compensation if successful and assert attempts
cicoyle 8fc18f9
update mechanical markdown
cicoyle 063e9ea
Merge branch 'master' into feat-wf-compensating-activities
cicoyle 958de58
Merge branch 'master' into feat-wf-compensating-activities
cicoyle 2528a21
add back pubsub... but this should be removed long term
cicoyle da76513
try adding waitforsidecar
cicoyle e9455ac
rm tests from examples pr
cicoyle 13a0891
reset unintended changes
cicoyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
examples/src/main/java/io/dapr/examples/workflows/compensation/BookCarActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2025 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.workflows.compensation; | ||
|
||
import io.dapr.workflows.WorkflowActivity; | ||
import io.dapr.workflows.WorkflowActivityContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class BookCarActivity implements WorkflowActivity { | ||
private static final Logger logger = LoggerFactory.getLogger(BookCarActivity.class); | ||
|
||
@Override | ||
public String run(WorkflowActivityContext ctx) { | ||
logger.info("Starting Activity: " + ctx.getName()); | ||
|
||
// Simulate work | ||
try { | ||
TimeUnit.SECONDS.sleep(2); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
logger.info("Forcing Failure to trigger compensation for activity: " + ctx.getName()); | ||
|
||
// force the compensation | ||
throw new RuntimeException("Failed to book car"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/src/main/java/io/dapr/examples/workflows/compensation/BookFlightActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2025 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.workflows.compensation; | ||
|
||
import io.dapr.workflows.WorkflowActivity; | ||
import io.dapr.workflows.WorkflowActivityContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class BookFlightActivity implements WorkflowActivity { | ||
private static final Logger logger = LoggerFactory.getLogger(BookFlightActivity.class); | ||
|
||
@Override | ||
public String run(WorkflowActivityContext ctx) { | ||
logger.info("Starting Activity: " + ctx.getName()); | ||
|
||
// Simulate work | ||
try { | ||
TimeUnit.SECONDS.sleep(2); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
String result = "Flight booked successfully"; | ||
logger.info("Activity completed with result: " + result); | ||
return result; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
examples/src/main/java/io/dapr/examples/workflows/compensation/BookHotelActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2025 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.workflows.compensation; | ||
|
||
import io.dapr.workflows.WorkflowActivity; | ||
import io.dapr.workflows.WorkflowActivityContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class BookHotelActivity implements WorkflowActivity { | ||
private static final Logger logger = LoggerFactory.getLogger(BookHotelActivity.class); | ||
|
||
@Override | ||
public String run(WorkflowActivityContext ctx) { | ||
logger.info("Starting Activity: " + ctx.getName()); | ||
logger.info("Simulating hotel booking process..."); | ||
|
||
// Simulate some work | ||
try { | ||
Thread.sleep(2000); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
String result = "Hotel booked successfully"; | ||
logger.info("Activity completed with result: " + result); | ||
return result; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
examples/src/main/java/io/dapr/examples/workflows/compensation/BookTripClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2025 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.workflows.compensation; | ||
|
||
import io.dapr.workflows.client.DaprWorkflowClient; | ||
import io.dapr.workflows.client.WorkflowInstanceStatus; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class BookTripClient { | ||
public static void main(String[] args) { | ||
try (DaprWorkflowClient client = new DaprWorkflowClient()) { | ||
String instanceId = client.scheduleNewWorkflow(BookTripWorkflow.class); | ||
System.out.printf("Started a new trip booking workflow with instance ID: %s%n", instanceId); | ||
|
||
WorkflowInstanceStatus status = client.waitForInstanceCompletion(instanceId, Duration.ofMinutes(30), true); | ||
System.out.printf("Workflow instance with ID: %s completed with status: %s%n", instanceId, status); | ||
System.out.printf("Workflow output: %s%n", status.getSerializedOutput()); | ||
} catch (TimeoutException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/src/main/java/io/dapr/examples/workflows/compensation/BookTripWorker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2025 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.workflows.compensation; | ||
|
||
import io.dapr.workflows.runtime.WorkflowRuntime; | ||
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder; | ||
|
||
public class BookTripWorker { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Register the Workflow with the builder | ||
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder() | ||
.registerWorkflow(BookTripWorkflow.class) | ||
.registerActivity(BookFlightActivity.class) | ||
.registerActivity(CancelFlightActivity.class) | ||
.registerActivity(BookHotelActivity.class) | ||
.registerActivity(CancelHotelActivity.class) | ||
.registerActivity(BookCarActivity.class) | ||
.registerActivity(CancelCarActivity.class); | ||
|
||
// Build and start the workflow runtime | ||
try (WorkflowRuntime runtime = builder.build()) { | ||
System.out.println("Start workflow runtime"); | ||
runtime.start(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.