diff --git a/examples/Workflow/README.md b/examples/Workflow/README.md index 1af855767..728e3ada6 100644 --- a/examples/Workflow/README.md +++ b/examples/Workflow/README.md @@ -13,8 +13,7 @@ This Dapr workflow example shows how to create a Dapr workflow (`Workflow`) and ## Optional Setup Dapr workflow, as well as this example program, now support authentication through the use of API tokens. For more information on this, view the following document: [API Token](https://github.com/dapr/dotnet-sdk/blob/master/docs/api-tokens.md) -## Projects in sample - +## Console App This sample contains a single [WorkflowConsoleApp](./WorkflowConsoleApp) .NET project. It utilizes the workflow SDK as well as the workflow management API for simulating inventory management and sale of goods in a store. The main `Program.cs` file contains the main setup of the app, the registration of the workflow and its activities, and interaction with the user. The workflow definition is found in the `Workflows` directory and the workflow activity definitions are found in the `Activities` directory. @@ -32,8 +31,6 @@ This sample also contains a [WorkflowUnitTest](./WorkflowUnitTest) .NET project It works by creating an instance of the `OrderProcessingWorkflow` (defined in the `WorkflowConsoleApp` project), mocking activity calls, and testing the inputs and outputs. The tests also verify that outputs of the workflow. -## Running the console app example - To run the workflow web app locally, two separate terminal windows are required. In the first terminal window, from the `WorkflowConsoleApp` directory, run the following command to start the program itself: @@ -132,4 +129,150 @@ info: WorkflowConsoleApp.Activities.NotifyActivity[0] Order 1234 processed successfully! ``` -If you have Zipkin configured for Dapr locally on your machine, then you can view the workflow trace spans in the Zipkin web UI (typically at http://localhost:9411/zipkin/). \ No newline at end of file +If you have Zipkin configured for Dapr locally on your machine, then you can view the workflow trace spans in the Zipkin web UI (typically at http://localhost:9411/zipkin/). + +## Task Chaining +Details can be found in Dapr [Workflow Patterns Task Chaining](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-patterns/#task-chaining). + +To run this sample, in the first terminal window run the following command from the WorkflowTaskChaining directory +``` +dotnet tun +``` +Next, in a separate terminal window, start the dapr sidecar: +``` +dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500 +``` +The stdout of the workflow app should look like the following: +``` +Workflow Started. +Step 1: Received input: 42. +Step 2: Received input: 43. +Step 3: Received input: 86. +Workflow state: Completed +Workflow result: 43 86 84 +``` + +## Fan-Out/Fan-In +Details can be found in Dapr [Workflow Patterns Fan-Out/Fan-In](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-patterns/#fan-outfan-in). + +To run this sample, in the first terminal window run the following command from the WorkflowFanOutFanin directory +``` +dotnet tun +``` +Next, in a separate terminal window, start the dapr sidecar: +``` +dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500 +``` +The stdout of the workflow app should look like the following: +``` +Workflow Started. +calling task 3 ... +calling task 2 ... +calling task 1 ... +Workflow state: Completed +``` +The order of tasks log is uncertain because they are executed concurrently. + +## Monitor +Details can be found in Dapr [Workflow Patterns Monitor](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-patterns/#monitor). + +To run this sample, in the first terminal window run the following command from the WorkflowMonitor directory +``` +dotnet tun +``` +Next, in a separate terminal window, start the dapr sidecar: +``` +dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500 +``` +The stdout of the workflow app should look like the following: +``` +Workflow Started. +... +Status is unhealthy. Set check interval to 1s +This job is healthy +This job is unhealthy +Status is unhealthy. Set check interval to 1s +Status is unhealthy. Set check interval to 1s +... +``` + +## External System Interaction +Details can be found in Dapr [Workflow Patterns External System Interaction](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-patterns/#external-system-interaction). + +To run this sample, in the first terminal window run the following command from the WorkflowMonitor directory +``` +dotnet tun +``` +Next, in a separate terminal window, start the dapr sidecar: +``` +dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500 +``` +The stdout of the workflow app should look like the following: +``` +// If press enter +Workflow Started. +Press [ENTER] in 10s to approve this workflow. +Approved. +Workflow demo-workflow-994ed458 is approved. +Running Approval activity ... +Approve Activity finished +Workflow state: Completed + +// if do nothing +Workflow Started. +Press [ENTER] in 10s to approve this workflow. +Rejected. +Approval timeout. +Workflow demo-workflow-c9036657 is rejected. +Running Reject activity ... +Approval timeout. +Reject Activity finished +Workflow state: Completed +``` + +## Sub-Workflow +The sub-workflow pattern allows you to call a workflow from another workflow. +The `DemoWorkflow` class defines the workflow. It calls a sub-workflow `DemoSubWorkflow` to do the work. See the code snippet below: +```c# + public class DemoWorkflow : Workflow + { + public override async Task RunAsync(WorkflowContext context, string instanceId) + { + Console.WriteLine($"Workflow {instanceId} Started."); + string subInstanceId = instanceId + "-sub"; + ChildWorkflowTaskOptions options = new ChildWorkflowTaskOptions(subInstanceId); + await context.CallChildWorkflowAsync(nameof(DemoSubWorkflow), "Hello, sub-workflow", options); + return true; + } + } +``` + +The `DemoSubWorkflow` class defines the sub-workflow. It prints its instanceID and input, and then pauses for 5 seconds to simulate transaction processing. See the code snippet below: +```c# + public class DemoSubWorkflow : Workflow + { + public override async Task RunAsync(WorkflowContext context, string input) + { + Console.WriteLine($"Workflow {context.InstanceId} Started."); + Console.WriteLine($"Received input: {input}."); + await context.CreateTimer(TimeSpan.FromSeconds(5)); + return true; + } + } +``` +To run this sample, in the first terminal window run the following command from the WorkflowMonitor directory +``` +dotnet tun +``` +Next, in a separate terminal window, start the dapr sidecar: +``` +dapr run --app-id wfapp --dapr-grpc-port 4001 --dapr-http-port 3500 +``` +The stdout of the workflow app should look like the following: +``` +Workflow Started. +Workflow demo-workflow-ee513152 Started. +Workflow demo-workflow-ee513152-sub Started. +Received input: Hello, sub-workflow. +Workflow demo-workflow-ee513152 state: Completed +``` \ No newline at end of file