|
1 | 1 | using Cofoundry.Core.BackgroundTasks;
|
2 | 2 | using Hangfire;
|
3 |
| -using System; |
4 | 3 |
|
5 |
| -namespace Cofoundry.Plugins.BackgroundTasks.Hangfire |
| 4 | +namespace Cofoundry.Plugins.BackgroundTasks.Hangfire; |
| 5 | + |
| 6 | +public class HangfireBackgroundTaskScheduler : IBackgroundTaskScheduler |
6 | 7 | {
|
7 |
| - public class HangfireBackgroundTaskScheduler : IBackgroundTaskScheduler |
| 8 | + public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int days, int atHour, int atMinute) where TTask : IRecurringBackgroundTask |
8 | 9 | {
|
9 |
| - public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int days, int atHour, int atMinute) where TTask : IRecurringBackgroundTask |
10 |
| - { |
11 |
| - ValidateDailyTaskParameters(days, atHour, atMinute); |
| 10 | + ValidateDailyTaskParameters(days, atHour, atMinute); |
12 | 11 |
|
13 |
| - string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute); |
14 |
| - RegisterRecurringTask<TTask>(cronExpression); |
| 12 | + string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute); |
| 13 | + RegisterRecurringTask<TTask>(cronExpression); |
15 | 14 |
|
16 |
| - return this; |
17 |
| - } |
| 15 | + return this; |
| 16 | + } |
18 | 17 |
|
19 |
| - public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IRecurringBackgroundTask |
| 18 | + public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IRecurringBackgroundTask |
| 19 | + { |
| 20 | + ValidateHourlyTaskParameters(hours, minute); |
| 21 | + |
| 22 | + if (hours == 0 && minute > 0) |
20 | 23 | {
|
21 |
| - ValidateHourlyTaskParameters(hours, minute); |
| 24 | + return RegisterRecurringTask<TTask>(minute); |
| 25 | + } |
22 | 26 |
|
23 |
| - if (hours == 0 && minute > 0) |
24 |
| - { |
25 |
| - return RegisterRecurringTask<TTask>(minute); |
26 |
| - } |
| 27 | + string cronExpression = string.Format("{1} */{0} * * *", hours, minute); |
| 28 | + RegisterRecurringTask<TTask>(cronExpression); |
27 | 29 |
|
28 |
| - string cronExpression = string.Format("{1} */{0} * * *", hours, minute); |
29 |
| - RegisterRecurringTask<TTask>(cronExpression); |
| 30 | + return this; |
| 31 | + } |
30 | 32 |
|
31 |
| - return this; |
32 |
| - } |
| 33 | + public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int minutes) where TTask : IRecurringBackgroundTask |
| 34 | + { |
| 35 | + ValidateMinuteTaskParameters(minutes); |
33 | 36 |
|
34 |
| - public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int minutes) where TTask : IRecurringBackgroundTask |
| 37 | + if (minutes > 59) |
35 | 38 | {
|
36 |
| - ValidateMinuteTaskParameters(minutes); |
| 39 | + var hours = minutes / 60; |
| 40 | + minutes = minutes % 60; |
| 41 | + return RegisterRecurringTask<TTask>(hours, minutes); |
| 42 | + } |
37 | 43 |
|
38 |
| - if (minutes > 59) |
39 |
| - { |
40 |
| - var hours = minutes / 60; |
41 |
| - minutes = minutes % 60; |
42 |
| - return RegisterRecurringTask<TTask>(hours, minutes); |
43 |
| - } |
| 44 | + string cronExpression = string.Format("*/{0} * * * *", minutes); |
| 45 | + RegisterRecurringTask<TTask>(cronExpression); |
44 | 46 |
|
45 |
| - string cronExpression = string.Format("*/{0} * * * *", minutes); |
46 |
| - RegisterRecurringTask<TTask>(cronExpression); |
| 47 | + return this; |
| 48 | + } |
47 | 49 |
|
48 |
| - return this; |
49 |
| - } |
| 50 | + public IBackgroundTaskScheduler DeregisterRecurringTask<TTask>() where TTask : IRecurringBackgroundTask |
| 51 | + { |
| 52 | + RecurringJob.RemoveIfExists(GetJobId<TTask>()); |
50 | 53 |
|
51 |
| - public IBackgroundTaskScheduler DeregisterRecurringTask<TTask>() where TTask : IRecurringBackgroundTask |
52 |
| - { |
53 |
| - RecurringJob.RemoveIfExists(GetJobId<TTask>()); |
| 54 | + return this; |
| 55 | + } |
54 | 56 |
|
55 |
| - return this; |
56 |
| - } |
| 57 | + public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int days, int atHour = 0, int atMinute = 0) where TTask : IAsyncRecurringBackgroundTask |
| 58 | + { |
| 59 | + ValidateDailyTaskParameters(days, atHour, atMinute); |
57 | 60 |
|
58 |
| - public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int days, int atHour = 0, int atMinute = 0) where TTask : IAsyncRecurringBackgroundTask |
59 |
| - { |
60 |
| - ValidateDailyTaskParameters(days, atHour, atMinute); |
| 61 | + string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute); |
| 62 | + RegisterAsyncRecurringTask<TTask>(cronExpression); |
61 | 63 |
|
62 |
| - string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute); |
63 |
| - RegisterAsyncRecurringTask<TTask>(cronExpression); |
| 64 | + return this; |
| 65 | + } |
64 | 66 |
|
65 |
| - return this; |
66 |
| - } |
| 67 | + public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IAsyncRecurringBackgroundTask |
| 68 | + { |
| 69 | + ValidateHourlyTaskParameters(hours, minute); |
67 | 70 |
|
68 |
| - public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IAsyncRecurringBackgroundTask |
| 71 | + if (hours == 0 && minute > 0) |
69 | 72 | {
|
70 |
| - ValidateHourlyTaskParameters(hours, minute); |
| 73 | + return RegisterAsyncRecurringTask<TTask>(minute); |
| 74 | + } |
71 | 75 |
|
72 |
| - if (hours == 0 && minute > 0) |
73 |
| - { |
74 |
| - return RegisterAsyncRecurringTask<TTask>(minute); |
75 |
| - } |
| 76 | + string cronExpression = string.Format("{1} */{0} * * *", hours, minute); |
| 77 | + RegisterAsyncRecurringTask<TTask>(cronExpression); |
76 | 78 |
|
77 |
| - string cronExpression = string.Format("{1} */{0} * * *", hours, minute); |
78 |
| - RegisterAsyncRecurringTask<TTask>(cronExpression); |
| 79 | + return this; |
| 80 | + } |
79 | 81 |
|
80 |
| - return this; |
81 |
| - } |
| 82 | + public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int minutes) where TTask : IAsyncRecurringBackgroundTask |
| 83 | + { |
| 84 | + ValidateMinuteTaskParameters(minutes); |
82 | 85 |
|
83 |
| - public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int minutes) where TTask : IAsyncRecurringBackgroundTask |
| 86 | + if (minutes > 59) |
84 | 87 | {
|
85 |
| - ValidateMinuteTaskParameters(minutes); |
| 88 | + var hours = minutes / 60; |
| 89 | + minutes = minutes % 60; |
| 90 | + return RegisterAsyncRecurringTask<TTask>(hours, minutes); |
| 91 | + } |
86 | 92 |
|
87 |
| - if (minutes > 59) |
88 |
| - { |
89 |
| - var hours = minutes / 60; |
90 |
| - minutes = minutes % 60; |
91 |
| - return RegisterAsyncRecurringTask<TTask>(hours, minutes); |
92 |
| - } |
| 93 | + string cronExpression = string.Format("*/{0} * * * *", minutes); |
| 94 | + RegisterAsyncRecurringTask<TTask>(cronExpression); |
93 | 95 |
|
94 |
| - string cronExpression = string.Format("*/{0} * * * *", minutes); |
95 |
| - RegisterAsyncRecurringTask<TTask>(cronExpression); |
| 96 | + return this; |
| 97 | + } |
96 | 98 |
|
97 |
| - return this; |
98 |
| - } |
| 99 | + public IBackgroundTaskScheduler DeregisterAsyncRecurringTask<TTask>() where TTask : IAsyncRecurringBackgroundTask |
| 100 | + { |
| 101 | + RecurringJob.RemoveIfExists(GetJobId<TTask>()); |
99 | 102 |
|
100 |
| - public IBackgroundTaskScheduler DeregisterAsyncRecurringTask<TTask>() where TTask : IAsyncRecurringBackgroundTask |
101 |
| - { |
102 |
| - RecurringJob.RemoveIfExists(GetJobId<TTask>()); |
| 103 | + return this; |
| 104 | + } |
103 | 105 |
|
104 |
| - return this; |
105 |
| - } |
| 106 | + private string GetJobId<TTask>() |
| 107 | + { |
| 108 | + return typeof(TTask).FullName; |
| 109 | + } |
106 | 110 |
|
107 |
| - private string GetJobId<TTask>() |
108 |
| - { |
109 |
| - return typeof(TTask).FullName; |
110 |
| - } |
| 111 | + private void RegisterRecurringTask<TTask>(string cronExpression) where TTask : IRecurringBackgroundTask |
| 112 | + { |
| 113 | + RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.Execute(), cronExpression); |
| 114 | + } |
111 | 115 |
|
112 |
| - private void RegisterRecurringTask<TTask>(string cronExpression) where TTask : IRecurringBackgroundTask |
| 116 | + private void RegisterAsyncRecurringTask<TTask>(string cronExpression) where TTask : IAsyncRecurringBackgroundTask |
| 117 | + { |
| 118 | + RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.ExecuteAsync(), cronExpression); |
| 119 | + } |
| 120 | + |
| 121 | + private void ValidateDailyTaskParameters(int days, int atHour, int atMinute) |
| 122 | + { |
| 123 | + ValidateNotNegative(days, nameof(days)); |
| 124 | + ValidateNotNegative(atHour, nameof(atHour)); |
| 125 | + ValidateNotNegative(atMinute, nameof(atMinute)); |
| 126 | + |
| 127 | + if (days == 0) |
113 | 128 | {
|
114 |
| - RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.Execute(), cronExpression); |
| 129 | + throw new ArgumentOutOfRangeException(nameof(days), "Daily recurring tasks need to have an interval of at least 1 day."); |
115 | 130 | }
|
| 131 | + } |
116 | 132 |
|
117 |
| - private void RegisterAsyncRecurringTask<TTask>(string cronExpression) where TTask : IAsyncRecurringBackgroundTask |
| 133 | + private void ValidateHourlyTaskParameters(int hours, int minute) |
| 134 | + { |
| 135 | + ValidateNotNegative(hours, nameof(hours)); |
| 136 | + ValidateNotNegative(minute, nameof(minute)); |
| 137 | + |
| 138 | + if (hours == 0 && minute == 0) |
118 | 139 | {
|
119 |
| - RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.ExecuteAsync(), cronExpression); |
| 140 | + throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks need to have an interval of at least 1 minute."); |
120 | 141 | }
|
121 | 142 |
|
122 |
| - private void ValidateDailyTaskParameters(int days, int atHour, int atMinute) |
| 143 | + if (hours > 23) |
123 | 144 | {
|
124 |
| - ValidateNotNegative(days, nameof(days)); |
125 |
| - ValidateNotNegative(atHour, nameof(atHour)); |
126 |
| - ValidateNotNegative(atMinute, nameof(atMinute)); |
127 |
| - |
128 |
| - if (days == 0) |
129 |
| - { |
130 |
| - throw new ArgumentOutOfRangeException(nameof(days), "Daily recurring tasks need to have an interval of at least 1 day."); |
131 |
| - } |
| 145 | + throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval of 24 hours or more should be scheduled using a daily interval instead."); |
132 | 146 | }
|
133 | 147 |
|
134 |
| - private void ValidateHourlyTaskParameters(int hours, int minute) |
| 148 | + if (minute > 59) |
135 | 149 | {
|
136 |
| - ValidateNotNegative(hours, nameof(hours)); |
137 |
| - ValidateNotNegative(minute, nameof(minute)); |
138 |
| - |
139 |
| - if (hours == 0 && minute == 0) |
140 |
| - { |
141 |
| - throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks need to have an interval of at least 1 minute."); |
142 |
| - } |
143 |
| - |
144 |
| - if (hours > 23) |
145 |
| - { |
146 |
| - throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval of 24 hours or more should be scheduled using a daily interval instead."); |
147 |
| - } |
148 |
| - |
149 |
| - if (minute > 59) |
150 |
| - { |
151 |
| - throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval measured in hours and minutes cannot have an minute component greater than 59 minutes."); |
152 |
| - } |
| 150 | + throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval measured in hours and minutes cannot have an minute component greater than 59 minutes."); |
153 | 151 | }
|
| 152 | + } |
154 | 153 |
|
155 |
| - private void ValidateMinuteTaskParameters(int minutes) |
| 154 | + private void ValidateMinuteTaskParameters(int minutes) |
| 155 | + { |
| 156 | + ValidateNotNegative(minutes, nameof(minutes)); |
| 157 | + if (minutes == 0) |
156 | 158 | {
|
157 |
| - ValidateNotNegative(minutes, nameof(minutes)); |
158 |
| - if (minutes == 0) |
159 |
| - { |
160 |
| - throw new ArgumentOutOfRangeException(nameof(minutes), "Recurring tasks need to have an interval of at least 1 minute."); |
161 |
| - } |
| 159 | + throw new ArgumentOutOfRangeException(nameof(minutes), "Recurring tasks need to have an interval of at least 1 minute."); |
162 | 160 | }
|
| 161 | + } |
163 | 162 |
|
164 |
| - private void ValidateNotNegative(int number, string argumentName) |
| 163 | + private void ValidateNotNegative(int number, string argumentName) |
| 164 | + { |
| 165 | + if (number < 0) |
165 | 166 | {
|
166 |
| - if (number < 0) |
167 |
| - { |
168 |
| - throw new ArgumentOutOfRangeException(argumentName, "Recurring tasks cannot set the interval to negative numbers."); |
169 |
| - } |
| 167 | + throw new ArgumentOutOfRangeException(argumentName, "Recurring tasks cannot set the interval to negative numbers."); |
170 | 168 | }
|
171 | 169 | }
|
172 | 170 | }
|
0 commit comments