elsa-core/src/activities/Elsa.Activities.Timers.Hangfire/Extensions/BackgroundJobClientExtensions.cs
Craig Fowler ac44bf4535 WIP #564 - Bulk namepace change: Timers → Temporal
The name "Timers" is being replaced with "Temporal".  This commit
changes just the code (not project names/directories).
2021-02-28 20:49:34 +01:00

34 lines
1.3 KiB
C#

using System;
using System.Linq;
using Elsa.Activities.Temporal.Hangfire.Jobs;
using Elsa.Activities.Temporal.Hangfire.Models;
using Hangfire;
// ReSharper disable once CheckNamespace
namespace Elsa.Activities.Temporal
{
public static class BackgroundJobClientExtensions
{
public static void ScheduleWorkflow(this IBackgroundJobClient backgroundJobClient, RunHangfireWorkflowJobModel data, DateTimeOffset dateTimeOffset)
{
backgroundJobClient.UnscheduleJobWhenAlreadyExists(data);
backgroundJobClient.Schedule<RunHangfireWorkflowJob>(job => job.ExecuteAsync(data), dateTimeOffset);
}
public static void UnscheduleJobWhenAlreadyExists(this IBackgroundJobClient backgroundJobClient, RunHangfireWorkflowJobModel data)
{
var identity = data.GetIdentity();
var monitor = JobStorage.Current.GetMonitoringApi();
var workflowJobType = typeof(RunHangfireWorkflowJob);
var jobs = monitor.ScheduledJobs(0, int.MaxValue)
.Where(x => x.Value.Job.Type == workflowJobType && ((RunHangfireWorkflowJobModel)x.Value.Job.Args[0]).GetIdentity() == identity);
foreach (var job in jobs)
{
BackgroundJob.Delete(job.Key);
}
}
}
}