Skip to content

Commit a972d2f

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Added workflow sample: Monitor (dapr#1388)
* Added workflow monitor Signed-off-by: Whit Waldo <[email protected]> * Restore to original argument names Signed-off-by: Whit Waldo <[email protected]> * Update to target .NET 6 Signed-off-by: Whit Waldo <[email protected]> * Added missing copyright headers Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]>
1 parent e04af4e commit a972d2f

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

all.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ 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}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}"
123+
EndProject
122124
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}"
123125
EndProject
124126
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}"
@@ -325,6 +327,10 @@ Global
325327
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
326328
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
327329
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
330+
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
331+
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
332+
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
333+
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.Build.0 = Release|Any CPU
328334
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
329335
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
330336
{945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -421,6 +427,7 @@ Global
421427
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
422428
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
423429
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
430+
{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
424431
{945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
425432
{FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
426433
{D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
16+
namespace WorkflowMonitor.Activities;
17+
18+
internal sealed class CheckStatus : WorkflowActivity<bool, string>
19+
{
20+
private static List<string> status = new List<string> { "healthy", "unhealthy" };
21+
private Random random = new();
22+
23+
/// <summary>
24+
/// Override to implement async (non-blocking) workflow activity logic.
25+
/// </summary>
26+
/// <param name="context">Provides access to additional context for the current activity execution.</param>
27+
/// <param name="input">The deserialized activity input.</param>
28+
/// <returns>The output of the activity as a task.</returns>
29+
public override Task<string> RunAsync(WorkflowActivityContext context, bool input) =>
30+
Task.FromResult<string>(status[random.Next(status.Count)]);
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 WorkflowMonitor.Activities;
18+
using WorkflowMonitor.Workflows;
19+
20+
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
21+
{
22+
services.AddDaprWorkflow(options =>
23+
{
24+
options.RegisterWorkflow<DemoWorkflow>();
25+
options.RegisterActivity<CheckStatus>();
26+
});
27+
});
28+
29+
using var host = builder.Build();
30+
await host.StartAsync();
31+
32+
await using var scope = host.Services.CreateAsyncScope();
33+
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>();
34+
35+
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}";
36+
var isHealthy = true;
37+
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, isHealthy);
38+
39+
//We don't want to block on workflow completion as this workflow will never complete
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+
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Hosting" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 WorkflowMonitor.Activities;
16+
17+
namespace WorkflowMonitor.Workflows;
18+
19+
internal sealed class DemoWorkflow : Workflow<bool, bool>
20+
{
21+
/// <summary>
22+
/// Override to implement workflow logic.
23+
/// </summary>
24+
/// <param name="context">The workflow context.</param>
25+
/// <param name="isHealthy">The deserialized workflow input.</param>
26+
/// <returns>The output of the workflow as a task.</returns>
27+
public override async Task<bool> RunAsync(WorkflowContext context, bool isHealthy)
28+
{
29+
string status = await context.CallActivityAsync<string>(nameof(CheckStatus), true);
30+
int next_sleep_interval;
31+
if (!context.IsReplaying)
32+
{
33+
Console.WriteLine($"This job is {status}");
34+
}
35+
36+
if (status == "healthy")
37+
{
38+
isHealthy = true;
39+
next_sleep_interval = 30;
40+
}
41+
else
42+
{
43+
if (isHealthy)
44+
{
45+
isHealthy = false;
46+
}
47+
Console.WriteLine("Status is unhealthy. Set check interval to 5s");
48+
next_sleep_interval = 5;
49+
}
50+
51+
await context.CreateTimer(TimeSpan.FromSeconds(next_sleep_interval));
52+
context.ContinueAsNew(isHealthy);
53+
54+
//This workflow will never complete
55+
return true;
56+
}
57+
}

0 commit comments

Comments
 (0)