-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MregXN <[email protected]>
- Loading branch information
Showing
5 changed files
with
131 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
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
using Dapr.Workflow; | ||
|
||
namespace WorkflowMonitor.Activities | ||
{ | ||
public class CheckStatus : WorkflowActivity<bool, string> | ||
{ | ||
static List<string> status = new List<string>() { "healthy", "unhealthy" }; | ||
Random random = new Random(); | ||
public override Task<string> RunAsync(WorkflowActivityContext context, bool input) | ||
{ | ||
return 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,57 @@ | ||
using Dapr.Client; | ||
using Dapr.Workflow; | ||
using WorkflowMonitor.Activities; | ||
using WorkflowMonitor.Workflows; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
const string DaprWorkflowComponent = "dapr"; | ||
|
||
// The workflow host is a background service that connects to the sidecar over gRPC | ||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<CheckStatus>(); | ||
}); | ||
}); | ||
|
||
// Dapr uses a random port for gRPC by default. If we don't know what that port | ||
// is (because this app was started separate from dapr), then assume 4001. | ||
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DAPR_GRPC_PORT"))) | ||
{ | ||
Environment.SetEnvironmentVariable("DAPR_GRPC_PORT", "4001"); | ||
} | ||
|
||
// Start the app - this is the point where we connect to the Dapr sidecar to | ||
// listen for workflow work-items to execute. | ||
using var host = builder.Build(); | ||
host.Start(); | ||
|
||
|
||
DaprClient daprClient = new DaprClientBuilder().Build(); | ||
|
||
while (!await daprClient.CheckHealthAsync()) | ||
{ | ||
Thread.Sleep(TimeSpan.FromSeconds(5)); | ||
} | ||
|
||
using (daprClient) | ||
{ | ||
Console.WriteLine($"Workflow Started."); | ||
await daprClient.WaitForSidecarAsync(); | ||
|
||
string instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
|
||
bool isHealthy = true; | ||
await daprClient.StartWorkflowAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
workflowName: nameof(DemoWorkflow), | ||
instanceId: instanceId, | ||
input: isHealthy); | ||
|
||
|
||
await daprClient.WaitForWorkflowCompletionAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
instanceId: instanceId); | ||
} |
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<NoWarn>612,618</NoWarn> | ||
</PropertyGroup> | ||
|
||
</Project> |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
using Dapr.Workflow; | ||
using WorkflowMonitor.Activities; | ||
|
||
namespace WorkflowMonitor.Workflows | ||
{ | ||
public class DemoWorkflow : Workflow<bool, bool> | ||
{ | ||
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 1s"); | ||
next_sleep_interval = 5; | ||
} | ||
|
||
await context.CreateTimer(TimeSpan.FromSeconds(next_sleep_interval)); | ||
context.ContinueAsNew(isHealthy); | ||
|
||
// This workflow will never complete. | ||
return true; | ||
} | ||
} | ||
} |