Skip to content

Commit

Permalink
fix: added multiple trigger support for timed triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
pksorensen committed Nov 2, 2023
1 parent 3443315 commit 6e1bbc5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/WorkflowEngine.Core/ExpressionEngineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async ValueTask<IDictionary<string,object>> ResolveInputs(this IEx
{

var resolvedInputs = new Dictionary<string, object>();

if(actionMetadata.Inputs!=null)
foreach (var input in actionMetadata.Inputs)
{
if (input.Value is string str && str.Contains("@"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IServiceCollection AddWorkflowEngine<TOutputsRepository>(this ISer

services.AddFunctions();
services.AddScoped<IOutputsRepository, TOutputsRepository>();
services.AddSingleton<IWorkflowRepository, DefaultWorkflowRepository>();
services.AddTransient<IWorkflowRepository, DefaultWorkflowRepository>();

services.AddHostedService<WorkflowStarterBackgroundJob>();

Expand Down
9 changes: 3 additions & 6 deletions src/WorkflowEngine.Hangfire/WorkflowEngine.Hangfire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
</ItemGroup>



<ItemGroup>
<ProjectReference Include="..\WorkflowEngine.Core\WorkflowEngine.Core.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WorkflowEngine.Core\WorkflowEngine.Core.csproj" />
</ItemGroup>

</Project>
50 changes: 30 additions & 20 deletions src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TriggerMetadata>)))
foreach (var trigger in workflow.Manifest.Triggers.Where(t => t.Value.Type == "TimerTrigger"))
{
workflow.Manifest = null;

jobs.AddOrUpdate<IHangfireWorkflowExecutor>(workflow.Id.ToString(),
(executor) => executor.TriggerAsync(new TriggerContext
{
Workflow = workflow,
Trigger = new Trigger

if (!trigger.Equals(default(KeyValuePair<string, TriggerMetadata>)))
{

workflow.Manifest = null;

jobs.AddOrUpdate(workflow.Id.ToString() + trigger.Key,
(System.Linq.Expressions.Expression<Action<IHangfireWorkflowExecutor>>)((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());
}
}
}

Expand All @@ -66,7 +69,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

}


static TimeZoneInfo GetTimeZone(KeyValuePair<string, TriggerMetadata> 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;

}
}
}
}

0 comments on commit 6e1bbc5

Please sign in to comment.