-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added workflow sample: Task chaining (#1387)
* Added Workflow Task Chaining example to replace #1206 Signed-off-by: Whit Waldo <[email protected]> * Targeting .NET 6, fixed transposition error Signed-off-by: Whit Waldo <[email protected]> * Added missing copyright headers Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]>
- Loading branch information
Showing
8 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int, int> | ||
{ | ||
/// <summary> | ||
/// Override to implement async (non-blocking) workflow activity logic. | ||
/// </summary> | ||
/// <param name="context">Provides access to additional context for the current activity execution.</param> | ||
/// <param name="input">The deserialized activity input.</param> | ||
/// <returns>The output of the activity as a task.</returns> | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($@"Step 1: Received input: {input}."); | ||
return Task.FromResult(input + 1); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int, int> | ||
{ | ||
/// <summary> | ||
/// Override to implement async (non-blocking) workflow activity logic. | ||
/// </summary> | ||
/// <param name="context">Provides access to additional context for the current activity execution.</param> | ||
/// <param name="input">The deserialized activity input.</param> | ||
/// <returns>The output of the activity as a task.</returns> | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($@"Step 2: Received input: {input}."); | ||
return Task.FromResult(input + 2); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int, int> | ||
{ | ||
/// <summary> | ||
/// Override to implement async (non-blocking) workflow activity logic. | ||
/// </summary> | ||
/// <param name="context">Provides access to additional context for the current activity execution.</param> | ||
/// <param name="input">The deserialized activity input.</param> | ||
/// <returns>The output of the activity as a task.</returns> | ||
public override Task<int> RunAsync(WorkflowActivityContext context, int input) | ||
{ | ||
Console.WriteLine($@"Step 3: Received input: {input}."); | ||
return Task.FromResult(input ^ 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DemoWorkflow>(); | ||
options.RegisterActivity<Step1>(); | ||
options.RegisterActivity<Step2>(); | ||
options.RegisterActivity<Step3>(); | ||
}); | ||
}); | ||
|
||
// 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<DaprWorkflowClient>(); | ||
|
||
//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<int[]>() ?? Array.Empty<int>()); | ||
Console.WriteLine($@"Workflow result: {result}"); | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int, int[]> | ||
{ | ||
/// <summary> | ||
/// Override to implement workflow logic. | ||
/// </summary> | ||
/// <param name="context">The workflow context.</param> | ||
/// <param name="input">The deserialized workflow input.</param> | ||
/// <returns>The output of the workflow as a task.</returns> | ||
public override async Task<int[]> RunAsync(WorkflowContext context, int input) | ||
{ | ||
var result1 = await context.CallActivityAsync<int>(nameof(Step1), input); | ||
var result2 = await context.CallActivityAsync<int>(nameof(Step2), result1); | ||
var result3 = await context.CallActivityAsync<int>(nameof(Step3), result2); | ||
var ret = new int[] { result1, result2, result3 }; | ||
|
||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters