From 74a98111dc6514251893d1cf4c23cbec0b292bf2 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Tue, 12 Nov 2024 14:08:21 -0700 Subject: [PATCH] Added async operations workflow sample (#1394) Signed-off-by: Whit Waldo --- Directory.Packages.props | 5 +- all.sln | 10 +++- .../Activities/NotifyWarehouseActivity.cs | 33 ++++++++++++ .../Activities/ProcessPaymentActivity.cs | 33 ++++++++++++ .../Models/Transaction.cs | 19 +++++++ .../WorkflowAsyncOperations/Program.cs | 48 +++++++++++++++++ .../WorkflowAsyncOperations.csproj | 18 +++++++ .../Workflows/DemoWorkflow.cs | 52 +++++++++++++++++++ 8 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Program.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj create mode 100644 examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index e5e60cd7..772dd7c6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -29,10 +29,11 @@ - + + + - diff --git a/all.sln b/all.sln index 3b995990..34f0f6fd 100644 --- a/all.sln +++ b/all.sln @@ -119,11 +119,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging.Test", "test\Dapr.Messaging.Test\Dapr.Messaging.Test.csproj", "{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging", "src\Dapr.Messaging\Dapr.Messaging.csproj", "{0EAE36A1-B578-4F13-A113-7A477ECA1BDA}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamingSubscriptionExample", "examples\Client\PublishSubscribe\StreamingSubscriptionExample\StreamingSubscriptionExample.csproj", "{290D1278-F613-4DF3-9DF5-F37E38CDC363}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs", "src\Dapr.Jobs\Dapr.Jobs.csproj", "{C8BB6A85-A7EA-40C0-893D-F36F317829B3}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs.Test", "test\Dapr.Jobs.Test\Dapr.Jobs.Test.csproj", "{BF9828E9-5597-4D42-AA6E-6E6C12214204}" @@ -316,6 +319,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.Build.0 = Debug|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.ActiveCfg = Release|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.Build.0 = Release|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.Build.0 = Debug|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -327,7 +334,7 @@ Global {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.Build.0 = Debug|Any CPU {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.ActiveCfg = Release|Any CPU - {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CPU + {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CP {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -396,6 +403,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B} {0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {290D1278-F613-4DF3-9DF5-F37E38CDC363} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6} diff --git a/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs b/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs new file mode 100644 index 00000000..2088ff81 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs @@ -0,0 +1,33 @@ +// ------------------------------------------------------------------------ +// Copyright 2024 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +using Dapr.Workflow; +using WorkflowAsyncOperations.Models; + +namespace WorkflowAsyncOperations.Activities; + +internal sealed class NotifyWarehouseActivity : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, Transaction input) + { + //Contact the warehouse to ship the product + await Task.Delay(TimeSpan.FromSeconds(8)); + return null; + } +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs b/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs new file mode 100644 index 00000000..1cd65889 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs @@ -0,0 +1,33 @@ +// ------------------------------------------------------------------------ +// Copyright 2024 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +using Dapr.Workflow; +using WorkflowAsyncOperations.Models; + +namespace WorkflowAsyncOperations.Activities; + +internal sealed class ProcessPaymentActivity : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, Transaction input) + { + //Confirm payment with processor + await Task.Delay(TimeSpan.FromSeconds(10)); + return null; + } +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs b/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs new file mode 100644 index 00000000..082c9564 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs @@ -0,0 +1,19 @@ +// ------------------------------------------------------------------------ +// Copyright 2024 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +namespace WorkflowAsyncOperations.Models; + +internal sealed record Transaction(decimal Value) +{ + public Guid CustomerId { get; init; } = Guid.NewGuid(); +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Program.cs b/examples/Workflow/WorkflowAsyncOperations/Program.cs new file mode 100644 index 00000000..df46d1f9 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Program.cs @@ -0,0 +1,48 @@ +// ------------------------------------------------------------------------ +// Copyright 2024 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +using Dapr.Workflow; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using WorkflowAsyncOperations.Activities; +using WorkflowAsyncOperations.Models; +using WorkflowAsyncOperations.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +var host = await builder.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; +var transaction = new Transaction(16.58m); +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, transaction); + +//Poll for status updates every second +var status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +do +{ + Console.WriteLine($"Current status: {status.RuntimeStatus}, step: {status.ReadCustomStatusAs()}"); + status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +} while (!status.IsWorkflowCompleted); + +Console.WriteLine($"Workflow completed - {status.ReadCustomStatusAs()}"); diff --git a/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj b/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj new file mode 100644 index 00000000..a1350fa7 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs new file mode 100644 index 00000000..7d70ca63 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs @@ -0,0 +1,52 @@ +// ------------------------------------------------------------------------ +// Copyright 2024 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +using Dapr.Workflow; +using WorkflowAsyncOperations.Activities; +using WorkflowAsyncOperations.Models; + +namespace WorkflowAsyncOperations.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, Transaction input) + { + try + { + //Submit the transaction to the payment processor + context.SetCustomStatus("Processing payment..."); + await context.CallActivityAsync(nameof(ProcessPaymentActivity), input); + + + //Send the transaction details to the warehouse + context.SetCustomStatus("Contacting warehouse..."); + await context.CallActivityAsync(nameof(NotifyWarehouseActivity), input); + + context.SetCustomStatus("Success!"); + return true; + } + catch + { + //If anything goes wrong, return false + context.SetCustomStatus("Something went wrong"); + return false; + } + } +} +