-
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.
Merge branch 'master' into workflow-example-sub
Signed-off-by: Whit Waldo <[email protected]>
- Loading branch information
Showing
7 changed files
with
211 additions
and
1 deletion.
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
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowAsyncOperations/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 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 WorkflowAsyncOperations.Models; | ||
|
||
namespace WorkflowAsyncOperations.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/WorkflowAsyncOperations/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 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 WorkflowAsyncOperations.Models; | ||
|
||
namespace WorkflowAsyncOperations.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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/Workflow/WorkflowAsyncOperations/Models/Transaction.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,19 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace WorkflowAsyncOperations.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 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 WorkflowAsyncOperations.Activities; | ||
using WorkflowAsyncOperations.Models; | ||
using WorkflowAsyncOperations.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/WorkflowAsyncOperations/WorkflowAsyncOperations.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> |
52 changes: 52 additions & 0 deletions
52
examples/Workflow/WorkflowAsyncOperations/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,52 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowAsyncOperations.Activities; | ||
using WorkflowAsyncOperations.Models; | ||
|
||
namespace WorkflowAsyncOperations.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; | ||
} | ||
} | ||
} | ||
|