|
| 1 | +using System; |
| 2 | +using Quartz; |
| 3 | +using Quartz.Impl; |
| 4 | + |
| 5 | +namespace InEngine.Core.Scheduling |
| 6 | +{ |
| 7 | + public class Occurence |
| 8 | + { |
| 9 | + public IScheduler Scheduler { get; set; } = StdSchedulerFactory.GetDefaultScheduler(); |
| 10 | + public AbstractCommand Command { get; set; } |
| 11 | + public IJobDetail JobDetail { get; set; } |
| 12 | + |
| 13 | + public static TriggerBuilder MakeTriggerBuilder(AbstractCommand command) |
| 14 | + { |
| 15 | + if (command == null) |
| 16 | + throw new ArgumentNullException(command.GetType().Name, "The command to schedule cannot be null."); |
| 17 | + return TriggerBuilder |
| 18 | + .Create() |
| 19 | + .WithIdentity($"{command.Name}:job:{command.ScheduleId}", command.SchedulerGroup); |
| 20 | + } |
| 21 | + |
| 22 | + public void RegisterJob(Action<DailyTimeIntervalScheduleBuilder> action) |
| 23 | + { |
| 24 | + Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithDailyTimeIntervalSchedule(action).Build()); |
| 25 | + } |
| 26 | + |
| 27 | + public void RegisterJob(string cronExpression) |
| 28 | + { |
| 29 | + Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithCronSchedule(cronExpression).Build()); |
| 30 | + } |
| 31 | + |
| 32 | + public void RegisterJob(Action<SimpleScheduleBuilder> action) |
| 33 | + { |
| 34 | + Scheduler.ScheduleJob(JobDetail, MakeTriggerBuilder(Command).WithSimpleSchedule(action).Build()); |
| 35 | + } |
| 36 | + |
| 37 | + public void Cron(string cronExpression) |
| 38 | + { |
| 39 | + RegisterJob(cronExpression); |
| 40 | + } |
| 41 | + |
| 42 | + public void EverySecond() |
| 43 | + { |
| 44 | + RegisterJob(x => x.WithIntervalInSeconds(1).RepeatForever()); |
| 45 | + } |
| 46 | + |
| 47 | + public void EveryMinute() |
| 48 | + { |
| 49 | + RegisterJob(x => x.WithIntervalInMinutes(1).RepeatForever()); |
| 50 | + } |
| 51 | + |
| 52 | + public void EveryFiveMinutes() |
| 53 | + { |
| 54 | + RegisterJob(x => x.WithIntervalInMinutes(5).RepeatForever()); |
| 55 | + } |
| 56 | + |
| 57 | + public void EveryTenMinutes() |
| 58 | + { |
| 59 | + RegisterJob(x => x.WithIntervalInMinutes(10).RepeatForever()); |
| 60 | + } |
| 61 | + |
| 62 | + public void EveryFifteenMinutes() |
| 63 | + { |
| 64 | + RegisterJob(x => x.WithIntervalInMinutes(15).RepeatForever()); |
| 65 | + } |
| 66 | + |
| 67 | + public void EveryThirtyMinutes() |
| 68 | + { |
| 69 | + RegisterJob(x => x.WithIntervalInMinutes(30).RepeatForever()); |
| 70 | + } |
| 71 | + |
| 72 | + public void Hourly() |
| 73 | + { |
| 74 | + RegisterJob(x => x.WithIntervalInHours(1).RepeatForever()); |
| 75 | + } |
| 76 | + |
| 77 | + public void HourlyAt(int minutesAfterTheHour) |
| 78 | + { |
| 79 | + RegisterJob($"0 {minutesAfterTheHour} * * * ?"); |
| 80 | + } |
| 81 | + |
| 82 | + public void Daily() |
| 83 | + { |
| 84 | + RegisterJob(x => x.WithIntervalInHours(24).RepeatForever()); |
| 85 | + } |
| 86 | + |
| 87 | + public void DailyAt(TimeOfDay timeOfDay) |
| 88 | + { |
| 89 | + RegisterJob(x => x.StartingDailyAt(timeOfDay)); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments