Skip to content

Commit

Permalink
fix: added environment variables to disable workflows,workflows__{nam…
Browse files Browse the repository at this point in the history
…e}=false will remove the workflow schedule
  • Loading branch information
pksorensen committed Aug 8, 2024
1 parent 6d36157 commit fa4d37e
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hangfire;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,31 +30,45 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

var workflows = sp.GetRequiredService<IWorkflowRepository>();
var jobs = sp.GetRequiredService<IRecurringJobManager>();
var configuration = sp.GetRequiredService<IConfiguration>();

foreach (var workflow in await workflows.GetAllWorkflows())
{

foreach (var trigger in workflow.Manifest.Triggers.Where(t => t.Value.Type == "TimerTrigger"))
{



if (!trigger.Equals(default(KeyValuePair<string, TriggerMetadata>)))
{
if(configuration.GetSection($"Workflows:{workflow.GetType().Name}")?.Value == "false" ||
configuration.GetSection($"Workflows:{workflow.GetType().Name}:{trigger.Key}")?.Value == "false"
)
{
jobs.RemoveIfExists(workflow.Id.ToString() + trigger.Key);
continue;
}

workflow.Manifest = null;

jobs.AddOrUpdate(workflow.Id.ToString() + trigger.Key,
(System.Linq.Expressions.Expression<System.Action<IHangfireWorkflowExecutor>>)((executor) => executor.TriggerAsync(new TriggerContext
{
PrincipalId = "1b714972-8d0a-4feb-b166-08d93c6ae328",
Workflow = workflow,
Trigger = new Trigger
{
Inputs = trigger.Value.Inputs,
ScheduledTime = DateTimeOffset.UtcNow,
Type = trigger.Value.Type,
Key = trigger.Key
},
}, null)), trigger.Value.Inputs["cronExpression"] as string,GetTimeZone(trigger) );
(System.Linq.Expressions.Expression<System.Action<IHangfireWorkflowExecutor>>) ((executor) => executor.TriggerAsync(new TriggerContext
{
PrincipalId = "1b714972-8d0a-4feb-b166-08d93c6ae328",
Workflow = workflow,
Trigger = new Trigger
{
Inputs = trigger.Value.Inputs,
ScheduledTime = DateTimeOffset.UtcNow,
Type = trigger.Value.Type,
Key = trigger.Key
},
}, null)), trigger.Value.Inputs["cronExpression"] as string,new RecurringJobOptions
{
TimeZone = GetTimeZone(trigger)
});


if (first && trigger.Value.Inputs.ContainsKey("runAtStartup") && (bool)trigger.Value.Inputs["runAtStartup"])
jobs.Trigger(workflow.Id.ToString() + trigger.Key);
Expand Down

0 comments on commit fa4d37e

Please sign in to comment.