From 650c34867d29b0a9c25a2daa488c92f4fcca3dc2 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 13:47:47 -0500 Subject: [PATCH 1/3] Added workflow example demonstrating external interaction Signed-off-by: Whit Waldo --- Directory.Packages.props | 1 + all.sln | 7 +++ .../Activities/ApproveActivity.cs | 20 ++++++ .../Activities/RejectActivity.cs | 20 ++++++ .../WorkflowExternalInteraction/Program.cs | 63 +++++++++++++++++++ .../WorkflowExternalInteraction.csproj | 18 ++++++ .../Workflows/DemoWorkflow.cs | 33 ++++++++++ 7 files changed, 162 insertions(+) create mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/Program.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj create mode 100644 examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 332939a5b..d659de938 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -27,6 +27,7 @@ + diff --git a/all.sln b/all.sln index 1dd0ab3c5..171507191 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ 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}") = "WorkflowExternalInteraction", "examples\Workflow\WorkflowExternalInteraction\WorkflowExternalInteraction.csproj", "{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -303,6 +305,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 + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -359,6 +365,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} + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs new file mode 100644 index 000000000..0a17f1302 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs @@ -0,0 +1,20 @@ +using Dapr.Workflow; + +namespace WorkflowExternalInteraction.Activities; + +internal sealed class ApproveActivity : 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, string input) + { + Console.WriteLine($"Workflow {input} is approved"); + Console.WriteLine("Running Approval activity..."); + await Task.Delay(TimeSpan.FromSeconds(5)); + return true; + } +} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs new file mode 100644 index 000000000..87656d9b2 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs @@ -0,0 +1,20 @@ +using Dapr.Workflow; + +namespace WorkflowExternalInteraction.Activities; + +internal sealed class RejectActivity : 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, string input) + { + Console.WriteLine($"Workflow {input} is rejected"); + Console.WriteLine("Running Reject activity..."); + await Task.Delay(TimeSpan.FromSeconds(5)); + return true; + } +} diff --git a/examples/Workflow/WorkflowExternalInteraction/Program.cs b/examples/Workflow/WorkflowExternalInteraction/Program.cs new file mode 100644 index 000000000..6e3c12f87 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Program.cs @@ -0,0 +1,63 @@ +using Dapr.Workflow; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using WorkflowExternalInteraction.Activities; +using WorkflowExternalInteraction.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; + +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId); + + +bool enterPressed = false; +Console.WriteLine("Press [ENTER] within the next 10 seconds to approve this workflow"); +using (var cts = new CancellationTokenSource()) +{ + var inputTask = Task.Run(() => + { + if (Console.ReadKey().Key == ConsoleKey.Enter) + { + Console.WriteLine("Approved"); + enterPressed = true; + cts.Cancel(); //Cancel the delay task if Enter is pressed + } + }); + + try + { + await Task.Delay(TimeSpan.FromSeconds(10), cts.Token); + } + catch (TaskCanceledException) + { + // Task was cancelled because Enter was pressed + } +} + +if (enterPressed) +{ + await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true); +} +else +{ + Console.WriteLine("Rejected"); +} + +await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); +var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); diff --git a/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj b/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj new file mode 100644 index 000000000..4aae25c46 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..ff6b0eb53 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs @@ -0,0 +1,33 @@ +using Dapr.Workflow; +using WorkflowExternalInteraction.Activities; + +namespace WorkflowExternalInteraction.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, string input) + { + try + { + await context.WaitForExternalEventAsync(eventName: "Approval", timeout: TimeSpan.FromSeconds(10)); + } + catch (TaskCanceledException) + { + Console.WriteLine("Approval timeout"); + await context.CallActivityAsync(nameof(RejectActivity), input); + Console.WriteLine("Reject Activity finished"); + return false; + } + + await context.CallActivityAsync(nameof(ApproveActivity), input); + Console.WriteLine("Approve Activity finished"); + + return true; + } +} From 56170015a431fcec45c7b0151c3fc60845a5e960 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 13:50:25 -0500 Subject: [PATCH 2/3] Revert "Added workflow example demonstrating external interaction" This reverts commit 650c34867d29b0a9c25a2daa488c92f4fcca3dc2. --- Directory.Packages.props | 1 - all.sln | 7 --- .../Activities/ApproveActivity.cs | 20 ------ .../Activities/RejectActivity.cs | 20 ------ .../WorkflowExternalInteraction/Program.cs | 63 ------------------- .../WorkflowExternalInteraction.csproj | 18 ------ .../Workflows/DemoWorkflow.cs | 33 ---------- 7 files changed, 162 deletions(-) delete mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs delete mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs delete mode 100644 examples/Workflow/WorkflowExternalInteraction/Program.cs delete mode 100644 examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj delete mode 100644 examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index d659de938..332939a5b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -27,7 +27,6 @@ - diff --git a/all.sln b/all.sln index 171507191..1dd0ab3c5 100644 --- a/all.sln +++ b/all.sln @@ -119,8 +119,6 @@ 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}") = "WorkflowExternalInteraction", "examples\Workflow\WorkflowExternalInteraction\WorkflowExternalInteraction.csproj", "{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -305,10 +303,6 @@ 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 - {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -365,7 +359,6 @@ 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} - {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs deleted file mode 100644 index 0a17f1302..000000000 --- a/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Dapr.Workflow; - -namespace WorkflowExternalInteraction.Activities; - -internal sealed class ApproveActivity : 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, string input) - { - Console.WriteLine($"Workflow {input} is approved"); - Console.WriteLine("Running Approval activity..."); - await Task.Delay(TimeSpan.FromSeconds(5)); - return true; - } -} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs deleted file mode 100644 index 87656d9b2..000000000 --- a/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Dapr.Workflow; - -namespace WorkflowExternalInteraction.Activities; - -internal sealed class RejectActivity : 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, string input) - { - Console.WriteLine($"Workflow {input} is rejected"); - Console.WriteLine("Running Reject activity..."); - await Task.Delay(TimeSpan.FromSeconds(5)); - return true; - } -} diff --git a/examples/Workflow/WorkflowExternalInteraction/Program.cs b/examples/Workflow/WorkflowExternalInteraction/Program.cs deleted file mode 100644 index 6e3c12f87..000000000 --- a/examples/Workflow/WorkflowExternalInteraction/Program.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Dapr.Workflow; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using WorkflowExternalInteraction.Activities; -using WorkflowExternalInteraction.Workflows; - -var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => -{ - services.AddDaprWorkflow(options => - { - options.RegisterWorkflow(); - options.RegisterActivity(); - options.RegisterActivity(); - }); -}); - -using var host = builder.Build(); -await host.StartAsync(); - -await using var scope = host.Services.CreateAsyncScope(); -var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); - -var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; - -await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId); - - -bool enterPressed = false; -Console.WriteLine("Press [ENTER] within the next 10 seconds to approve this workflow"); -using (var cts = new CancellationTokenSource()) -{ - var inputTask = Task.Run(() => - { - if (Console.ReadKey().Key == ConsoleKey.Enter) - { - Console.WriteLine("Approved"); - enterPressed = true; - cts.Cancel(); //Cancel the delay task if Enter is pressed - } - }); - - try - { - await Task.Delay(TimeSpan.FromSeconds(10), cts.Token); - } - catch (TaskCanceledException) - { - // Task was cancelled because Enter was pressed - } -} - -if (enterPressed) -{ - await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true); -} -else -{ - Console.WriteLine("Rejected"); -} - -await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); -var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); -Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); diff --git a/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj b/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj deleted file mode 100644 index 4aae25c46..000000000 --- a/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - - - - - - - - - - diff --git a/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs deleted file mode 100644 index ff6b0eb53..000000000 --- a/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Dapr.Workflow; -using WorkflowExternalInteraction.Activities; - -namespace WorkflowExternalInteraction.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, string input) - { - try - { - await context.WaitForExternalEventAsync(eventName: "Approval", timeout: TimeSpan.FromSeconds(10)); - } - catch (TaskCanceledException) - { - Console.WriteLine("Approval timeout"); - await context.CallActivityAsync(nameof(RejectActivity), input); - Console.WriteLine("Reject Activity finished"); - return false; - } - - await context.CallActivityAsync(nameof(ApproveActivity), input); - Console.WriteLine("Approve Activity finished"); - - return true; - } -} From 38f9473f395cd142fe09c33576c3e6647f0d1d86 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 15:01:18 -0500 Subject: [PATCH 3/3] Added async workflow example + copyright headers Signed-off-by: Whit Waldo --- Directory.Packages.props | 93 ++++++++++--------- all.sln | 7 ++ .../Activities/NotifyWarehouseActivity.cs | 33 +++++++ .../Activities/ProcessPaymentActivity.cs | 33 +++++++ .../WorkflowAsyncApi/Models/Transaction.cs | 19 ++++ examples/Workflow/WorkflowAsyncApi/Program.cs | 48 ++++++++++ .../WorkflowAsyncApi/WorkflowAsyncApi.csproj | 18 ++++ .../Workflows/DemoWorkflow.cs | 51 ++++++++++ 8 files changed, 256 insertions(+), 46 deletions(-) create mode 100644 examples/Workflow/WorkflowAsyncApi/Activities/NotifyWarehouseActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncApi/Activities/ProcessPaymentActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncApi/Models/Transaction.cs create mode 100644 examples/Workflow/WorkflowAsyncApi/Program.cs create mode 100644 examples/Workflow/WorkflowAsyncApi/WorkflowAsyncApi.csproj create mode 100644 examples/Workflow/WorkflowAsyncApi/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 332939a5b..34cdb26ad 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,47 +1,48 @@ - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/all.sln b/all.sln index 1dd0ab3c5..a9ca01aed 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ 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}") = "WorkflowAsyncApi", "examples\Workflow\WorkflowAsyncApi\WorkflowAsyncApi.csproj", "{720B28A2-3790-4232-A0B8-754292CF721C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -303,6 +305,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 + {720B28A2-3790-4232-A0B8-754292CF721C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {720B28A2-3790-4232-A0B8-754292CF721C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {720B28A2-3790-4232-A0B8-754292CF721C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {720B28A2-3790-4232-A0B8-754292CF721C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -359,6 +365,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} + {720B28A2-3790-4232-A0B8-754292CF721C} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/examples/Workflow/WorkflowAsyncApi/Activities/NotifyWarehouseActivity.cs b/examples/Workflow/WorkflowAsyncApi/Activities/NotifyWarehouseActivity.cs new file mode 100644 index 000000000..74944296a --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/Activities/NotifyWarehouseActivity.cs @@ -0,0 +1,33 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 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 WorkflowAsyncApi.Models; + +namespace WorkflowAsyncApi.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/WorkflowAsyncApi/Activities/ProcessPaymentActivity.cs b/examples/Workflow/WorkflowAsyncApi/Activities/ProcessPaymentActivity.cs new file mode 100644 index 000000000..b3a85d614 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/Activities/ProcessPaymentActivity.cs @@ -0,0 +1,33 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 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 WorkflowAsyncApi.Models; + +namespace WorkflowAsyncApi.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/WorkflowAsyncApi/Models/Transaction.cs b/examples/Workflow/WorkflowAsyncApi/Models/Transaction.cs new file mode 100644 index 000000000..cd06598e2 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/Models/Transaction.cs @@ -0,0 +1,19 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 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 WorkflowAsyncApi.Models; + +internal sealed record Transaction(decimal Value) +{ + public Guid CustomerId { get; init; } = Guid.NewGuid(); +} diff --git a/examples/Workflow/WorkflowAsyncApi/Program.cs b/examples/Workflow/WorkflowAsyncApi/Program.cs new file mode 100644 index 000000000..c0f555b19 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/Program.cs @@ -0,0 +1,48 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 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 WorkflowAsyncApi.Activities; +using WorkflowAsyncApi.Models; +using WorkflowAsyncApi.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/WorkflowAsyncApi/WorkflowAsyncApi.csproj b/examples/Workflow/WorkflowAsyncApi/WorkflowAsyncApi.csproj new file mode 100644 index 000000000..a1350fa79 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/WorkflowAsyncApi.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowAsyncApi/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowAsyncApi/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..3a6555438 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncApi/Workflows/DemoWorkflow.cs @@ -0,0 +1,51 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 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 WorkflowAsyncApi.Activities; +using WorkflowAsyncApi.Models; + +namespace WorkflowAsyncApi.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; + } + } +}