From dda0a2930084cbaa4f425b46f69e8bae8356108c Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 1 Feb 2025 21:23:51 +0300 Subject: [PATCH] refactor: subscribe to onChange instead of waiting for the event to be received from the manager --- .../Common/Services/ScheduleSyncManager.cs | 5 ---- .../BackgroundTasks/ScheduleSyncService.cs | 23 +++++++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Endpoint/Common/Services/ScheduleSyncManager.cs b/Endpoint/Common/Services/ScheduleSyncManager.cs index 7bff018..e380d54 100644 --- a/Endpoint/Common/Services/ScheduleSyncManager.cs +++ b/Endpoint/Common/Services/ScheduleSyncManager.cs @@ -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(); } \ No newline at end of file diff --git a/Endpoint/Configuration/Core/BackgroundTasks/ScheduleSyncService.cs b/Endpoint/Configuration/Core/BackgroundTasks/ScheduleSyncService.cs index 8d49770..9c57870 100644 --- a/Endpoint/Configuration/Core/BackgroundTasks/ScheduleSyncService.cs +++ b/Endpoint/Configuration/Core/BackgroundTasks/ScheduleSyncService.cs @@ -15,23 +15,32 @@ namespace Mirea.Api.Endpoint.Configuration.Core.BackgroundTasks; public class ScheduleSyncService : IHostedService, IDisposable { private Timer? _timer; - private readonly IOptionsMonitor _generalConfigMonitor; + private string _cronUpdate; private readonly ILogger _logger; private CancellationTokenSource _cancellationTokenSource = new(); private readonly IServiceProvider _serviceProvider; + private readonly IDisposable? _onChangeUpdateCron; public ScheduleSyncService(IOptionsMonitor generalConfigMonitor, ILogger 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);