| | | 1 | | using Cronos; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Common.RecurringTasks; |
| | | 5 | | |
| | 1 | 6 | | public class RecurringTaskScheduleManager(IOptions<RecurringTaskOptions> options, ISystemClock systemClock) |
| | | 7 | | { |
| | 7 | 8 | | public IDictionary<Type, ISchedule> ScheduledTasks { get; set; } = new Dictionary<Type, ISchedule>(); |
| | | 9 | | |
| | | 10 | | public ISchedule GetScheduleFor(Type taskType) |
| | | 11 | | { |
| | 3 | 12 | | if (!ScheduledTasks.TryGetValue(taskType, out var schedule)) |
| | | 13 | | { |
| | 3 | 14 | | var intervalExpression = options.Value.Schedule.ScheduledTasks.TryGetValue(taskType, out var expr) ? expr : |
| | 3 | 15 | | schedule = intervalExpression != null ? CreateSchedule(intervalExpression) : new IntervalSchedule(TimeSpan.F |
| | 3 | 16 | | ScheduledTasks[taskType] = schedule; |
| | | 17 | | } |
| | | 18 | | |
| | 3 | 19 | | return schedule; |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | private ISchedule CreateSchedule(IntervalExpression intervalExpression) |
| | | 23 | | { |
| | 3 | 24 | | return intervalExpression.Type switch |
| | 3 | 25 | | { |
| | 0 | 26 | | IntervalExpressionType.Cron => new CronSchedule(systemClock, CronExpression.Parse(intervalExpression.Express |
| | 3 | 27 | | IntervalExpressionType.Interval => new IntervalSchedule(TimeSpan.Parse(intervalExpression.Expression)), |
| | 0 | 28 | | _ => throw new NotSupportedException($"Interval expression type '{intervalExpression.Type}' is not supported |
| | 3 | 29 | | }; |
| | | 30 | | } |
| | | 31 | | } |