-
-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathScheduledEvent.cs
More file actions
426 lines (362 loc) · 13.7 KB
/
ScheduledEvent.cs
File metadata and controls
426 lines (362 loc) · 13.7 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Threading;
using System.Threading.Tasks;
using Coravel.Invocable;
using Coravel.Scheduling.Schedule.Cron;
using Coravel.Scheduling.Schedule.Interfaces;
using Coravel.Tasks;
using Coravel.Scheduling.Schedule.Zoned;
using Microsoft.Extensions.DependencyInjection;
namespace Coravel.Scheduling.Schedule.Event
{
public class ScheduledEvent : IScheduleInterval, IScheduledEventConfiguration, IGetScheduleInfo
{
private CronExpression _expression;
private ActionOrAsyncFunc _scheduledAction;
private Type _invocableType = null;
private bool _preventOverlapping = false;
private string _eventUniqueId = null;
private IServiceScopeFactory _scopeFactory;
private Func<Task<bool>> _whenPredicate;
private bool _isScheduledPerSecond = false;
private int? _secondsInterval = null;
private object[] _constructorParameters = null;
private ZonedTime _zonedTime = ZonedTime.AsUTC();
private bool _runOnceAtStart = false;
private bool _runOnce = false;
private bool _wasPreviouslyRun = false;
public ScheduledEvent(Action scheduledAction, IServiceScopeFactory scopeFactory) : this(scopeFactory)
{
this._scheduledAction = new ActionOrAsyncFunc(scheduledAction);
}
public ScheduledEvent(Func<Task> scheduledAsyncTask, IServiceScopeFactory scopeFactory) : this(scopeFactory)
{
this._scheduledAction = new ActionOrAsyncFunc(scheduledAsyncTask);
}
private ScheduledEvent(IServiceScopeFactory scopeFactory)
{
this._scopeFactory = scopeFactory;
this._eventUniqueId = Guid.NewGuid().ToString();
}
public static ScheduledEvent WithInvocable<T>(IServiceScopeFactory scopeFactory) where T : IInvocable
{
return WithInvocableType(typeof(T), scopeFactory);
}
internal static ScheduledEvent WithInvocableAndParams<T>(IServiceScopeFactory scopeFactory, object[] parameters)
where T : IInvocable
{
var scheduledEvent = WithInvocableType(typeof(T), scopeFactory);
scheduledEvent._constructorParameters = parameters;
return scheduledEvent;
}
internal static ScheduledEvent WithInvocableAndParams(Type invocableType, IServiceScopeFactory scopeFactory, object[] parameters)
{
if (!typeof(IInvocable).IsAssignableFrom(invocableType))
{
throw new ArgumentException(
$"When using {nameof(IScheduler.ScheduleWithParams)}() you must supply a type that inherits from {nameof(IInvocable)}.",
nameof(invocableType));
}
var scheduledEvent = WithInvocableType(invocableType, scopeFactory);
scheduledEvent._constructorParameters = parameters;
return scheduledEvent;
}
public static ScheduledEvent WithInvocableType(Type invocableType, IServiceScopeFactory scopeFactory)
{
var scheduledEvent = new ScheduledEvent(scopeFactory);
scheduledEvent._invocableType = invocableType;
return scheduledEvent;
}
private static readonly int _OneMinuteAsSeconds = 60;
public bool IsDue(DateTime utcNow)
{
var zonedNow = this._zonedTime.Convert(utcNow);
if (this._isScheduledPerSecond)
{
var isSecondDue = this.IsSecondsDue(zonedNow);
var isWeekDayDue = this._expression.IsWeekDayDue(zonedNow);
return isSecondDue && isWeekDayDue;
}
else
{
return this._expression.IsDue(zonedNow);
}
}
public async Task InvokeScheduledEvent(CancellationToken cancellationToken)
{
if (await WhenPredicateFails())
{
return;
}
if (this._invocableType is null)
{
await this._scheduledAction.Invoke();
}
else
{
await using AsyncServiceScope scope = new(this._scopeFactory.CreateAsyncScope());
if (GetInvocable(scope.ServiceProvider) is IInvocable invocable)
{
if (invocable is ICancellableInvocable cancellableInvokable)
{
cancellableInvokable.CancellationToken = cancellationToken;
}
await invocable.Invoke();
}
}
MarkedAsExecutedOnce();
UnScheduleIfWarranted();
}
public bool ShouldPreventOverlapping() => this._preventOverlapping;
public string OverlappingUniqueIdentifier() => this._eventUniqueId;
public bool IsScheduledCronBasedTask() => !this._isScheduledPerSecond;
public ScheduleInfo GetScheduleInfo()
{
return new ScheduleInfo(
cronExpression: this._expression?.ToString() ?? string.Empty,
isScheduledPerSecond: this._isScheduledPerSecond,
secondsInterval: this._secondsInterval,
invocableType: this._invocableType,
preventOverlapping: this._preventOverlapping,
eventUniqueId: this._eventUniqueId,
hasWhenPredicates: this._whenPredicate != null,
zonedTimeZone: this._zonedTime?.TimeZoneInfo ?? TimeZoneInfo.Utc,
runOnceAtStart: this._runOnceAtStart,
runOnce: this._runOnce
);
}
public IScheduledEventConfiguration Daily()
{
this._expression = new CronExpression("00 00 * * *");
return this;
}
public IScheduledEventConfiguration DailyAtHour(int hour)
{
this._expression = new CronExpression($"00 {hour} * * *");
return this;
}
public IScheduledEventConfiguration DailyAt(int hour, int minute)
{
this._expression = new CronExpression($"{minute} {hour} * * *");
return this;
}
public IScheduledEventConfiguration Hourly()
{
this._expression = new CronExpression($"00 * * * *");
return this;
}
public IScheduledEventConfiguration HourlyAt(int minute)
{
this._expression = new CronExpression($"{minute} * * * *");
return this;
}
public IScheduledEventConfiguration EveryMinute()
{
this._expression = new CronExpression($"* * * * *");
return this;
}
public IScheduledEventConfiguration EveryFiveMinutes()
{
this._expression = new CronExpression($"*/5 * * * *");
return this;
}
public IScheduledEventConfiguration EveryTenMinutes()
{
// todo fix "*/10" in cron part
this._expression = new CronExpression($"*/10 * * * *");
return this;
}
public IScheduledEventConfiguration EveryFifteenMinutes()
{
this._expression = new CronExpression($"*/15 * * * *");
return this;
}
public IScheduledEventConfiguration EveryThirtyMinutes()
{
this._expression = new CronExpression($"*/30 * * * *");
return this;
}
public IScheduledEventConfiguration Weekly()
{
this._expression = new CronExpression($"00 00 * * 1");
return this;
}
public IScheduledEventConfiguration Monthly()
{
this._expression = new CronExpression($"00 00 1 * *");
return this;
}
public IScheduledEventConfiguration Cron(string cronExpression)
{
this._expression = new CronExpression(cronExpression);
return this;
}
public IScheduledEventConfiguration Monday()
{
this._expression.AppendWeekDay(DayOfWeek.Monday);
return this;
}
public IScheduledEventConfiguration Tuesday()
{
this._expression.AppendWeekDay(DayOfWeek.Tuesday);
return this;
}
public IScheduledEventConfiguration Wednesday()
{
this._expression.AppendWeekDay(DayOfWeek.Wednesday);
return this;
}
public IScheduledEventConfiguration Thursday()
{
this._expression.AppendWeekDay(DayOfWeek.Thursday);
return this;
}
public IScheduledEventConfiguration Friday()
{
this._expression.AppendWeekDay(DayOfWeek.Friday);
return this;
}
public IScheduledEventConfiguration Saturday()
{
this._expression.AppendWeekDay(DayOfWeek.Saturday);
return this;
}
public IScheduledEventConfiguration Sunday()
{
this._expression.AppendWeekDay(DayOfWeek.Sunday);
return this;
}
public IScheduledEventConfiguration Weekday()
{
this.Monday()
.Tuesday()
.Wednesday()
.Thursday()
.Friday();
return this;
}
public IScheduledEventConfiguration Weekend()
{
this.Saturday()
.Sunday();
return this;
}
public IScheduledEventConfiguration PreventOverlapping(string uniqueIdentifier)
{
this._preventOverlapping = true;
return this.AssignUniqueIndentifier(uniqueIdentifier);
}
public IScheduledEventConfiguration When(Func<Task<bool>> predicate)
{
this._whenPredicate = predicate;
return this;
}
public IScheduledEventConfiguration AssignUniqueIndentifier(string uniqueIdentifier)
{
this._eventUniqueId = uniqueIdentifier;
return this;
}
public Type InvocableType() => this._invocableType;
private async Task<bool> WhenPredicateFails()
{
return this._whenPredicate != null && (!await _whenPredicate.Invoke());
}
public IScheduledEventConfiguration EverySecond()
{
this._secondsInterval = 1;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration EveryFiveSeconds()
{
this._secondsInterval = 5;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration EveryTenSeconds()
{
this._secondsInterval = 10;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration EveryFifteenSeconds()
{
this._secondsInterval = 15;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration EveryThirtySeconds()
{
this._secondsInterval = 30;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration EverySeconds(int seconds)
{
if (seconds < 1 || seconds > 59)
{
throw new ArgumentException(
"When calling 'EverySeconds(int seconds)', 'seconds' must be between 0 and 60");
}
this._secondsInterval = seconds;
this._isScheduledPerSecond = true;
this._expression = new CronExpression("* * * * *");
return this;
}
public IScheduledEventConfiguration Zoned(TimeZoneInfo timeZoneInfo)
{
this._zonedTime = new ZonedTime(timeZoneInfo);
return this;
}
public IScheduledEventConfiguration RunOnceAtStart()
{
this._runOnceAtStart = true;
return this;
}
public IScheduledEventConfiguration Once()
{
this._runOnce = true;
return this;
}
private bool IsSecondsDue(DateTime utcNow)
{
if (utcNow.Second == 0)
{
return _OneMinuteAsSeconds % this._secondsInterval == 0;
}
else
{
return utcNow.Second % this._secondsInterval == 0;
}
}
internal bool ShouldRunOnceAtStart() => this._runOnceAtStart;
private object GetInvocable(IServiceProvider serviceProvider)
{
if (this._constructorParameters?.Length > 0)
{
return ActivatorUtilities.CreateInstance(serviceProvider, this._invocableType,
this._constructorParameters);
}
return serviceProvider.GetRequiredService(this._invocableType);
}
private bool PreviouslyRanAndMarkedToRunOnlyOnce() => this._runOnce && this._wasPreviouslyRun;
private void MarkedAsExecutedOnce()
{
this._wasPreviouslyRun = true;
}
private void UnScheduleIfWarranted()
{
if (PreviouslyRanAndMarkedToRunOnlyOnce())
{
using var scope = this._scopeFactory.CreateScope();
var scheduler = scope.ServiceProvider.GetService<IScheduler>() as Scheduler;
scheduler.TryUnschedule(this._eventUniqueId);
}
}
}
}