Skip to content

Commit 180447c

Browse files
WhitWaldodivzi-p
authored andcommitted
Added async operations workflow sample (dapr#1394)
Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Divya Perumal <[email protected]>
1 parent ec505e2 commit 180447c

File tree

8 files changed

+215
-3
lines changed

8 files changed

+215
-3
lines changed

Directory.Packages.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
3030
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
3131
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
32-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
32+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
33+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
34+
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0" />
3335
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
3436
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
35-
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0" />
3637
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
3738
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
3839
<PackageVersion Include="MinVer" Version="2.3.0" />

all.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,14 @@ 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}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging.Test", "test\Dapr.Messaging.Test\Dapr.Messaging.Test.csproj", "{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging", "src\Dapr.Messaging\Dapr.Messaging.csproj", "{0EAE36A1-B578-4F13-A113-7A477ECA1BDA}"
125127
EndProject
126128
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamingSubscriptionExample", "examples\Client\PublishSubscribe\StreamingSubscriptionExample\StreamingSubscriptionExample.csproj", "{290D1278-F613-4DF3-9DF5-F37E38CDC363}"
129+
EndProject
127130
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs", "src\Dapr.Jobs\Dapr.Jobs.csproj", "{C8BB6A85-A7EA-40C0-893D-F36F317829B3}"
128131
EndProject
129132
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs.Test", "test\Dapr.Jobs.Test\Dapr.Jobs.Test.csproj", "{BF9828E9-5597-4D42-AA6E-6E6C12214204}"
@@ -316,6 +319,10 @@ Global
316319
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
317320
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
318321
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
322+
{00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
323+
{00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.Build.0 = Debug|Any CPU
324+
{00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.ActiveCfg = Release|Any CPU
325+
{00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.Build.0 = Release|Any CPU
319326
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
320327
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
321328
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -327,7 +334,7 @@ Global
327334
{290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
328335
{290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.Build.0 = Debug|Any CPU
329336
{290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.ActiveCfg = Release|Any CPU
330-
{290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CPU
337+
{290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CP
331338
{C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
332339
{C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
333340
{C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -396,6 +403,7 @@ Global
396403
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
397404
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
398405
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
406+
{00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
399407
{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B}
400408
{0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
401409
{290D1278-F613-4DF3-9DF5-F37E38CDC363} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6}
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+
using WorkflowAsyncOperations.Models;
16+
17+
namespace WorkflowAsyncOperations.Activities;
18+
19+
internal sealed class NotifyWarehouseActivity : WorkflowActivity<Transaction, object?>
20+
{
21+
/// <summary>
22+
/// Override to implement async (non-blocking) workflow activity logic.
23+
/// </summary>
24+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
25+
/// <param name="input">The deserialized activity input.</param>
26+
/// <returns>The output of the activity as a task.</returns>
27+
public override async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input)
28+
{
29+
//Contact the warehouse to ship the product
30+
await Task.Delay(TimeSpan.FromSeconds(8));
31+
return null;
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+
using WorkflowAsyncOperations.Models;
16+
17+
namespace WorkflowAsyncOperations.Activities;
18+
19+
internal sealed class ProcessPaymentActivity : WorkflowActivity<Transaction, object?>
20+
{
21+
/// <summary>
22+
/// Override to implement async (non-blocking) workflow activity logic.
23+
/// </summary>
24+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
25+
/// <param name="input">The deserialized activity input.</param>
26+
/// <returns>The output of the activity as a task.</returns>
27+
public override async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input)
28+
{
29+
//Confirm payment with processor
30+
await Task.Delay(TimeSpan.FromSeconds(10));
31+
return null;
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
namespace WorkflowAsyncOperations.Models;
15+
16+
internal sealed record Transaction(decimal Value)
17+
{
18+
public Guid CustomerId { get; init; } = Guid.NewGuid();
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 WorkflowAsyncOperations.Activities;
18+
using WorkflowAsyncOperations.Models;
19+
using WorkflowAsyncOperations.Workflows;
20+
21+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
22+
{
23+
services.AddDaprWorkflow(options =>
24+
{
25+
options.RegisterWorkflow<DemoWorkflow>();
26+
options.RegisterActivity<ProcessPaymentActivity>();
27+
options.RegisterActivity<NotifyWarehouseActivity>();
28+
});
29+
});
30+
31+
var host = await builder.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+
var transaction = new Transaction(16.58m);
38+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, transaction);
39+
40+
//Poll for status updates every second
41+
var status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
42+
do
43+
{
44+
Console.WriteLine($"Current status: {status.RuntimeStatus}, step: {status.ReadCustomStatusAs<string>()}");
45+
status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId);
46+
} while (!status.IsWorkflowCompleted);
47+
48+
Console.WriteLine($"Workflow completed - {status.ReadCustomStatusAs<string>()}");
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+
<PackageReference Include="Microsoft.Extensions.Hosting" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 WorkflowAsyncOperations.Activities;
16+
using WorkflowAsyncOperations.Models;
17+
18+
namespace WorkflowAsyncOperations.Workflows;
19+
20+
internal sealed class DemoWorkflow : Workflow<Transaction, bool>
21+
{
22+
/// <summary>
23+
/// Override to implement workflow logic.
24+
/// </summary>
25+
/// <param name="context">The workflow context.</param>
26+
/// <param name="input">The deserialized workflow input.</param>
27+
/// <returns>The output of the workflow as a task.</returns>
28+
public override async Task<bool> RunAsync(WorkflowContext context, Transaction input)
29+
{
30+
try
31+
{
32+
//Submit the transaction to the payment processor
33+
context.SetCustomStatus("Processing payment...");
34+
await context.CallActivityAsync(nameof(ProcessPaymentActivity), input);
35+
36+
37+
//Send the transaction details to the warehouse
38+
context.SetCustomStatus("Contacting warehouse...");
39+
await context.CallActivityAsync(nameof(NotifyWarehouseActivity), input);
40+
41+
context.SetCustomStatus("Success!");
42+
return true;
43+
}
44+
catch
45+
{
46+
//If anything goes wrong, return false
47+
context.SetCustomStatus("Something went wrong");
48+
return false;
49+
}
50+
}
51+
}
52+

0 commit comments

Comments
 (0)