From 1578c53139f37d19f210e2602504e0e9b5b1eb6f Mon Sep 17 00:00:00 2001 From: MregXN Date: Fri, 8 Dec 2023 10:05:37 +0800 Subject: [PATCH 1/2] add fan-out-fan-in sample Signed-off-by: MregXN --- all.sln | 7 +++ .../Activities/NotifyActivity.cs | 13 +++++ .../Workflow/WorkflowFanOutFanIn/Program.cs | 54 +++++++++++++++++++ .../WorkflowFanOutFanIn.csproj | 14 +++++ .../Workflows/DemoWorkflow.cs | 21 ++++++++ 5 files changed, 109 insertions(+) create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Program.cs create mode 100644 examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index 47fc9098c..8eec31759 100644 --- a/all.sln +++ b/all.sln @@ -104,6 +104,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BulkPublishEventExample", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkflowUnitTest", "examples\Workflow\WorkflowUnitTest\WorkflowUnitTest.csproj", "{8CA09061-2BEF-4506-A763-07062D2BD6AC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{A33BADFD-3BCE-47A4-89A5-1F26CC801DFA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -248,6 +250,10 @@ Global {DDC41278-FB60-403A-B969-2AEBD7C2D83C}.Release|Any CPU.Build.0 = Release|Any CPU {8CA09061-2BEF-4506-A763-07062D2BD6AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8CA09061-2BEF-4506-A763-07062D2BD6AC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A33BADFD-3BCE-47A4-89A5-1F26CC801DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A33BADFD-3BCE-47A4-89A5-1F26CC801DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A33BADFD-3BCE-47A4-89A5-1F26CC801DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A33BADFD-3BCE-47A4-89A5-1F26CC801DFA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -293,6 +299,7 @@ Global {4A175C27-EAFE-47E7-90F6-873B37863656} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6} {DDC41278-FB60-403A-B969-2AEBD7C2D83C} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6} {8CA09061-2BEF-4506-A763-07062D2BD6AC} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} + {A33BADFD-3BCE-47A4-89A5-1F26CC801DFA} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs new file mode 100644 index 000000000..38a3f454b --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs @@ -0,0 +1,13 @@ +using Dapr.Workflow; + +namespace WorkflowFanOutFanIn.Activities +{ + public class NotifyActivity : WorkflowActivity + { + public override Task RunAsync(WorkflowActivityContext context, string message) + { + Console.WriteLine(message); + return Task.FromResult(null); + } + } +} diff --git a/examples/Workflow/WorkflowFanOutFanIn/Program.cs b/examples/Workflow/WorkflowFanOutFanIn/Program.cs new file mode 100644 index 000000000..ea198a552 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Program.cs @@ -0,0 +1,54 @@ +using Dapr.Client; +using Dapr.Workflow; +using WorkflowFanOutFanIn.Activities; +using WorkflowFanOutFanIn.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(); + options.RegisterActivity(); + }); +}); + +// 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)); +} + +using (daprClient) +{ + await daprClient.WaitForSidecarAsync(); + + string instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; + await daprClient.StartWorkflowAsync( + workflowComponent: DaprWorkflowComponent, + workflowName: nameof(DemoWorkflow), + instanceId: instanceId, + input: "test input"); + + + await daprClient.WaitForWorkflowCompletionAsync( + workflowComponent: DaprWorkflowComponent, + instanceId: instanceId); +} \ No newline at end of file diff --git a/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj b/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj new file mode 100644 index 000000000..9acf06b47 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj @@ -0,0 +1,14 @@ + + + + + + + + Exe + net6 + enable + 612,618 + + + diff --git a/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..0f22b293e --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs @@ -0,0 +1,21 @@ +using Dapr.Workflow; +using WorkflowFanOutFanIn.Activities; + +namespace WorkflowFanOutFanIn.Workflows +{ + public class DemoWorkflow : Workflow + { + public override async Task RunAsync(WorkflowContext context, string input) + { + Console.WriteLine("Workflow Started."); + + Task t1 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 1 ..."); + Task t2 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 2 ..."); + Task t3 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 3 ..."); + await Task.WhenAll(t1, t2, t3); + + Console.WriteLine("Workflow Completed."); + return "Workflow Completed."; + } + } +} From c08017e04342eae0561a7d5873fde4098849e482 Mon Sep 17 00:00:00 2001 From: MregXN Date: Sat, 9 Dec 2023 18:50:58 +0800 Subject: [PATCH 2/2] modify log Signed-off-by: MregXN --- examples/Workflow/WorkflowFanOutFanIn/Program.cs | 6 ++++++ .../Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/Workflow/WorkflowFanOutFanIn/Program.cs b/examples/Workflow/WorkflowFanOutFanIn/Program.cs index ea198a552..0b444bcdb 100644 --- a/examples/Workflow/WorkflowFanOutFanIn/Program.cs +++ b/examples/Workflow/WorkflowFanOutFanIn/Program.cs @@ -38,6 +38,7 @@ using (daprClient) { + Console.WriteLine($"Workflow Started."); await daprClient.WaitForSidecarAsync(); string instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; @@ -51,4 +52,9 @@ await daprClient.StartWorkflowAsync( await daprClient.WaitForWorkflowCompletionAsync( workflowComponent: DaprWorkflowComponent, instanceId: instanceId); + + GetWorkflowResponse state = await daprClient.GetWorkflowAsync( + instanceId: instanceId, + workflowComponent: DaprWorkflowComponent); + Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); } \ No newline at end of file diff --git a/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs index 0f22b293e..0138ffa41 100644 --- a/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs +++ b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs @@ -7,14 +7,11 @@ public class DemoWorkflow : Workflow { public override async Task RunAsync(WorkflowContext context, string input) { - Console.WriteLine("Workflow Started."); - Task t1 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 1 ..."); Task t2 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 2 ..."); Task t3 = context.CallActivityAsync(nameof(NotifyActivity), "calling task 3 ..."); await Task.WhenAll(t1, t2, t3); - Console.WriteLine("Workflow Completed."); return "Workflow Completed."; } }