-
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 workflow example demonstrating external interaction
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
- Loading branch information
Showing
7 changed files
with
162 additions
and
0 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
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
20 changes: 20 additions & 0 deletions
20
examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.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,20 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowExternalInteraction.Activities; | ||
|
||
internal sealed class ApproveActivity : WorkflowActivity<string, bool> | ||
{ | ||
/// <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<bool> RunAsync(WorkflowActivityContext context, string input) | ||
{ | ||
Console.WriteLine($"Workflow {input} is approved"); | ||
Console.WriteLine("Running Approval activity..."); | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
return true; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.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,20 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowExternalInteraction.Activities; | ||
|
||
internal sealed class RejectActivity : WorkflowActivity<string, bool> | ||
{ | ||
/// <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<bool> RunAsync(WorkflowActivityContext context, string input) | ||
{ | ||
Console.WriteLine($"Workflow {input} is rejected"); | ||
Console.WriteLine("Running Reject activity..."); | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
return true; | ||
} | ||
} |
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,63 @@ | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using WorkflowExternalInteraction.Activities; | ||
using WorkflowExternalInteraction.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<ApproveActivity>(); | ||
options.RegisterActivity<RejectActivity>(); | ||
}); | ||
}); | ||
|
||
using var host = builder.Build(); | ||
await host.StartAsync(); | ||
|
||
await using var scope = host.Services.CreateAsyncScope(); | ||
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>(); | ||
|
||
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
|
||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId); | ||
|
||
|
||
bool enterPressed = false; | ||
Console.WriteLine("Press [ENTER] within the next 10 seconds to approve this workflow"); | ||
using (var cts = new CancellationTokenSource()) | ||
{ | ||
var inputTask = Task.Run(() => | ||
{ | ||
if (Console.ReadKey().Key == ConsoleKey.Enter) | ||
{ | ||
Console.WriteLine("Approved"); | ||
enterPressed = true; | ||
cts.Cancel(); //Cancel the delay task if Enter is pressed | ||
} | ||
}); | ||
|
||
try | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(10), cts.Token); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Task was cancelled because Enter was pressed | ||
} | ||
} | ||
|
||
if (enterPressed) | ||
{ | ||
await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Rejected"); | ||
} | ||
|
||
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); | ||
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.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> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowExternalInteraction/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,33 @@ | ||
using Dapr.Workflow; | ||
using WorkflowExternalInteraction.Activities; | ||
|
||
namespace WorkflowExternalInteraction.Workflows; | ||
|
||
internal sealed class DemoWorkflow : Workflow<string, 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, string input) | ||
{ | ||
try | ||
{ | ||
await context.WaitForExternalEventAsync<bool>(eventName: "Approval", timeout: TimeSpan.FromSeconds(10)); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
Console.WriteLine("Approval timeout"); | ||
await context.CallActivityAsync(nameof(RejectActivity), input); | ||
Console.WriteLine("Reject Activity finished"); | ||
return false; | ||
} | ||
|
||
await context.CallActivityAsync(nameof(ApproveActivity), input); | ||
Console.WriteLine("Approve Activity finished"); | ||
|
||
return true; | ||
} | ||
} |