Skip to content

Commit

Permalink
Merge pull request #337 from temporalio/fix-replay-code-snippets
Browse files Browse the repository at this point in the history
Fix background check replay code snippets for Java and Python
  • Loading branch information
fairlydurable authored Nov 22, 2024
2 parents f14ea49 + 738a0d0 commit d67b649
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 31 deletions.
10 changes: 5 additions & 5 deletions docs/getting_started/dotnet/first_program_in_dotnet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class MoneyTransferWorkflow
InitialInterval = TimeSpan.FromSeconds(1),
MaximumInterval = TimeSpan.FromSeconds(100),
BackoffCoefficient = 2,
MaximumAttempts = 500,
MaximumAttempts = 3,
NonRetryableErrorTypes = new[] { "InvalidAccountException", "InsufficientFundsException" }
};

Expand All @@ -178,7 +178,7 @@ public class MoneyTransferWorkflow
// If everything succeeds, return transfer complete
return $"Transfer complete (transaction IDs: {withdrawResult}, {depositResult})";
}
catch (ApplicationFailureException depositEx)
catch (Exception depositEx)
{
try
{
Expand All @@ -188,9 +188,9 @@ public class MoneyTransferWorkflow
new ActivityOptions { StartToCloseTimeout = TimeSpan.FromMinutes(5), RetryPolicy = retryPolicy }
);
// If refund is successful, but deposit failed
throw new ApplicationFailureException($"Failed to deposit money into account {details.TargetAccount}. Money returned to {details.SourceAccount}. Cause: {depositEx.Message}", depositEx);
throw new ApplicationFailureException($"Failed to deposit money into account {details.TargetAccount}. Money returned to {details.SourceAccount}.", depositEx);
}
catch (ApplicationFailureException refundEx)
catch (Exception refundEx)
{
// If both deposit and refund fail
throw new ApplicationFailureException($"Failed to deposit money into account {details.TargetAccount}. Money could not be returned to {details.SourceAccount}. Cause: {refundEx.Message}", refundEx);
Expand Down Expand Up @@ -351,7 +351,7 @@ You'll see a **Retry Policy** defined that looks like this:
InitialInterval = TimeSpan.FromSeconds(1),
MaximumInterval = TimeSpan.FromSeconds(100),
BackoffCoefficient = 2,
MaximumAttempts = 500,
MaximumAttempts = 3,
NonRetryableErrorTypes = new[] { "InvalidAccountException", "InsufficientFundsException" }
```
<!--SNIPEND-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,4 @@ Refer to the [Temporal documentation](https://docs.temporal.io/) for more.

## Conclusion

In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
2 changes: 1 addition & 1 deletion docs/tutorials/infrastructure/envoy-sqlite-binary/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,4 @@ Refer to the [Temporal documentation](https://docs.temporal.io/) for more.

## Conclusion

In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
2 changes: 1 addition & 1 deletion docs/tutorials/infrastructure/nginx-sqlite-binary/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,4 @@ Refer to the [Temporal documentation](https://docs.temporal.io/) for more.

## Conclusion

In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
In this tutorial, you configured and deployed a baseline Temporal Service. Next, you can read about Temporal's [Visiblity features](https://docs.temporal.io/visibility) which require adding ElasticSearch to your deployment. You can also [learn more about the Temporal platform by following our self-paced online courses](https://learn.temporal.io/courses/), or talk to an expert about [Temporal Cloud](https://temporal.io/cloud).
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void testSuccessfulBackgroundCheckBoilerplateWithMocks(TestWorkflowEnviro
}

@Test
public void testSuccessfulReplayFromFile(TestWorkflowEnvironment testEnv, Worker worker,
BackgroundCheckReplayWorkflow workflow) throws Exception {
public void testSuccessfulReplayFromFile(BackgroundCheckReplayWorkflow workflow) throws Exception {

File eventHistoryFile = new File("backgroundcheck_workflow_event_history.json");

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/java/background-check/durable-execution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ Add the Replay test to the set of application tests.
```java
// ...
@Test
public void testSuccessfulReplayFromFile(TestWorkflowEnvironment testEnv, Worker worker,
BackgroundCheckReplayWorkflow workflow) throws Exception {
public void testSuccessfulReplayFromFile(BackgroundCheckReplayWorkflow workflow) throws Exception {

File eventHistoryFile = new File("backgroundcheck_workflow_event_history.json");

assertDoesNotThrow(() -> WorkflowReplayer.replayWorkflowExecution(eventHistoryFile,
BackgroundCheckReplayWorkflowImpl.class));

}
}
```
<!--SNIPEND-->

Expand Down
14 changes: 7 additions & 7 deletions docs/tutorials/java/background-check/project-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ In the Temporal Java SDK programming model, a [Workflow Definition](https://docs
#### Boilerplate Workflow Interface {#workflow-code}

<!--SNIPSTART java-project-setup-chapter-boilerplate-workflow-interface-->
[BackgroundCheckBoilerplateWorkflow.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflow.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflow.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflow.java)
```java
package backgroundcheckboilerplate;

Expand Down Expand Up @@ -395,7 +395,7 @@ There can only be one Workflow Method per Workflow Definition.
Now that you've defined your Workflow Interface you can define its implementation.

<!--SNIPSTART java-project-setup-chapter-boilerplate-workflow-implementation-->
[BackgroundCheckBoilerplateWorkflowImpl.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflowImpl.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflowImpl.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateWorkflowImpl.java)
```java
package backgroundcheckboilerplate;

Expand Down Expand Up @@ -461,7 +461,7 @@ In the Temporal Java SDK programming model, an Activity is defined as an interfa
The `BackgroundCheckActivity` interface below is an example of a the first part defining an Activity

<!--SNIPSTART java-project-setup-chapter-boilerplate-activities-interface-->
[BackgroundCheckBoilerplateActivities.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivities.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivities.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivities.java)
```java
package backgroundcheckboilerplate;

Expand Down Expand Up @@ -491,7 +491,7 @@ Activity. There can multiple Activity Methods per Activity Definition.
Now that you've defined your Activity Interface you can define its implementation.

<!--SNIPSTART java-project-setup-chapter-boilerplate-activity-implementation-->
[BackgroundCheckBoilerplateActivitiesImpl.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivitiesImpl.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivitiesImpl.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/BackgroundCheckBoilerplateActivitiesImpl.java)
```java
package backgroundcheckboilerplate;

Expand Down Expand Up @@ -551,7 +551,7 @@ To run a Worker Process with a local development server, define the following st
Temporal recommends keeping Worker code separate from Workflow and Activity code.

<!--SNIPSTART java-project-setup-chapter-boilerplate-dev-service-worker-->
[workers/DevServerWorker.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/DevServerWorker.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/DevServerWorker.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/DevServerWorker.java)
```java
package backgroundcheckboilerplate.workers;

Expand Down Expand Up @@ -599,7 +599,7 @@ A Temporal Cloud Worker requires that you specify the following in the Client co
- Certificate and private key associated with the Namespace

<!--SNIPSTART java-project-setup-chapter-boilerplate-cloud-worker-->
[workers/CloudWorker.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/CloudWorker.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/CloudWorker.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/CloudWorker.java)
```java
package backgroundcheckboilerplate.workers;

Expand Down Expand Up @@ -758,7 +758,7 @@ Set IP address and port in the Service Stubs Options and the Namespace in the
Temporal Client options.

<!--SNIPSTART java-project-setup-chapter-self-hosted-worker-->
[workers/SelfHostedWorker.java](code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/SelfHostedWorker.java)
[docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/SelfHostedWorker.java](https://github.com/temporalio/temporal-learning/blob/main/docs/tutorials/java/background-check/code/backgroundcheck/src/main/java/backgroundcheckboilerplate/workers/SelfHostedWorker.java)
```java
package backgroundcheckboilerplate.workers;

Expand Down
7 changes: 7 additions & 0 deletions docs/tutorials/java/build-an-email-drip-campaign/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import subscription.model.*;
import subscription.workflows.SendEmailWorkflow;
@RestController
public class Controller {
@Autowired
WorkflowClient client;
```
<!--SNIPEND-->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ async def test_execute_workflow():
# @@@SNIPSTART python-durability-chapter-replay-from-file-test
@pytest.mark.asyncio
async def test_replay_workflow_history_from_file():
async with await WorkflowEnvironment.start_time_skipping():
with open("tests/backgroundcheck_workflow_history.json", "r") as f:
history_json = json.load(f)
await Replayer(workflows=[BackgroundCheck]).replay_workflow(
WorkflowHistory.from_json("backgroundcheck_workflow", history_json)
)
with open("tests/backgroundcheck_workflow_history.json", "r") as f:
history_json = json.load(f)
await Replayer(workflows=[BackgroundCheck]).replay_workflow(
WorkflowHistory.from_json("backgroundcheck_workflow", history_json)
)
# @@@SNIPEND
11 changes: 5 additions & 6 deletions docs/tutorials/python/background-check/durable-execution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,11 @@ If the Workflow Definition and the Event History are incompatible, then the test
```py
@pytest.mark.asyncio
async def test_replay_workflow_history_from_file():
async with await WorkflowEnvironment.start_time_skipping():
with open("tests/backgroundcheck_workflow_history.json", "r") as f:
history_json = json.load(f)
await Replayer(workflows=[BackgroundCheck]).replay_workflow(
WorkflowHistory.from_json("backgroundcheck_workflow", history_json)
)
with open("tests/backgroundcheck_workflow_history.json", "r") as f:
history_json = json.load(f)
await Replayer(workflows=[BackgroundCheck]).replay_workflow(
WorkflowHistory.from_json("backgroundcheck_workflow", history_json)
)
```
<!--SNIPEND-->

Expand Down

0 comments on commit d67b649

Please sign in to comment.