Skip to content

Commit e04af4e

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Added workflow sample: Task chaining (dapr#1387)
* Added Workflow Task Chaining example to replace dapr#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]> Signed-off-by: Siri Varma Vegiraju <[email protected]>
1 parent 33d94a5 commit e04af4e

File tree

8 files changed

+219
-1
lines changed

8 files changed

+219
-1
lines changed

all.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com
119119
EndProject
120120
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}"
121121
EndProject
122+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}"
@@ -323,6 +325,10 @@ Global
323325
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
324326
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
325327
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
328+
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
329+
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
330+
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
331+
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.Build.0 = Release|Any CPU
326332
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
327333
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
328334
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -415,6 +421,7 @@ Global
415421
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
416422
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
417423
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
424+
{945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
418425
{FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
419426
{D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
420427
{00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2024 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using Dapr.Workflow;
15+
16+
namespace WorkflowTaskChaining.Activities;
17+
18+
internal sealed class Step1 : WorkflowActivity<int, int>
19+
{
20+
/// <summary>
21+
/// Override to implement async (non-blocking) workflow activity logic.
22+
/// </summary>
23+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
24+
/// <param name="input">The deserialized activity input.</param>
25+
/// <returns>The output of the activity as a task.</returns>
26+
public override Task<int> RunAsync(WorkflowActivityContext context, int input)
27+
{
28+
Console.WriteLine($@"Step 1: Received input: {input}.");
29+
return Task.FromResult(input + 1);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2024 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using Dapr.Workflow;
15+
16+
namespace WorkflowTaskChaining.Activities;
17+
18+
internal sealed class Step2 : WorkflowActivity<int, int>
19+
{
20+
/// <summary>
21+
/// Override to implement async (non-blocking) workflow activity logic.
22+
/// </summary>
23+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
24+
/// <param name="input">The deserialized activity input.</param>
25+
/// <returns>The output of the activity as a task.</returns>
26+
public override Task<int> RunAsync(WorkflowActivityContext context, int input)
27+
{
28+
Console.WriteLine($@"Step 2: Received input: {input}.");
29+
return Task.FromResult(input + 2);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2024 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using Dapr.Workflow;
15+
16+
namespace WorkflowTaskChaining.Activities;
17+
18+
internal sealed class Step3 : WorkflowActivity<int, int>
19+
{
20+
/// <summary>
21+
/// Override to implement async (non-blocking) workflow activity logic.
22+
/// </summary>
23+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
24+
/// <param name="input">The deserialized activity input.</param>
25+
/// <returns>The output of the activity as a task.</returns>
26+
public override Task<int> RunAsync(WorkflowActivityContext context, int input)
27+
{
28+
Console.WriteLine($@"Step 3: Received input: {input}.");
29+
return Task.FromResult(input ^ 2);
30+
}
31+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2024 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using Dapr.Workflow;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Hosting;
17+
using WorkflowTaskChaining.Activities;
18+
using WorkflowTaskChaining.Workflows;
19+
20+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
21+
{
22+
services.AddDaprWorkflow(options =>
23+
{
24+
options.RegisterWorkflow<DemoWorkflow>();
25+
options.RegisterActivity<Step1>();
26+
options.RegisterActivity<Step2>();
27+
options.RegisterActivity<Step3>();
28+
});
29+
});
30+
31+
// Start the app - this is the point where we connect to the Dapr sidecar to listen
32+
// for workflow work-items to execute
33+
using var host = builder.Build();
34+
await host.StartAsync();
35+
36+
await using var scope = host.Services.CreateAsyncScope();
37+
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>();
38+
39+
//Check health
40+
const int wfInput = 42;
41+
Console.WriteLine(@"Workflow Started");
42+
43+
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}";
44+
45+
//Start the workflow immediately
46+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, wfInput);
47+
48+
//Get the status of the workflow
49+
WorkflowState workflowState;
50+
while (true)
51+
{
52+
workflowState = await daprWorkflowClient.GetWorkflowStateAsync(instanceId, true);
53+
Console.WriteLine($@"Workflow status: {workflowState.RuntimeStatus}");
54+
if (workflowState.IsWorkflowCompleted)
55+
break;
56+
57+
await Task.Delay(TimeSpan.FromSeconds(1));
58+
}
59+
60+
//Display the result from the workflow
61+
var result = string.Join(" ", workflowState.ReadOutputAs<int[]>() ?? Array.Empty<int>());
62+
Console.WriteLine($@"Workflow result: {result}");
63+
64+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Hosting" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2024 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using Dapr.Workflow;
15+
using WorkflowTaskChaining.Activities;
16+
17+
namespace WorkflowTaskChaining.Workflows;
18+
19+
internal sealed class DemoWorkflow : Workflow<int, int[]>
20+
{
21+
/// <summary>
22+
/// Override to implement workflow logic.
23+
/// </summary>
24+
/// <param name="context">The workflow context.</param>
25+
/// <param name="input">The deserialized workflow input.</param>
26+
/// <returns>The output of the workflow as a task.</returns>
27+
public override async Task<int[]> RunAsync(WorkflowContext context, int input)
28+
{
29+
var result1 = await context.CallActivityAsync<int>(nameof(Step1), input);
30+
var result2 = await context.CallActivityAsync<int>(nameof(Step2), result1);
31+
var result3 = await context.CallActivityAsync<int>(nameof(Step3), result2);
32+
var ret = new int[] { result1, result2, result3 };
33+
34+
return ret;
35+
}
36+
}

test/Dapr.E2E.Test/DaprCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public DaprCommand(ITestOutputHelper output)
3838

3939
public void Run()
4040
{
41-
Console.WriteLine($"Running command: {this.Command}");
41+
Console.WriteLine($@"Running command: {this.Command}");
4242
var escapedArgs = Command.Replace("\"", "\\\"");
4343
var process = new Process()
4444
{

0 commit comments

Comments
 (0)