Skip to content

Commit c0a5a35

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Added workflow example: Fan out/fan in (dapr#1396)
* Added workflow fan out/fan in example Signed-off-by: Whit Waldo <[email protected]> * Added 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 3cbc50f commit c0a5a35

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
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}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging.Test", "test\Dapr.Messaging.Test\Dapr.Messaging.Test.csproj", "{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}"
@@ -319,6 +321,10 @@ Global
319321
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
320322
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
321323
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
324+
{D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
325+
{D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.Build.0 = Debug|Any CPU
326+
{D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.ActiveCfg = Release|Any CPU
327+
{D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.Build.0 = Release|Any CPU
322328
{00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
323329
{00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.Build.0 = Debug|Any CPU
324330
{00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -403,6 +409,7 @@ Global
403409
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
404410
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
405411
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
412+
{D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
406413
{00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
407414
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B}
408415
{0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
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 WorkflowFanOutFanIn.Activities;
17+
18+
internal sealed class NotifyActivity : WorkflowActivity<string, object?>
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<object?> RunAsync(WorkflowActivityContext context, string input)
27+
{
28+
Console.WriteLine(input);
29+
return Task.FromResult<object?>(null);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 WorkflowFanOutFanIn.Activities;
18+
using WorkflowFanOutFanIn.Workflows;
19+
20+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
21+
{
22+
services.AddDaprWorkflow(options =>
23+
{
24+
options.RegisterWorkflow<DemoWorkflow>();
25+
options.RegisterActivity<NotifyActivity>();
26+
});
27+
});
28+
29+
var host = builder.Build();
30+
await host.StartAsync();
31+
32+
await using var scope = host.Services.CreateAsyncScope();
33+
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>();
34+
35+
var instanceId = $"workflow-demo-{Guid.NewGuid().ToString()[..8]}";
36+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, "test input");
37+
38+
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId);
39+
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
40+
Console.WriteLine($"Workflow state: {state.RuntimeStatus}");
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>net8.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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 WorkflowFanOutFanIn.Activities;
16+
17+
namespace WorkflowFanOutFanIn.Workflows;
18+
19+
public sealed class DemoWorkflow : Workflow<string, string>
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<string> RunAsync(WorkflowContext context, string input)
28+
{
29+
var tasks = new List<Task>();
30+
for (var a = 1; a <= 3; a++)
31+
{
32+
var task = context.CallActivityAsync(nameof(NotifyActivity), $"calling task {a}");
33+
tasks.Add(task);
34+
}
35+
36+
await Task.WhenAll(tasks);
37+
38+
return "Workflow completed";
39+
}
40+
}

0 commit comments

Comments
 (0)