Skip to content

Commit f9c9e23

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Added workflow example: External interaction (dapr#1389)
* Added workflow example demonstrating external interaction Signed-off-by: Whit Waldo <[email protected]> * Added copyright headers Signed-off-by: Whit Waldo <[email protected]> * Fixed .sln file Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]>
1 parent a972d2f commit f9c9e23

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-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}") = "WorkflowExternalInteraction", "examples\Workflow\WorkflowExternalInteraction\WorkflowExternalInteraction.csproj", "{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}"
@@ -327,6 +329,10 @@ Global
327329
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
328330
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
329331
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
332+
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
333+
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
334+
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
335+
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.Build.0 = Release|Any CPU
330336
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
331337
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
332338
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -427,6 +433,7 @@ Global
427433
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
428434
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
429435
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
436+
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
430437
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
431438
{945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
432439
{FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
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 WorkflowExternalInteraction.Activities;
17+
18+
internal sealed class ApproveActivity : WorkflowActivity<string, bool>
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 async Task<bool> RunAsync(WorkflowActivityContext context, string input)
27+
{
28+
Console.WriteLine($"Workflow {input} is approved");
29+
Console.WriteLine("Running Approval activity...");
30+
await Task.Delay(TimeSpan.FromSeconds(5));
31+
return true;
32+
}
33+
}
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 WorkflowExternalInteraction.Activities;
17+
18+
internal sealed class RejectActivity : WorkflowActivity<string, bool>
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 async Task<bool> RunAsync(WorkflowActivityContext context, string input)
27+
{
28+
Console.WriteLine($"Workflow {input} is rejected");
29+
Console.WriteLine("Running Reject activity...");
30+
await Task.Delay(TimeSpan.FromSeconds(5));
31+
return true;
32+
}
33+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 WorkflowExternalInteraction.Activities;
18+
using WorkflowExternalInteraction.Workflows;
19+
20+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
21+
{
22+
services.AddDaprWorkflow(options =>
23+
{
24+
options.RegisterWorkflow<DemoWorkflow>();
25+
options.RegisterActivity<ApproveActivity>();
26+
options.RegisterActivity<RejectActivity>();
27+
});
28+
});
29+
30+
using var host = builder.Build();
31+
await host.StartAsync();
32+
33+
await using var scope = host.Services.CreateAsyncScope();
34+
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>();
35+
36+
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}";
37+
38+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId);
39+
40+
41+
bool enterPressed = false;
42+
Console.WriteLine("Press [ENTER] within the next 10 seconds to approve this workflow");
43+
using (var cts = new CancellationTokenSource())
44+
{
45+
var inputTask = Task.Run(() =>
46+
{
47+
if (Console.ReadKey().Key == ConsoleKey.Enter)
48+
{
49+
Console.WriteLine("Approved");
50+
enterPressed = true;
51+
cts.Cancel(); //Cancel the delay task if Enter is pressed
52+
}
53+
});
54+
55+
try
56+
{
57+
await Task.Delay(TimeSpan.FromSeconds(10), cts.Token);
58+
}
59+
catch (TaskCanceledException)
60+
{
61+
// Task was cancelled because Enter was pressed
62+
}
63+
}
64+
65+
if (enterPressed)
66+
{
67+
await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true);
68+
}
69+
else
70+
{
71+
Console.WriteLine("Rejected");
72+
}
73+
74+
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId);
75+
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
76+
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>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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 WorkflowExternalInteraction.Activities;
16+
17+
namespace WorkflowExternalInteraction.Workflows;
18+
19+
internal sealed class DemoWorkflow : Workflow<string, bool>
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<bool> RunAsync(WorkflowContext context, string input)
28+
{
29+
try
30+
{
31+
await context.WaitForExternalEventAsync<bool>(eventName: "Approval", timeout: TimeSpan.FromSeconds(10));
32+
}
33+
catch (TaskCanceledException)
34+
{
35+
Console.WriteLine("Approval timeout");
36+
await context.CallActivityAsync(nameof(RejectActivity), input);
37+
Console.WriteLine("Reject Activity finished");
38+
return false;
39+
}
40+
41+
await context.CallActivityAsync(nameof(ApproveActivity), input);
42+
Console.WriteLine("Approve Activity finished");
43+
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)