-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-1230: Add UnregisterEventHandler() (#2797)
- Loading branch information
1 parent
94aa186
commit f87c135
Showing
13 changed files
with
335 additions
and
39 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
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
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
21 changes: 21 additions & 0 deletions
21
src/VirtoCommerce.Platform.Hangfire/IRecurringJobService.cs
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,21 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
using VirtoCommerce.Platform.Core.Settings; | ||
|
||
namespace VirtoCommerce.Platform.Hangfire; | ||
|
||
public interface IRecurringJobService | ||
{ | ||
void WatchJobSetting<T>( | ||
SettingDescriptor enablerSetting, | ||
SettingDescriptor cronSetting, | ||
Expression<Func<T, Task>> methodCall, | ||
string jobId, | ||
TimeZoneInfo timeZoneInfo, | ||
string queue); | ||
|
||
void WatchJobSetting(SettingCronJob settingCronJob); | ||
|
||
Task WatchJobSettingAsync(SettingCronJob settingCronJob); | ||
} |
108 changes: 108 additions & 0 deletions
108
src/VirtoCommerce.Platform.Hangfire/RecurringJobService.cs
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,108 @@ | ||
// This service is functionally identical to the obsolete RecurringJobExtensions | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
using Hangfire; | ||
using VirtoCommerce.Platform.Core.Common; | ||
using VirtoCommerce.Platform.Core.Events; | ||
using VirtoCommerce.Platform.Core.Settings; | ||
using VirtoCommerce.Platform.Core.Settings.Events; | ||
using VirtoCommerce.Platform.Hangfire.Extensions; | ||
|
||
namespace VirtoCommerce.Platform.Hangfire; | ||
|
||
public class RecurringJobService : IRecurringJobService, IEventHandler<ObjectSettingChangedEvent> | ||
{ | ||
// Key is the observed setting name, value is the object of setting job | ||
private static readonly ConcurrentDictionary<string, SettingCronJob> _observedSettingsDict = new(); | ||
|
||
private readonly IRecurringJobManager _recurringJobManager; | ||
private readonly ISettingsManager _settingsManager; | ||
|
||
public RecurringJobService(IRecurringJobManager recurringJobManager, ISettingsManager settingsManager) | ||
{ | ||
_recurringJobManager = recurringJobManager; | ||
_settingsManager = settingsManager; | ||
} | ||
|
||
public void WatchJobSetting<T>( | ||
SettingDescriptor enablerSetting, | ||
SettingDescriptor cronSetting, | ||
Expression<Func<T, Task>> methodCall, | ||
string jobId, | ||
TimeZoneInfo timeZoneInfo, | ||
string queue) | ||
{ | ||
var settingCronJob = new SettingCronJobBuilder(new SettingCronJob()) | ||
.SetEnablerSetting(enablerSetting) | ||
.SetCronSetting(cronSetting) | ||
.SetJobId(jobId) | ||
.SetQueueName(queue) | ||
.SetTimeZoneInfo(timeZoneInfo) | ||
.ToJob(methodCall) | ||
.Build(); | ||
|
||
WatchJobSetting(settingCronJob); | ||
} | ||
|
||
/// <summary> | ||
/// Use SettingCronJobBuilder for creating SettingCronJob | ||
/// </summary> | ||
public void WatchJobSetting(SettingCronJob settingCronJob) | ||
{ | ||
WatchJobSettingAsync(settingCronJob).GetAwaiter().GetResult(); | ||
} | ||
|
||
/// <summary> | ||
/// Use SettingCronJobBuilder for creating SettingCronJob | ||
/// </summary> | ||
public Task WatchJobSettingAsync(SettingCronJob settingCronJob) | ||
{ | ||
_observedSettingsDict.AddOrUpdate(settingCronJob.EnableSetting.Name, settingCronJob, (_, _) => settingCronJob); | ||
_observedSettingsDict.AddOrUpdate(settingCronJob.CronSetting.Name, settingCronJob, (_, _) => settingCronJob); | ||
|
||
return RunOrRemoveJobAsync(settingCronJob); | ||
} | ||
|
||
public async Task Handle(ObjectSettingChangedEvent message) | ||
{ | ||
foreach (var settingName in message.ChangedEntries | ||
.Where(x => x.EntryState is EntryState.Modified or EntryState.Added) | ||
.Select(x => x.NewEntry.Name)) | ||
{ | ||
if (_observedSettingsDict.TryGetValue(settingName, out var settingCronJob)) | ||
{ | ||
await RunOrRemoveJobAsync(settingCronJob); | ||
} | ||
} | ||
|
||
// Temporary solution for backward compatibility | ||
#pragma warning disable VC0008 // Type or member is obsolete | ||
await _recurringJobManager.HandleSettingChangeAsync(_settingsManager, message); | ||
#pragma warning restore VC0008 // Type or member is obsolete | ||
} | ||
|
||
private async Task RunOrRemoveJobAsync(SettingCronJob settingCronJob) | ||
{ | ||
var processJobEnableSettingValue = await _settingsManager.GetValueAsync<object>(settingCronJob.EnableSetting); | ||
var processJobEnable = settingCronJob.EnabledEvaluator(processJobEnableSettingValue); | ||
|
||
if (processJobEnable) | ||
{ | ||
var cronExpression = await _settingsManager.GetValueAsync<string>(settingCronJob.CronSetting); | ||
|
||
_recurringJobManager.AddOrUpdate( | ||
settingCronJob.RecurringJobId, | ||
settingCronJob.Job, | ||
cronExpression, | ||
settingCronJob.TimeZone, | ||
settingCronJob.Queue); | ||
} | ||
else | ||
{ | ||
_recurringJobManager.RemoveIfExists(settingCronJob.RecurringJobId); | ||
} | ||
} | ||
} |
Oops, something went wrong.