-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added async workflow example + copyright headers
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
- Loading branch information
Showing
8 changed files
with
256 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" /> | ||
<PackageVersion Include="FluentAssertions" Version="5.9.0" /> | ||
<PackageVersion Include="GitHubActionsTestLogger" Version="1.1.2" /> | ||
<PackageVersion Include="Google.Api.CommonProtos" Version="2.2.0" /> | ||
<PackageVersion Include="Google.Protobuf" Version="3.28.2" /> | ||
<PackageVersion Include="Grpc.AspNetCore" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Core.Testing" Version="2.46.6" /> | ||
<PackageVersion Include="Grpc.Net.Client" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Tools" Version="2.67.0" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.35" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="6.0.35" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.DurableTask.Client.Grpc" Version="1.3.0" /> | ||
<PackageVersion Include="Microsoft.DurableTask.Worker.Grpc" Version="1.3.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | ||
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" /> | ||
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" /> | ||
<PackageVersion Include="MinVer" Version="2.3.0" /> | ||
<PackageVersion Include="Moq" Version="4.20.72" /> | ||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageVersion Include="protobuf-net.Grpc.AspNetCore" Version="1.2.2" /> | ||
<PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" /> | ||
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" /> | ||
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
<PackageVersion Include="System.Formats.Asn1" Version="6.0.1" /> | ||
<PackageVersion Include="System.Text.Json" Version="6.0.10" /> | ||
<PackageVersion Include="xunit" Version="2.9.2" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
</Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" /> | ||
<PackageVersion Include="FluentAssertions" Version="5.9.0" /> | ||
<PackageVersion Include="GitHubActionsTestLogger" Version="1.1.2" /> | ||
<PackageVersion Include="Google.Api.CommonProtos" Version="2.2.0" /> | ||
<PackageVersion Include="Google.Protobuf" Version="3.28.2" /> | ||
<PackageVersion Include="Grpc.AspNetCore" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Core.Testing" Version="2.46.6" /> | ||
<PackageVersion Include="Grpc.Net.Client" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.66.0" /> | ||
<PackageVersion Include="Grpc.Tools" Version="2.67.0" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.35" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="6.0.35" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" /> | ||
<PackageVersion Include="Microsoft.DurableTask.Client.Grpc" Version="1.3.0" /> | ||
<PackageVersion Include="Microsoft.DurableTask.Worker.Grpc" Version="1.3.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | ||
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="6.0.1" /> | ||
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" /> | ||
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" /> | ||
<PackageVersion Include="MinVer" Version="2.3.0" /> | ||
<PackageVersion Include="Moq" Version="4.20.72" /> | ||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageVersion Include="protobuf-net.Grpc.AspNetCore" Version="1.2.2" /> | ||
<PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" /> | ||
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" /> | ||
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
<PackageVersion Include="System.Formats.Asn1" Version="6.0.1" /> | ||
<PackageVersion Include="System.Text.Json" Version="6.0.10" /> | ||
<PackageVersion Include="xunit" Version="2.9.2" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
</Project> |
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
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowAsyncApi/Activities/NotifyWarehouseActivity.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,33 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 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 WorkflowAsyncApi.Models; | ||
|
||
namespace WorkflowAsyncApi.Activities; | ||
|
||
internal sealed class NotifyWarehouseActivity : WorkflowActivity<Transaction, 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 async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input) | ||
{ | ||
//Contact the warehouse to ship the product | ||
await Task.Delay(TimeSpan.FromSeconds(8)); | ||
return null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowAsyncApi/Activities/ProcessPaymentActivity.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,33 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 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 WorkflowAsyncApi.Models; | ||
|
||
namespace WorkflowAsyncApi.Activities; | ||
|
||
internal sealed class ProcessPaymentActivity : WorkflowActivity<Transaction, 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 async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input) | ||
{ | ||
//Confirm payment with processor | ||
await Task.Delay(TimeSpan.FromSeconds(10)); | ||
return 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,19 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 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. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace WorkflowAsyncApi.Models; | ||
|
||
internal sealed record Transaction(decimal Value) | ||
{ | ||
public Guid CustomerId { get; init; } = Guid.NewGuid(); | ||
} |
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,48 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 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 WorkflowAsyncApi.Activities; | ||
using WorkflowAsyncApi.Models; | ||
using WorkflowAsyncApi.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<ProcessPaymentActivity>(); | ||
options.RegisterActivity<NotifyWarehouseActivity>(); | ||
}); | ||
}); | ||
|
||
var host = await builder.StartAsync(); | ||
|
||
await using var scope = host.Services.CreateAsyncScope(); | ||
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>(); | ||
|
||
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
var transaction = new Transaction(16.58m); | ||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, transaction); | ||
|
||
//Poll for status updates every second | ||
var status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
do | ||
{ | ||
Console.WriteLine($"Current status: {status.RuntimeStatus}, step: {status.ReadCustomStatusAs<string>()}"); | ||
status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
} while (!status.IsWorkflowCompleted); | ||
|
||
Console.WriteLine($"Workflow completed - {status.ReadCustomStatusAs<string>()}"); |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowAsyncApi/WorkflowAsyncApi.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>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
examples/Workflow/WorkflowAsyncApi/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,51 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2021 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 WorkflowAsyncApi.Activities; | ||
using WorkflowAsyncApi.Models; | ||
|
||
namespace WorkflowAsyncApi.Workflows; | ||
|
||
internal sealed class DemoWorkflow : Workflow<Transaction, bool> | ||
{ | ||
/// <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<bool> RunAsync(WorkflowContext context, Transaction input) | ||
{ | ||
try | ||
{ | ||
//Submit the transaction to the payment processor | ||
context.SetCustomStatus("Processing payment..."); | ||
await context.CallActivityAsync(nameof(ProcessPaymentActivity), input); | ||
|
||
|
||
//Send the transaction details to the warehouse | ||
context.SetCustomStatus("Contacting warehouse..."); | ||
await context.CallActivityAsync(nameof(NotifyWarehouseActivity), input); | ||
|
||
context.SetCustomStatus("Success!"); | ||
return true; | ||
} | ||
catch | ||
{ | ||
//If anything goes wrong, return false | ||
context.SetCustomStatus("Something went wrong"); | ||
return false; | ||
} | ||
} | ||
} |