-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
CronScheduleBuilder.cs
321 lines (285 loc) · 12.9 KB
/
CronScheduleBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#region License
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
#endregion
using System;
using Quartz.Impl.Triggers;
using Quartz.Spi;
namespace Quartz
{
/// <summary>
/// CronScheduleBuilder is a <see cref="IScheduleBuilder" /> that defines
/// <see cref="CronExpression" />-based schedules for <see cref="ITrigger" />s.
/// </summary>
/// <remarks>
/// <para>
/// Quartz provides a builder-style API for constructing scheduling-related
/// entities via a Domain-Specific Language (DSL). The DSL can best be
/// utilized through the usage of static imports of the methods on the classes
/// <see cref="TriggerBuilder" />, <see cref="JobBuilder" />,
/// <see cref="DateBuilder" />, <see cref="JobKey" />, <see cref="TriggerKey" />
/// and the various <see cref="IScheduleBuilder" /> implementations.
/// </para>
/// <para>
/// Client code can then use the DSL to write code such as this:
/// </para>
/// <code>
/// IJobDetail job = JobBuilder.Create<MyJob>()
/// .WithIdentity("myJob")
/// .Build();
/// ITrigger trigger = newTrigger()
/// .WithIdentity(triggerKey("myTrigger", "myTriggerGroup"))
/// .WithSimpleSchedule(x => x.WithIntervalInHours(1).RepeatForever())
/// .StartAt(DateBuilder.FutureDate(10, IntervalUnit.Minute))
/// .Build();
/// scheduler.scheduleJob(job, trigger);
/// </code>
/// </remarks>
/// <seealso cref="CronExpression" />
/// <seealso cref="ICronTrigger" />
/// <seealso cref="IScheduleBuilder" />
/// <seealso cref="SimpleScheduleBuilder" />
/// <seealso cref="CalendarIntervalScheduleBuilder" />
/// <seealso cref="TriggerBuilder" />
public class CronScheduleBuilder : ScheduleBuilder<ICronTrigger>
{
private readonly CronExpression cronExpression;
private int misfireInstruction = MisfireInstruction.SmartPolicy;
protected CronScheduleBuilder(CronExpression cronExpression)
{
if (cronExpression == null)
{
throw new ArgumentNullException("cronExpression", "cronExpression cannot be null");
}
this.cronExpression = cronExpression;
}
/// <summary>
/// Build the actual Trigger -- NOT intended to be invoked by end users,
/// but will rather be invoked by a TriggerBuilder which this
/// ScheduleBuilder is given to.
/// </summary>
/// <seealso cref="TriggerBuilder.WithSchedule" />
public override IMutableTrigger Build()
{
CronTriggerImpl ct = new CronTriggerImpl();
ct.CronExpression = cronExpression;
ct.TimeZone = cronExpression.TimeZone;
ct.MisfireInstruction = misfireInstruction;
return ct;
}
/// <summary>
/// Create a CronScheduleBuilder with the given cron-expression - which
/// is presumed to b e valid cron expression (and hence only a RuntimeException
/// will be thrown if it is not).
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="cronExpression">the cron expression to base the schedule on.</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder CronSchedule(string cronExpression)
{
CronExpression.ValidateExpression(cronExpression);
return CronScheduleNoParseException(cronExpression);
}
/// <summary>
/// Create a CronScheduleBuilder with the given cron-expression string - which
/// may not be a valid cron expression (and hence a ParseException will be thrown
/// f it is not).
/// </summary>
/// <param name="presumedValidCronExpression">the cron expression string to base the schedule on</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
private static CronScheduleBuilder CronScheduleNoParseException(String presumedValidCronExpression)
{
try
{
return CronSchedule(new CronExpression(presumedValidCronExpression));
}
catch (FormatException e)
{
// all methods of construction ensure the expression is valid by this point...
throw new Exception("CronExpression '" + presumedValidCronExpression +
"' is invalid, which should not be possible, please report bug to Quartz developers.", e);
}
}
/// <summary>
/// Create a CronScheduleBuilder with the given cron-expression.
/// </summary>
/// <param name="cronExpression">the cron expression to base the schedule on.</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder CronSchedule(CronExpression cronExpression)
{
return new CronScheduleBuilder(cronExpression);
}
/// <summary>
/// Create a CronScheduleBuilder with a cron-expression that sets the
/// schedule to fire every day at the given time (hour and minute).
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="hour">the hour of day to fire</param>
/// <param name="minute">the minute of the given hour to fire</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder DailyAtHourAndMinute(int hour, int minute)
{
DateBuilder.ValidateHour(hour);
DateBuilder.ValidateMinute(minute);
string cronExpression = String.Format("0 {0} {1} ? * *", minute, hour);
return CronScheduleNoParseException(cronExpression);
}
/// <summary>
/// Create a CronScheduleBuilder with a cron-expression that sets the
/// schedule to fire at the given day at the given time (hour and minute) on the given days of the week.
/// </summary>
/// <param name="hour">the hour of day to fire</param>
/// <param name="minute">the minute of the given hour to fire</param>
/// <param name="daysOfWeek">the days of the week to fire</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder AtHourAndMinuteOnGivenDaysOfWeek(int hour, int minute, params DayOfWeek[] daysOfWeek)
{
if (daysOfWeek == null || daysOfWeek.Length == 0)
{
throw new ArgumentException("You must specify at least one day of week.");
}
DateBuilder.ValidateHour(hour);
DateBuilder.ValidateMinute(minute);
string cronExpression = string.Format("0 {0} {1} ? * {2}", minute, hour, ((int) daysOfWeek[0]) + 1);
for (int i = 1; i < daysOfWeek.Length; i++)
{
cronExpression = cronExpression + "," + (((int) daysOfWeek[i]) + 1);
}
return CronScheduleNoParseException(cronExpression);
}
/// <summary>
/// Create a CronScheduleBuilder with a cron-expression that sets the
/// schedule to fire one per week on the given day at the given time
/// (hour and minute).
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="dayOfWeek">the day of the week to fire</param>
/// <param name="hour">the hour of day to fire</param>
/// <param name="minute">the minute of the given hour to fire</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder WeeklyOnDayAndHourAndMinute(DayOfWeek dayOfWeek, int hour, int minute)
{
DateBuilder.ValidateHour(hour);
DateBuilder.ValidateMinute(minute);
string cronExpression = string.Format("0 {0} {1} ? * {2}", minute, hour, ((int) dayOfWeek) + 1);
return CronScheduleNoParseException(cronExpression);
}
/// <summary>
/// Create a CronScheduleBuilder with a cron-expression that sets the
/// schedule to fire one per month on the given day of month at the given
/// time (hour and minute).
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="dayOfMonth">the day of the month to fire</param>
/// <param name="hour">the hour of day to fire</param>
/// <param name="minute">the minute of the given hour to fire</param>
/// <returns>the new CronScheduleBuilder</returns>
/// <seealso cref="CronExpression" />
public static CronScheduleBuilder MonthlyOnDayAndHourAndMinute(int dayOfMonth, int hour, int minute)
{
DateBuilder.ValidateDayOfMonth(dayOfMonth);
DateBuilder.ValidateHour(hour);
DateBuilder.ValidateMinute(minute);
string cronExpression = String.Format("0 {0} {1} {2} * ?", minute, hour, dayOfMonth);
return CronScheduleNoParseException(cronExpression);
}
/// <summary>
/// The <see cref="TimeZoneInfo" /> in which to base the schedule.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="tz">the time-zone for the schedule.</param>
/// <returns>the updated CronScheduleBuilder</returns>
/// <seealso cref="CronExpression.TimeZone" />
public CronScheduleBuilder InTimeZone(TimeZoneInfo tz)
{
cronExpression.TimeZone = tz;
return this;
}
/// <summary>
/// If the Trigger misfires, use the
/// <see cref="MisfireInstruction.IgnoreMisfirePolicy" /> instruction.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>the updated CronScheduleBuilder</returns>
/// <seealso cref="MisfireInstruction.IgnoreMisfirePolicy" />
public CronScheduleBuilder WithMisfireHandlingInstructionIgnoreMisfires()
{
misfireInstruction = MisfireInstruction.IgnoreMisfirePolicy;
return this;
}
/// <summary>
/// If the Trigger misfires, use the <see cref="MisfireInstruction.CronTrigger.DoNothing" />
/// instruction.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>the updated CronScheduleBuilder</returns>
/// <seealso cref="MisfireInstruction.CronTrigger.DoNothing" />
public CronScheduleBuilder WithMisfireHandlingInstructionDoNothing()
{
misfireInstruction = MisfireInstruction.CronTrigger.DoNothing;
return this;
}
/// <summary>
/// If the Trigger misfires, use the <see cref="MisfireInstruction.CronTrigger.FireOnceNow" />
/// instruction.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>the updated CronScheduleBuilder</returns>
/// <seealso cref="MisfireInstruction.CronTrigger.FireOnceNow" />
public CronScheduleBuilder WithMisfireHandlingInstructionFireAndProceed()
{
misfireInstruction = MisfireInstruction.CronTrigger.FireOnceNow;
return this;
}
internal CronScheduleBuilder WithMisfireHandlingInstruction(int readMisfireInstructionFromString)
{
misfireInstruction = readMisfireInstructionFromString;
return this;
}
}
/// <summary>
/// Extension methods that attach <see cref="CronScheduleBuilder" /> to <see cref="TriggerBuilder" />.
/// </summary>
public static class CronScheduleTriggerBuilderExtensions
{
public static TriggerBuilder WithCronSchedule(this TriggerBuilder triggerBuilder, string cronExpression)
{
CronScheduleBuilder builder = CronScheduleBuilder.CronSchedule(cronExpression);
return triggerBuilder.WithSchedule(builder);
}
public static TriggerBuilder WithCronSchedule(this TriggerBuilder triggerBuilder, string cronExpression, Action<CronScheduleBuilder> action)
{
CronScheduleBuilder builder = CronScheduleBuilder.CronSchedule(cronExpression);
action(builder);
return triggerBuilder.WithSchedule(builder);
}
}
}