diff --git a/all.sln b/all.sln index 26262176..fe014052 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}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}" @@ -323,6 +325,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 + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.Build.0 = Release|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -415,6 +421,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} + {945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs new file mode 100644 index 00000000..a0e160b4 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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; + +namespace WorkflowTaskChaining.Activities; + +internal sealed class Step1 : 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 Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 1: Received input: {input}."); + return Task.FromResult(input + 1); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs new file mode 100644 index 00000000..59823993 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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; + +namespace WorkflowTaskChaining.Activities; + +internal sealed class Step2 : 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 Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 2: Received input: {input}."); + return Task.FromResult(input + 2); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs new file mode 100644 index 00000000..67a39001 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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; + +namespace WorkflowTaskChaining.Activities; + +internal sealed class Step3 : 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 Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 3: Received input: {input}."); + return Task.FromResult(input ^ 2); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Program.cs b/examples/Workflow/WorkflowTaskChaining/Program.cs new file mode 100644 index 00000000..126eff60 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Program.cs @@ -0,0 +1,64 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; +using WorkflowTaskChaining.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +// 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(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +//Check health +const int wfInput = 42; +Console.WriteLine(@"Workflow Started"); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; + +//Start the workflow immediately +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, wfInput); + +//Get the status of the workflow +WorkflowState workflowState; +while (true) +{ + workflowState = await daprWorkflowClient.GetWorkflowStateAsync(instanceId, true); + Console.WriteLine($@"Workflow status: {workflowState.RuntimeStatus}"); + if (workflowState.IsWorkflowCompleted) + break; + + await Task.Delay(TimeSpan.FromSeconds(1)); +} + +//Display the result from the workflow +var result = string.Join(" ", workflowState.ReadOutputAs() ?? Array.Empty()); +Console.WriteLine($@"Workflow result: {result}"); + + diff --git a/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj b/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj new file mode 100644 index 00000000..91ded8af --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs new file mode 100644 index 00000000..72211434 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs @@ -0,0 +1,36 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; + +namespace WorkflowTaskChaining.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, int input) + { + var result1 = await context.CallActivityAsync(nameof(Step1), input); + var result2 = await context.CallActivityAsync(nameof(Step2), result1); + var result3 = await context.CallActivityAsync(nameof(Step3), result2); + var ret = new int[] { result1, result2, result3 }; + + return ret; + } +} diff --git a/test/Dapr.E2E.Test/DaprCommand.cs b/test/Dapr.E2E.Test/DaprCommand.cs index 768e8196..a692ec63 100644 --- a/test/Dapr.E2E.Test/DaprCommand.cs +++ b/test/Dapr.E2E.Test/DaprCommand.cs @@ -38,7 +38,7 @@ namespace Dapr.E2E.Test public void Run() { - Console.WriteLine($"Running command: {this.Command}"); + Console.WriteLine($@"Running command: {this.Command}"); var escapedArgs = Command.Replace("\"", "\\\""); var process = new Process() {