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