mirror of https://github.com/dapr/dotnet-sdk.git
Added workflow example: External interaction (#1389)
* Added workflow example demonstrating external interaction Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added copyright headers Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Fixed .sln file Signed-off-by: Whit Waldo <whit.waldo@innovian.net> --------- Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
This commit is contained in:
parent
57a656bd2b
commit
f769eb1205
7
all.sln
7
all.sln
|
@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowExternalInteraction", "examples\Workflow\WorkflowExternalInteraction\WorkflowExternalInteraction.csproj", "{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}"
|
||||
|
@ -327,6 +329,10 @@ Global
|
|||
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
@ -427,6 +433,7 @@ Global
|
|||
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
|
||||
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
|
||||
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
|
||||
{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
|
||||
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
|
||||
{945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
|
||||
{FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
|
||||
|
|
|
@ -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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
// ------------------------------------------------------------------------
|
||||
// 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 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}");
|
|
@ -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>
|
|
@ -0,0 +1,46 @@
|
|||
// ------------------------------------------------------------------------
|
||||
// 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 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue