elsa-core/src/modules/Elsa.Common/RecurringTasks/RecurringTaskScheduleManager.cs
Sipke Schoorstra 6c0f7a48e7 Refactor recurring tasks scheduling logic.
Removed `ConfigureRecurringTasksScheduleStartupTask` and moved its functionality into `RecurringTaskScheduleManager`. Simplified recurring task configuration and streamlined dependencies, improving maintainability. Added retention policies to `Elsa.Server.Web`.
2025-01-02 11:11:42 +01:00

31 lines
1.3 KiB
C#

using Cronos;
using Microsoft.Extensions.Options;
namespace Elsa.Common.RecurringTasks;
public class RecurringTaskScheduleManager(IOptions<RecurringTaskOptions> options, ISystemClock systemClock)
{
public IDictionary<Type, ISchedule> ScheduledTasks { get; set; } = new Dictionary<Type, ISchedule>();
public ISchedule GetScheduleFor(Type taskType)
{
if (!ScheduledTasks.TryGetValue(taskType, out var schedule))
{
var intervalExpression = options.Value.Schedule.ScheduledTasks.TryGetValue(taskType, out var expr) ? expr : null;
schedule = intervalExpression != null ? CreateSchedule(intervalExpression) : new IntervalSchedule(TimeSpan.FromMinutes(1));
ScheduledTasks[taskType] = schedule;
}
return schedule;
}
private ISchedule CreateSchedule(IntervalExpression intervalExpression)
{
return intervalExpression.Type switch
{
IntervalExpressionType.Cron => new CronSchedule(systemClock, CronExpression.Parse(intervalExpression.Expression)),
IntervalExpressionType.Interval => new IntervalSchedule(TimeSpan.Parse(intervalExpression.Expression)),
_ => throw new NotSupportedException($"Interval expression type '{intervalExpression.Type}' is not supported.")
};
}
}