forked from dapr/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added workflow sample: Monitor (dapr#1388)
* Added workflow monitor Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Restore to original argument names Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Update to target .NET 6 Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added missing copyright headers Signed-off-by: Whit Waldo <whit.waldo@innovian.net> --------- Signed-off-by: Whit Waldo <whit.waldo@innovian.net> Signed-off-by: Divya Perumal <divzi.perumal@gmail.com>
- Loading branch information
Showing
5 changed files
with
152 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
31 changes: 31 additions & 0 deletions
31
examples/Workflow/WorkflowMonitor/Activities/CheckStatus.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,31 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowMonitor.Activities; | ||
|
||
internal sealed class CheckStatus : WorkflowActivity<bool, string> | ||
{ | ||
private static List<string> status = new List<string> { "healthy", "unhealthy" }; | ||
private Random random = new(); | ||
|
||
/// <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 Task<string> RunAsync(WorkflowActivityContext context, bool input) => | ||
Task.FromResult<string>(status[random.Next(status.Count)]); | ||
} |
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,39 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowMonitor.Activities; | ||
using WorkflowMonitor.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<CheckStatus>(); | ||
}); | ||
}); | ||
|
||
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]}"; | ||
var isHealthy = true; | ||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, isHealthy); | ||
|
||
//We don't want to block on workflow completion as this workflow will never complete |
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> |
57 changes: 57 additions & 0 deletions
57
examples/Workflow/WorkflowMonitor/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,57 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowMonitor.Activities; | ||
|
||
namespace WorkflowMonitor.Workflows; | ||
|
||
internal sealed class DemoWorkflow : Workflow<bool, bool> | ||
{ | ||
/// <summary> | ||
/// Override to implement workflow logic. | ||
/// </summary> | ||
/// <param name="context">The workflow context.</param> | ||
/// <param name="isHealthy">The deserialized workflow input.</param> | ||
/// <returns>The output of the workflow as a task.</returns> | ||
public override async Task<bool> RunAsync(WorkflowContext context, bool isHealthy) | ||
{ | ||
string status = await context.CallActivityAsync<string>(nameof(CheckStatus), true); | ||
int next_sleep_interval; | ||
if (!context.IsReplaying) | ||
{ | ||
Console.WriteLine($"This job is {status}"); | ||
} | ||
|
||
if (status == "healthy") | ||
{ | ||
isHealthy = true; | ||
next_sleep_interval = 30; | ||
} | ||
else | ||
{ | ||
if (isHealthy) | ||
{ | ||
isHealthy = false; | ||
} | ||
Console.WriteLine("Status is unhealthy. Set check interval to 5s"); | ||
next_sleep_interval = 5; | ||
} | ||
|
||
await context.CreateTimer(TimeSpan.FromSeconds(next_sleep_interval)); | ||
context.ContinueAsNew(isHealthy); | ||
|
||
//This workflow will never complete | ||
return true; | ||
} | ||
} |