refactor: subscribe to onChange instead of waiting for the event to be received from the manager
This commit is contained in:
@ -4,12 +4,7 @@ namespace Mirea.Api.Endpoint.Common.Services;
|
||||
|
||||
public static class ScheduleSyncManager
|
||||
{
|
||||
public static event Action? OnUpdateIntervalRequested;
|
||||
public static event Action? OnForceSyncRequested;
|
||||
|
||||
public static void RequestIntervalUpdate() =>
|
||||
OnUpdateIntervalRequested?.Invoke();
|
||||
|
||||
public static void RequestForceSync() =>
|
||||
OnForceSyncRequested?.Invoke();
|
||||
}
|
@ -15,23 +15,32 @@ namespace Mirea.Api.Endpoint.Configuration.Core.BackgroundTasks;
|
||||
public class ScheduleSyncService : IHostedService, IDisposable
|
||||
{
|
||||
private Timer? _timer;
|
||||
private readonly IOptionsMonitor<GeneralConfig> _generalConfigMonitor;
|
||||
private string _cronUpdate;
|
||||
private readonly ILogger<ScheduleSyncService> _logger;
|
||||
private CancellationTokenSource _cancellationTokenSource = new();
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IDisposable? _onChangeUpdateCron;
|
||||
|
||||
public ScheduleSyncService(IOptionsMonitor<GeneralConfig> generalConfigMonitor, ILogger<ScheduleSyncService> logger, IServiceProvider serviceProvider)
|
||||
{
|
||||
_generalConfigMonitor = generalConfigMonitor;
|
||||
_logger = logger;
|
||||
_serviceProvider = serviceProvider;
|
||||
_cronUpdate = generalConfigMonitor.CurrentValue.ScheduleSettings!.CronUpdateSchedule;
|
||||
|
||||
ScheduleSyncManager.OnForceSyncRequested += OnForceSyncRequested;
|
||||
ScheduleSyncManager.OnUpdateIntervalRequested += OnUpdateIntervalRequested;
|
||||
_onChangeUpdateCron = generalConfigMonitor.OnChange((config) =>
|
||||
{
|
||||
if (config.ScheduleSettings?.CronUpdateSchedule == null || _cronUpdate == config.ScheduleSettings.CronUpdateSchedule)
|
||||
return;
|
||||
|
||||
_cronUpdate = config.ScheduleSettings.CronUpdateSchedule;
|
||||
OnUpdateIntervalRequested();
|
||||
});
|
||||
}
|
||||
|
||||
private void OnForceSyncRequested()
|
||||
{
|
||||
_logger.LogInformation("It was requested to synchronize the data immediately.");
|
||||
StopAsync(CancellationToken.None).ContinueWith(_ =>
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
@ -41,6 +50,7 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
|
||||
private void OnUpdateIntervalRequested()
|
||||
{
|
||||
_logger.LogInformation("It was requested to update the time interval immediately.");
|
||||
StopAsync(CancellationToken.None).ContinueWith(_ =>
|
||||
{
|
||||
StartAsync(CancellationToken.None);
|
||||
@ -49,14 +59,13 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
|
||||
private void ScheduleNextRun()
|
||||
{
|
||||
var cronExpression = _generalConfigMonitor.CurrentValue.ScheduleSettings?.CronUpdateSchedule;
|
||||
if (string.IsNullOrEmpty(cronExpression))
|
||||
if (string.IsNullOrEmpty(_cronUpdate))
|
||||
{
|
||||
_logger.LogWarning("Cron expression is not set. The scheduled task will not run.");
|
||||
return;
|
||||
}
|
||||
|
||||
var nextRunTime = CronExpression.Parse(cronExpression).GetNextOccurrence(DateTimeOffset.Now, TimeZoneInfo.Local);
|
||||
var nextRunTime = CronExpression.Parse(_cronUpdate).GetNextOccurrence(DateTimeOffset.Now, TimeZoneInfo.Local);
|
||||
|
||||
if (!nextRunTime.HasValue)
|
||||
{
|
||||
@ -112,7 +121,7 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
StopAsync(CancellationToken.None).GetAwaiter().GetResult();
|
||||
_timer?.Dispose();
|
||||
ScheduleSyncManager.OnForceSyncRequested -= OnForceSyncRequested;
|
||||
ScheduleSyncManager.OnUpdateIntervalRequested -= OnUpdateIntervalRequested;
|
||||
_onChangeUpdateCron?.Dispose();
|
||||
_cancellationTokenSource.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
|
Reference in New Issue
Block a user