From 45e63cf32c429ec3f077a650538e3e89b84f42e0 Mon Sep 17 00:00:00 2001 From: MregXN Date: Fri, 8 Dec 2023 10:05:37 +0800 Subject: [PATCH] add fan-out-fan-in sample Signed-off-by: MregXN --- .../Activities/NotifyActivity.cs | 22 +++++++++++++ .../Workflow/WorkflowFanOutFanIn/Program.cs | 32 +++++++++++++++++++ .../WorkflowFanOutFanIn.csproj | 14 ++++++++ .../Workflows/DemoWorkflow.cs | 28 ++++++++++++++++ 4 files changed, 96 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/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs new file mode 100644 index 000000000..62856b648 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs @@ -0,0 +1,22 @@ +using Dapr.Workflow; +using Microsoft.Extensions.Logging; + +namespace WorkflowFanOutFanIn.Activities +{ + public class NotifyActivity : WorkflowActivity + { + readonly ILogger logger; + + public NotifyActivity(ILoggerFactory loggerFactory) + { + this.logger = loggerFactory.CreateLogger(); + } + + public override Task RunAsync(WorkflowActivityContext context, string message) + { + this.logger.LogInformation(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..277ef382c --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Program.cs @@ -0,0 +1,32 @@ +using Dapr.Client; +using Dapr.Workflow; +using WorkflowFanOutFanIn.Activities; +using WorkflowFanOutFanIn.Workflows; + +const string DaprWorkflowComponent = "dapr"; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + }); +}); + + +DaprClient daprClient = new DaprClientBuilder().Build(); + +// Wait for the sidecar to become available +while (!await daprClient.CheckHealthAsync()) +{ + Thread.Sleep(TimeSpan.FromSeconds(5)); +} + +using (daprClient) +{ + await daprClient.StartWorkflowAsync( + workflowComponent: DaprWorkflowComponent, + workflowName: nameof(WorkflowFanOutFanIn)); + +} \ 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..e82c2c7fd --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs @@ -0,0 +1,28 @@ +using Dapr.Workflow; +using WorkflowFanOutFanIn.Activities; + +namespace WorkflowFanOutFanIn.Workflows +{ + public class DemoWorkflow : Workflow + { + readonly WorkflowTaskOptions defaultActivityRetryOptions = new WorkflowTaskOptions + { + // NOTE: Beware that changing the number of retries is a breaking change for existing workflows. + RetryPolicy = new WorkflowRetryPolicy( + maxNumberOfAttempts: 3, + firstRetryInterval: TimeSpan.FromSeconds(5)), + }; + + public override async Task RunAsync(WorkflowContext context) + { + 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."; + } + } +}