From 6e1bbc5c6b2e709a48bda5104f6d361306a1727c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Poul=20Kjeldager=20S=C3=B8rensen?= Date: Thu, 2 Nov 2023 13:06:06 +0100 Subject: [PATCH] fix: added multiple trigger support for timed triggers --- .../ExpressionEngineExtensions.cs | 2 +- .../DependencyInjectionExtensions.cs | 2 +- .../WorkflowEngine.Hangfire.csproj | 9 ++-- .../WorkflowStarterBackgroundJob.cs | 50 +++++++++++-------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/WorkflowEngine.Core/ExpressionEngineExtensions.cs b/src/WorkflowEngine.Core/ExpressionEngineExtensions.cs index 5410c4c..8a62f0d 100644 --- a/src/WorkflowEngine.Core/ExpressionEngineExtensions.cs +++ b/src/WorkflowEngine.Core/ExpressionEngineExtensions.cs @@ -28,7 +28,7 @@ public static async ValueTask> ResolveInputs(this IEx { var resolvedInputs = new Dictionary(); - + if(actionMetadata.Inputs!=null) foreach (var input in actionMetadata.Inputs) { if (input.Value is string str && str.Contains("@")) diff --git a/src/WorkflowEngine.Hangfire/DependencyInjectionExtensions.cs b/src/WorkflowEngine.Hangfire/DependencyInjectionExtensions.cs index c941db5..15d5cb2 100644 --- a/src/WorkflowEngine.Hangfire/DependencyInjectionExtensions.cs +++ b/src/WorkflowEngine.Hangfire/DependencyInjectionExtensions.cs @@ -25,7 +25,7 @@ public static IServiceCollection AddWorkflowEngine(this ISer services.AddFunctions(); services.AddScoped(); - services.AddSingleton(); + services.AddTransient(); services.AddHostedService(); diff --git a/src/WorkflowEngine.Hangfire/WorkflowEngine.Hangfire.csproj b/src/WorkflowEngine.Hangfire/WorkflowEngine.Hangfire.csproj index 239f5ad..1e1d50b 100644 --- a/src/WorkflowEngine.Hangfire/WorkflowEngine.Hangfire.csproj +++ b/src/WorkflowEngine.Hangfire/WorkflowEngine.Hangfire.csproj @@ -21,11 +21,8 @@ - - - - - - + + + diff --git a/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs b/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs index 4002660..c134042 100644 --- a/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs +++ b/src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs @@ -32,28 +32,31 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) foreach (var workflow in await workflows.GetAllWorkflows()) { - var trigger = workflow.Manifest.Triggers.FirstOrDefault(t => t.Value.Type == "TimerTrigger"); - if (!trigger.Equals(default(KeyValuePair))) + foreach (var trigger in workflow.Manifest.Triggers.Where(t => t.Value.Type == "TimerTrigger")) { - - workflow.Manifest = null; - - jobs.AddOrUpdate(workflow.Id.ToString(), - (executor) => executor.TriggerAsync(new TriggerContext - { - Workflow = workflow, - Trigger = new Trigger + + if (!trigger.Equals(default(KeyValuePair))) + { + + workflow.Manifest = null; + + jobs.AddOrUpdate(workflow.Id.ToString() + trigger.Key, + (System.Linq.Expressions.Expression>)((executor) => executor.TriggerAsync(new TriggerContext { - Inputs = trigger.Value.Inputs, - ScheduledTime = DateTimeOffset.UtcNow, - Type = trigger.Value.Type, - Key = trigger.Key - }, - }), trigger.Value.Inputs["cronExpression"] as string); - - if (first && trigger.Value.Inputs.ContainsKey("runAtStartup") && (bool)trigger.Value.Inputs["runAtStartup"]) - jobs.Trigger(workflow.Id.ToString()); + Workflow = workflow, + Trigger = new Trigger + { + Inputs = trigger.Value.Inputs, + ScheduledTime = DateTimeOffset.UtcNow, + Type = trigger.Value.Type, + Key = trigger.Key + }, + })), trigger.Value.Inputs["cronExpression"] as string, GetTimeZone(trigger)); + + if (first && trigger.Value.Inputs.ContainsKey("runAtStartup") && (bool)trigger.Value.Inputs["runAtStartup"]) + jobs.Trigger(workflow.Id.ToString()); + } } } @@ -66,7 +69,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } - + static TimeZoneInfo GetTimeZone(KeyValuePair trigger) + { + + if (trigger.Value.Inputs.ContainsKey("timezone") && trigger.Value.Inputs["timezone"] is string zone && !string.IsNullOrWhiteSpace(zone)) + return TimeZoneInfo.FindSystemTimeZoneById(zone) ?? TimeZoneInfo.Utc; + return TimeZoneInfo.Utc; + + } } } }