-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added workflow example: Fan out/fan in (#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]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
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/WorkflowFanOutFanIn/Activities/NotifyActivity.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 WorkflowFanOutFanIn.Activities; | ||
|
||
internal sealed class NotifyActivity : WorkflowActivity<string, object?> | ||
{ | ||
/// <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<object?> RunAsync(WorkflowActivityContext context, string input) | ||
{ | ||
Console.WriteLine(input); | ||
return Task.FromResult<object?>(null); | ||
} | ||
} |
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,40 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowFanOutFanIn.Activities; | ||
using WorkflowFanOutFanIn.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<NotifyActivity>(); | ||
}); | ||
}); | ||
|
||
var host = builder.Build(); | ||
await host.StartAsync(); | ||
|
||
await using var scope = host.Services.CreateAsyncScope(); | ||
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>(); | ||
|
||
var instanceId = $"workflow-demo-{Guid.NewGuid().ToString()[..8]}"; | ||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, "test input"); | ||
|
||
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); | ||
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.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>net8.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> |
40 changes: 40 additions & 0 deletions
40
examples/Workflow/WorkflowFanOutFanIn/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,40 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowFanOutFanIn.Activities; | ||
|
||
namespace WorkflowFanOutFanIn.Workflows; | ||
|
||
public sealed class DemoWorkflow : Workflow<string, string> | ||
{ | ||
/// <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<string> RunAsync(WorkflowContext context, string input) | ||
{ | ||
var tasks = new List<Task>(); | ||
for (var a = 1; a <= 3; a++) | ||
{ | ||
var task = context.CallActivityAsync(nameof(NotifyActivity), $"calling task {a}"); | ||
tasks.Add(task); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
return "Workflow completed"; | ||
} | ||
} |