feat: add cron skipping date
This commit is contained in:
@ -5,8 +5,11 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Mirea.Api.Endpoint.Common.Services;
|
||||
using Mirea.Api.Endpoint.Configuration.Model;
|
||||
using Mirea.Api.Endpoint.Configuration.Model.GeneralSettings;
|
||||
using Mirea.Api.Endpoint.Sync;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -16,6 +19,7 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
{
|
||||
private Timer? _timer;
|
||||
private string _cronUpdate;
|
||||
private List<ScheduleSettings.CronUpdateSkip> _cronUpdateSkip;
|
||||
private readonly ILogger<ScheduleSyncService> _logger;
|
||||
private CancellationTokenSource _cancellationTokenSource = new();
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
@ -26,15 +30,28 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
_logger = logger;
|
||||
_serviceProvider = serviceProvider;
|
||||
_cronUpdate = generalConfigMonitor.CurrentValue.ScheduleSettings!.CronUpdateSchedule;
|
||||
_cronUpdateSkip = generalConfigMonitor.CurrentValue.ScheduleSettings!.CronUpdateSkipDateList;
|
||||
|
||||
ScheduleSyncManager.OnForceSyncRequested += OnForceSyncRequested;
|
||||
_onChangeUpdateCron = generalConfigMonitor.OnChange((config) =>
|
||||
{
|
||||
if (config.ScheduleSettings?.CronUpdateSchedule == null || _cronUpdate == config.ScheduleSettings.CronUpdateSchedule)
|
||||
return;
|
||||
var updated = false;
|
||||
if (config.ScheduleSettings?.CronUpdateSchedule != null && _cronUpdate != config.ScheduleSettings.CronUpdateSchedule)
|
||||
{
|
||||
_cronUpdate = config.ScheduleSettings.CronUpdateSchedule;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
_cronUpdate = config.ScheduleSettings.CronUpdateSchedule;
|
||||
OnUpdateIntervalRequested();
|
||||
if (config.ScheduleSettings?.CronUpdateSkipDateList != null && !config.ScheduleSettings.CronUpdateSkipDateList.SequenceEqual(_cronUpdateSkip))
|
||||
{
|
||||
_cronUpdateSkip = config.ScheduleSettings.CronUpdateSkipDateList
|
||||
.OrderBy(x => x.End ?? x.Date)
|
||||
.ToList();
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updated)
|
||||
OnUpdateIntervalRequested();
|
||||
});
|
||||
}
|
||||
|
||||
@ -65,17 +82,20 @@ public class ScheduleSyncService : IHostedService, IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
var nextRunTime = CronExpression.Parse(_cronUpdate).GetNextOccurrence(DateTimeOffset.Now, TimeZoneInfo.Local);
|
||||
var expression = CronExpression.Parse(_cronUpdate);
|
||||
|
||||
if (!nextRunTime.HasValue)
|
||||
var nextRunTime = _cronUpdateSkip.GetNextTask(expression).FirstOrDefault();
|
||||
|
||||
if (nextRunTime == default)
|
||||
{
|
||||
_logger.LogWarning("No next run time found. The task will not be scheduled. Timezone: {TimeZone}", TimeZoneInfo.Local.DisplayName);
|
||||
_logger.LogWarning("No next run time found. The task will not be scheduled. Timezone: {TimeZone}",
|
||||
TimeZoneInfo.Local.DisplayName);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Next task run in {Time}", nextRunTime.Value.ToString("G"));
|
||||
_logger.LogInformation("Next task run in {Time}", nextRunTime.ToString("G"));
|
||||
|
||||
var delay = (nextRunTime.Value - DateTimeOffset.Now).TotalMilliseconds;
|
||||
var delay = (nextRunTime - DateTimeOffset.Now).TotalMilliseconds;
|
||||
|
||||
// The chance is small, but it's better to check
|
||||
if (delay <= 0)
|
||||
|
Reference in New Issue
Block a user