-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MregXN <mregxn@gmail.com>
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
13 changes: 13 additions & 0 deletions
13
examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs
This file contains 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,13 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowTaskChianing.Activities | ||
{ | ||
public class Step1 : WorkflowActivity<int, int> | ||
{ | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($"Step 1: Received input: {input}."); | ||
return Task.FromResult<int>(input + 1); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs
This file contains 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,13 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowTaskChianing.Activities | ||
{ | ||
public class Step2 : WorkflowActivity<int, int> | ||
{ | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($"Step 2: Received input: {input}."); | ||
return Task.FromResult<int>(input * 2); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs
This file contains 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,13 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowTaskChianing.Activities | ||
{ | ||
public class Step3 : WorkflowActivity<int, int> | ||
{ | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($"Step 3: Received input: {input}."); | ||
return Task.FromResult<int>(input ^ 2); | ||
} | ||
} | ||
} |
This file contains 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,67 @@ | ||
using Dapr.Client; | ||
using Dapr.Workflow; | ||
using WorkflowTaskChianing.Activities; | ||
using WorkflowTaskChianing.Workflows; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
const string DaprWorkflowComponent = "dapr"; | ||
|
||
// The workflow host is a background service that connects to the sidecar over gRPC | ||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<Step1>(); | ||
options.RegisterActivity<Step2>(); | ||
options.RegisterActivity<Step3>(); | ||
}); | ||
}); | ||
|
||
// Dapr uses a random port for gRPC by default. If we don't know what that port | ||
// is (because this app was started separate from dapr), then assume 4001. | ||
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DAPR_GRPC_PORT"))) | ||
{ | ||
Environment.SetEnvironmentVariable("DAPR_GRPC_PORT", "4001"); | ||
} | ||
|
||
// Start the app - this is the point where we connect to the Dapr sidecar to | ||
// listen for workflow work-items to execute. | ||
using var host = builder.Build(); | ||
host.Start(); | ||
|
||
|
||
DaprClient daprClient = new DaprClientBuilder().Build(); | ||
|
||
while (!await daprClient.CheckHealthAsync()) | ||
{ | ||
Thread.Sleep(TimeSpan.FromSeconds(5)); | ||
} | ||
|
||
int wfInput = 42; | ||
using (daprClient) | ||
{ | ||
Console.WriteLine($"Workflow Started."); | ||
await daprClient.WaitForSidecarAsync(); | ||
|
||
string instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
|
||
await daprClient.StartWorkflowAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
workflowName: nameof(DemoWorkflow), | ||
instanceId: instanceId, | ||
input: wfInput); | ||
|
||
|
||
await daprClient.WaitForWorkflowCompletionAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
instanceId: instanceId); | ||
|
||
GetWorkflowResponse state = await daprClient.GetWorkflowAsync( | ||
instanceId: instanceId, | ||
workflowComponent: DaprWorkflowComponent); | ||
Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); | ||
|
||
string result = string.Join(" ", state.ReadOutputAs<int[]>()); | ||
Console.WriteLine($"Workflow result: {result}"); | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/Workflow/WorkflowTaskChaining/WorkflowFanOutFanIn.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<NoWarn>612,618</NoWarn> | ||
</PropertyGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs
This file contains 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,18 @@ | ||
using Dapr.Workflow; | ||
using WorkflowTaskChianing.Activities; | ||
|
||
namespace WorkflowTaskChianing.Workflows | ||
{ | ||
public class DemoWorkflow : Workflow<int, int[]> | ||
{ | ||
public override async Task<int[]> RunAsync(WorkflowContext context, int input) | ||
{ | ||
int result1 = await context.CallActivityAsync<int>(nameof(Step1), input); | ||
int result2 = await context.CallActivityAsync<int>(nameof(Step2), result1); | ||
int result3 = await context.CallActivityAsync<int>(nameof(Step3), result2); | ||
int[] ret = new int[] { result1, result2, result3 }; | ||
|
||
return ret; | ||
} | ||
} | ||
} |