Skip to content

Commit c0def6c

Browse files
WhitWaldodivzi-p
authored andcommitted
Added workflow sample: Sub-workflows (dapr#1395)
* Added Workflow with sub-workflow Signed-off-by: Whit Waldo <[email protected]> * Removed duplicate package version reference Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Divya Perumal <[email protected]>
1 parent d71f398 commit c0def6c

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-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}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}"
@@ -321,6 +323,10 @@ Global
321323
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
322324
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
323325
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
326+
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
327+
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
328+
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
329+
{FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.Build.0 = Release|Any CPU
324330
{D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
325331
{D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.Build.0 = Debug|Any CPU
326332
{D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -409,6 +415,7 @@ Global
409415
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
410416
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
411417
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
418+
{FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
412419
{D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
413420
{00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
414421
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 WorkflowSubWorkflow.Workflows;
18+
19+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
20+
{
21+
services.AddDaprWorkflow(options =>
22+
{
23+
options.RegisterWorkflow<DemoWorkflow>();
24+
options.RegisterWorkflow<DemoSubWorkflow>();
25+
});
26+
});
27+
28+
using var host = builder.Build();
29+
await host.StartAsync();
30+
31+
await using var scope = host.Services.CreateAsyncScope();
32+
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>();
33+
34+
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}";
35+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId );
36+
37+
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId);
38+
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
39+
Console.WriteLine($"Workflow {instanceId}, state: {state.RuntimeStatus}");
40+
41+
state = await daprWorkflowClient.GetWorkflowStateAsync($"{instanceId}-sub");
42+
Console.WriteLine($"Workflow {instanceId}-sub, 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 WorkflowSubWorkflow.Workflows;
17+
18+
internal sealed class DemoSubWorkflow : Workflow<string, bool>
19+
{
20+
/// <summary>
21+
/// Override to implement workflow logic.
22+
/// </summary>
23+
/// <param name="context">The workflow context.</param>
24+
/// <param name="instanceId">The deserialized workflow input.</param>
25+
/// <returns>The output of the workflow as a task.</returns>
26+
public override async Task<bool> RunAsync(WorkflowContext context, string instanceId)
27+
{
28+
Console.WriteLine($"Workflow {context.InstanceId} started");
29+
Console.WriteLine($"Received input: {instanceId}");
30+
await context.CreateTimer(TimeSpan.FromSeconds(5));
31+
return true;
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 WorkflowSubWorkflow.Workflows;
17+
18+
internal sealed class DemoWorkflow : Workflow<string, bool>
19+
{
20+
/// <summary>
21+
/// Override to implement workflow logic.
22+
/// </summary>
23+
/// <param name="context">The workflow context.</param>
24+
/// <param name="instanceId">The deserialized workflow input.</param>
25+
/// <returns>The output of the workflow as a task.</returns>
26+
public override async Task<bool> RunAsync(WorkflowContext context, string instanceId)
27+
{
28+
Console.WriteLine($"Workflow {instanceId} started");
29+
var subInstanceId = instanceId + "-sub";
30+
var options = new ChildWorkflowTaskOptions(subInstanceId);
31+
await context.CallChildWorkflowAsync<bool>(nameof(DemoSubWorkflow), "Hello, sub-workflow", options);
32+
return true;
33+
}
34+
}

0 commit comments

Comments
 (0)