From 98c878c2ef071c9ed110c0ae48a18e12fcbdf2d4 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 13:27:07 -0500 Subject: [PATCH 1/4] Added workflow monitor Signed-off-by: Whit Waldo --- Directory.Packages.props | 93 ++++++++++--------- all.sln | 7 ++ .../WorkflowMonitor/Activities/CheckStatus.cs | 18 ++++ examples/Workflow/WorkflowMonitor/Program.cs | 26 ++++++ .../WorkflowMonitor/WorkflowMonitor.csproj | 18 ++++ .../WorkflowMonitor/Workflows/DemoWorkflow.cs | 44 +++++++++ src/Dapr.Workflow/Workflow.cs | 4 +- 7 files changed, 162 insertions(+), 48 deletions(-) create mode 100644 examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs create mode 100644 examples/Workflow/WorkflowMonitor/Program.cs create mode 100644 examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj create mode 100644 examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 332939a5b..34cdb26ad 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,47 +1,48 @@ - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/all.sln b/all.sln index 1dd0ab3c5..8d93cd3fd 100644 --- a/all.sln +++ b/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}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -303,6 +305,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 + {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 + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -359,6 +365,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} + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs new file mode 100644 index 000000000..8f961f94e --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs @@ -0,0 +1,18 @@ +using Dapr.Workflow; + +namespace WorkflowMonitor.Activities; + +internal sealed class CheckStatus : WorkflowActivity +{ + private static List status = new List { "healthy", "unhealthy" }; + private Random random = new(); + + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, bool input) => + Task.FromResult(status[random.Next(status.Count)]); +} diff --git a/examples/Workflow/WorkflowMonitor/Program.cs b/examples/Workflow/WorkflowMonitor/Program.cs new file mode 100644 index 000000000..e8fe78a22 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Program.cs @@ -0,0 +1,26 @@ +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(); + options.RegisterActivity(); + }); +}); + +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +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 diff --git a/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj new file mode 100644 index 000000000..af3a1b2c8 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj @@ -0,0 +1,18 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..6aa8e8013 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs @@ -0,0 +1,44 @@ +using Dapr.Workflow; +using WorkflowMonitor.Activities; + +namespace WorkflowMonitor.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, bool isHealthy) + { + string status = await context.CallActivityAsync(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; + } +} diff --git a/src/Dapr.Workflow/Workflow.cs b/src/Dapr.Workflow/Workflow.cs index 6510414bf..070629ede 100644 --- a/src/Dapr.Workflow/Workflow.cs +++ b/src/Dapr.Workflow/Workflow.cs @@ -130,8 +130,8 @@ public abstract class Workflow : IWorkflow /// Override to implement workflow logic. /// /// The workflow context. - /// The deserialized workflow input. + /// The deserialized workflow input. /// The output of the workflow as a task. - public abstract Task RunAsync(WorkflowContext context, TInput input); + public abstract Task RunAsync(WorkflowContext context, TInput isHealthy); } } From 6ff2e4ae1f45043ef3b3426d84b2832b7749a2bd Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 13:29:17 -0500 Subject: [PATCH 2/4] Restore to original argument names Signed-off-by: Whit Waldo --- src/Dapr.Workflow/Workflow.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dapr.Workflow/Workflow.cs b/src/Dapr.Workflow/Workflow.cs index 070629ede..6510414bf 100644 --- a/src/Dapr.Workflow/Workflow.cs +++ b/src/Dapr.Workflow/Workflow.cs @@ -130,8 +130,8 @@ public abstract class Workflow : IWorkflow /// Override to implement workflow logic. /// /// The workflow context. - /// The deserialized workflow input. + /// The deserialized workflow input. /// The output of the workflow as a task. - public abstract Task RunAsync(WorkflowContext context, TInput isHealthy); + public abstract Task RunAsync(WorkflowContext context, TInput input); } } From 0dd3f2c4c9b27e0e600f45a3a083c34cee16cf28 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 13:51:02 -0500 Subject: [PATCH 3/4] Update to target .NET 6 Signed-off-by: Whit Waldo --- examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj index af3a1b2c8..91ded8afb 100644 --- a/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj +++ b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net6.0 enable enable From 0cf984c2216ec56933fb19a5138451fd9c2f01cc Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 30 Oct 2024 14:28:07 -0500 Subject: [PATCH 4/4] Added missing copyright headers Signed-off-by: Whit Waldo --- .../WorkflowMonitor/Activities/CheckStatus.cs | 15 ++++++++++++++- examples/Workflow/WorkflowMonitor/Program.cs | 15 ++++++++++++++- .../WorkflowMonitor/Workflows/DemoWorkflow.cs | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs index 8f961f94e..99e5ba2b1 100644 --- a/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs +++ b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs @@ -1,4 +1,17 @@ -using Dapr.Workflow; +// ------------------------------------------------------------------------ +// 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; diff --git a/examples/Workflow/WorkflowMonitor/Program.cs b/examples/Workflow/WorkflowMonitor/Program.cs index e8fe78a22..05697d480 100644 --- a/examples/Workflow/WorkflowMonitor/Program.cs +++ b/examples/Workflow/WorkflowMonitor/Program.cs @@ -1,4 +1,17 @@ -using Dapr.Workflow; +// ------------------------------------------------------------------------ +// 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; diff --git a/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs index 6aa8e8013..e456eea15 100644 --- a/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs +++ b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs @@ -1,4 +1,17 @@ -using Dapr.Workflow; +// ------------------------------------------------------------------------ +// 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;