-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from anthonypos/master
IRecurringJobManager updates
- Loading branch information
Showing
9 changed files
with
160 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Hangfire; | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace MIFCore.Hangfire | ||
{ | ||
public static class IRecurringJobManagerExtensions | ||
{ | ||
public static void CreateRecurringJob(this IRecurringJobManager recurringJobManager, string jobName, Expression<Func<Task>> methodCall, string cronSchedule = null, string queue = "default", bool triggerIfNeverExecuted = false) | ||
{ | ||
var manager = recurringJobManager as MIFCoreRecurringJobManager; | ||
|
||
if (manager is null) | ||
throw new ArgumentException("Parameter 'recurringJobManager' is not of type 'MIFCore.Hangfire.MIFCoreRecurringJobManager'"); | ||
|
||
manager.CreateRecurringJob(jobName: jobName, methodCall: methodCall, cronSchedule: cronSchedule, queue: queue, triggerIfNeverExecuted: triggerIfNeverExecuted); | ||
} | ||
|
||
public static void CreateRecurringJob<T>(this IRecurringJobManager recurringJobManager, string jobName, Expression<Func<T, Task>> methodCall, string cronSchedule = null, string queue = "default", bool triggerIfNeverExecuted = false) | ||
{ | ||
var manager = recurringJobManager as MIFCoreRecurringJobManager; | ||
|
||
if (manager is null) | ||
throw new ArgumentException("Parameter 'recurringJobManager' is not of type 'MIFCore.Hangfire.MIFCoreRecurringJobManager'"); | ||
|
||
manager.CreateRecurringJob<T>(jobName: jobName, methodCall: methodCall, cronSchedule: cronSchedule, queue: queue, triggerIfNeverExecuted: triggerIfNeverExecuted); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using Hangfire; | ||
using Hangfire.Annotations; | ||
using Hangfire.Common; | ||
using Hangfire.Storage; | ||
using Microsoft.Extensions.Configuration; | ||
using MIFCore.Common; | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace MIFCore.Hangfire | ||
{ | ||
public class MIFCoreRecurringJobManager : IRecurringJobManager | ||
{ | ||
private readonly IRecurringJobManager recurringJobManager; | ||
private readonly JobStorage jobStorage; | ||
|
||
public MIFCoreRecurringJobManager(JobStorage jobStorage) | ||
{ | ||
this.recurringJobManager = new RecurringJobManager(jobStorage); | ||
this.jobStorage = jobStorage; | ||
} | ||
|
||
public void CreateRecurringJob<T>(string jobName, Expression<Func<T, Task>> methodCall, string cronSchedule = null, string queue = "default", bool triggerIfNeverExecuted = false) | ||
{ | ||
cronSchedule = this.GetCronSchedule(jobName, cronSchedule); | ||
|
||
var job = Job.FromExpression(methodCall); | ||
|
||
this.recurringJobManager.AddOrUpdate<T>( | ||
recurringJobId: jobName, | ||
methodCall: methodCall, | ||
cronExpression: cronSchedule, | ||
timeZone: TimeZoneInfo.Local, | ||
queue: queue); | ||
|
||
if (triggerIfNeverExecuted) | ||
this.TriggerRecurringJobIfNeverExecuted(jobName); | ||
} | ||
|
||
public void CreateRecurringJob(string jobName, Expression<Func<Task>> methodCall, string cronSchedule = null, string queue = "default", bool triggerIfNeverExecuted = false) | ||
{ | ||
cronSchedule = this.GetCronSchedule(jobName, cronSchedule); | ||
|
||
this.recurringJobManager.AddOrUpdate( | ||
recurringJobId: jobName, | ||
methodCall: methodCall, | ||
cronExpression: cronSchedule, | ||
timeZone: TimeZoneInfo.Local, | ||
queue: queue); | ||
|
||
if (triggerIfNeverExecuted) | ||
this.TriggerRecurringJobIfNeverExecuted(jobName); | ||
} | ||
|
||
public string GetCronSchedule(string jobName, string cronSchedule = null) | ||
{ | ||
// override if jobName is available in the settings file. | ||
var cronOverride = this.GetCronFromConfig(jobName); | ||
|
||
if (string.IsNullOrWhiteSpace(cronOverride)) | ||
{ | ||
return cronSchedule ?? Cron.Daily(); | ||
} | ||
else | ||
{ | ||
return cronOverride; | ||
} | ||
} | ||
|
||
private string GetCronFromConfig(string jobName) | ||
{ | ||
var section = Globals.DefaultConfiguration.GetSection(jobName); | ||
return section.Exists() ? section.Value : null; | ||
} | ||
|
||
private void TriggerRecurringJobIfNeverExecuted(string jobName) | ||
{ | ||
var connection = this.jobStorage.GetConnection(); | ||
var newJob = connection.GetRecurringJobs(new string[] { jobName }).First(); | ||
|
||
if (newJob.LastExecution is null) | ||
RecurringJob.Trigger(jobName); | ||
} | ||
|
||
public void RemoveIfExists([NotNull] string recurringJobId) | ||
{ | ||
RecurringJob.RemoveIfExists(recurringJobId); | ||
} | ||
|
||
public void Trigger([NotNull] string recurringJobId) | ||
{ | ||
RecurringJob.Trigger(recurringJobId); | ||
} | ||
|
||
public void AddOrUpdate([NotNull] string recurringJobId, [NotNull] Job job, [NotNull] string cronExpression, [NotNull] RecurringJobOptions options) | ||
{ | ||
cronExpression = GetCronSchedule(recurringJobId, cronExpression); | ||
|
||
this.recurringJobManager.AddOrUpdate(recurringJobId: recurringJobId, job: job, cronExpression: cronExpression, options: options); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters