forked from fluentscheduler/FluentScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobManager.cs
539 lines (458 loc) · 17.4 KB
/
JobManager.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
namespace FluentScheduler
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Job manager that handles jobs execution.
/// </summary>
public static class JobManager
{
#region Internal fields
private const uint _maxTimerInterval = (uint)0xfffffffe;
private static bool _useUtc = false;
private static Timer _timer = new Timer(state => ScheduleJobs(), null, Timeout.Infinite, Timeout.Infinite);
private static ScheduleCollection _schedules = new ScheduleCollection();
private static readonly ISet<Tuple<Schedule, Task>> _running = new HashSet<Tuple<Schedule, Task>>();
internal static DateTime Now
{
get
{
return _useUtc ? DateTime.UtcNow : DateTime.Now;
}
}
#endregion
#region UTC
/// <summary>
/// Use UTC time rather than local time.
/// It's recommended to call this method before any other library interaction to avoid mixed dates.
/// </summary>
public static void UseUtcTime()
{
_useUtc = true;
}
#endregion
#region Job factory
private static IJobFactory _jobFactory;
/// <summary>
/// Job factory used by the job manager.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1044:PropertiesShouldNotBeWriteOnly",
Justification = "Doing that way to not break compatibility with older versions.")]
public static IJobFactory JobFactory
{
private get
{
return (_jobFactory = _jobFactory ?? new JobFactory());
}
set
{
_jobFactory = value;
}
}
internal static Action GetJobAction<T>() where T : IJob
{
return () =>
{
var job = JobFactory.GetJobInstance<T>();
try
{
job.Execute();
}
finally
{
DisposeIfNeeded(job);
}
};
}
internal static Action GetJobAction(IJob job)
{
return () =>
{
try
{
job.Execute();
}
finally
{
DisposeIfNeeded(job);
}
};
}
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",
Justification = "It is spelled correctly.")]
internal static Action GetJobAction(Func<IJob> jobFactory)
{
return () =>
{
var job = jobFactory();
if (job == null)
throw new InvalidOperationException("The given Func<IJob> returned null.");
try
{
job.Execute();
}
finally
{
DisposeIfNeeded(job);
}
};
}
private static void DisposeIfNeeded(IJob job)
{
var disposable = job as IDisposable;
if (disposable != null)
disposable.Dispose();
}
#endregion
#region Event handling
/// <summary>
/// Event raised when an exception occurs in a job.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event Action<JobExceptionInfo> JobException;
/// <summary>
/// Event raised when a job starts.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event Action<JobStartInfo> JobStart;
/// <summary>
/// Evemt raised when a job ends.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event Action<JobEndInfo> JobEnd;
#endregion
#region Start, stop & initialize
/// <summary>
/// Initializes the job manager with the jobs to run and starts it.
/// </summary>
/// <param name="registries">Registries of jobs to run</param>
public static void Initialize(params Registry[] registries)
{
InitializeWithoutStarting(registries);
Start();
}
/// <summary>
/// Initializes the job manager with the jobs without starting it.
/// </summary>
/// <param name="registries">Registries of jobs to run</param>
public static void InitializeWithoutStarting(params Registry[] registries)
{
if (registries == null)
throw new ArgumentNullException("registries");
CalculateNextRun(registries.SelectMany(r => r.Schedules)).ToList().ForEach(RunJob);
}
/// <summary>
/// Starts the job manager.
/// </summary>
public static void Start()
{
ScheduleJobs();
}
/// <summary>
/// Stops the job manager.
/// </summary>
public static void Stop()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
/// <summary>
/// Stops the job manager and blocks until all running schedules finishes.
/// </summary>
public static void StopAndBlock()
{
Stop();
var tasks = new Task[0];
// Even though Stop() was just called, a scheduling may be happening right now, that's why the loop.
// Simply waiting for the tasks inside the lock causes a deadlock (a task may try to remove itself from
// running, but it can't access the collection, it's blocked by the wait).
do
{
lock (_running)
{
tasks = _running.Select(t => t.Item2).ToArray();
}
Task.WaitAll(tasks);
} while (tasks.Any());
}
#endregion
#region Exposing schedules
/// <summary>
/// Returns the schedule of the given name.
/// </summary>
/// <param name="name">Name of the schedule.</param>
/// <returns>The schedule of the given name, if any.</returns>
public static Schedule GetSchedule(string name)
{
return _schedules.Get(name);
}
/// <summary>
/// Collection of the currently running schedules.
/// </summary>
public static IEnumerable<Schedule> RunningSchedules
{
get
{
lock (_running)
{
return _running.Select(t => t.Item1).ToList();
}
}
}
/// <summary>
/// Collection of all schedules.
/// </summary>
public static IEnumerable<Schedule> AllSchedules
{
get
{
// returning a shallow copy
return _schedules.All().ToList();
}
}
#endregion
#region Exposing adding & removing jobs (without the registry)
/// <summary>
/// Adds a job schedule to the job manager.
/// </summary>
/// <param name="job">Job to run.</param>
/// <param name="schedule">Job schedule to add.</param>
public static void AddJob(Action job, Action<Schedule> schedule)
{
if (job == null)
throw new ArgumentNullException("job");
if (schedule == null)
throw new ArgumentNullException("schedule");
AddJob(schedule, new Schedule(job));
}
/// <summary>
/// Adds a job schedule to the job manager.
/// </summary>
/// <param name="job">Job to run.</param>
/// <param name="schedule">Job schedule to add.</param>
public static void AddJob(IJob job, Action<Schedule> schedule)
{
if (job == null)
throw new ArgumentNullException("job");
if (schedule == null)
throw new ArgumentNullException("schedule");
AddJob(schedule, new Schedule(JobManager.GetJobAction(job)));
}
/// <summary>
/// Adds a job schedule to the job manager.
/// </summary>
/// <typeparam name="T">Job to run.</typeparam>
/// <param name="schedule">Job schedule to add.</param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
Justification = "The 'T' requirement is on purpose.")]
public static void AddJob<T>(Action<Schedule> schedule) where T : IJob
{
if (schedule == null)
throw new ArgumentNullException("schedule");
AddJob(schedule, new Schedule(JobManager.GetJobAction<T>()) { Name = typeof(T).Name });
}
private static void AddJob(Action<Schedule> jobSchedule, Schedule schedule)
{
jobSchedule(schedule);
CalculateNextRun(new Schedule[] { schedule }).ToList().ForEach(RunJob);
ScheduleJobs();
}
/// <summary>
/// Removes the schedule of the given name.
/// </summary>
/// <param name="name">Name of the schedule.</param>
public static void RemoveJob(string name)
{
_schedules.Remove(name);
}
/// <summary>
/// Removes all schedules.
/// </summary>
public static void RemoveAllJobs()
{
_schedules.RemoveAll();
}
#endregion
#region Calculating, scheduling & running
private static IEnumerable<Schedule> CalculateNextRun(IEnumerable<Schedule> schedules)
{
foreach (var schedule in schedules)
{
if (schedule.CalculateNextRun == null)
{
if (schedule.DelayRunFor > TimeSpan.Zero)
{
// delayed job
schedule.NextRun = Now.Add(schedule.DelayRunFor);
_schedules.Add(schedule);
}
else
{
// run immediately
yield return schedule;
}
var hasAdded = false;
foreach (var child in schedule.AdditionalSchedules.Where(x => x.CalculateNextRun != null))
{
var nextRun = child.CalculateNextRun(Now.Add(child.DelayRunFor).AddMilliseconds(1));
if (!hasAdded || schedule.NextRun > nextRun)
{
schedule.NextRun = nextRun;
hasAdded = true;
}
}
}
else
{
schedule.NextRun = schedule.CalculateNextRun(Now.Add(schedule.DelayRunFor));
_schedules.Add(schedule);
}
foreach (var childSchedule in schedule.AdditionalSchedules)
{
if (childSchedule.CalculateNextRun == null)
{
if (childSchedule.DelayRunFor > TimeSpan.Zero)
{
// delayed job
childSchedule.NextRun = Now.Add(childSchedule.DelayRunFor);
_schedules.Add(childSchedule);
}
else
{
// run immediately
yield return childSchedule;
continue;
}
}
else
{
childSchedule.NextRun = childSchedule.CalculateNextRun(Now.Add(childSchedule.DelayRunFor));
_schedules.Add(childSchedule);
}
}
}
}
private static void ScheduleJobs()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_schedules.Sort();
if (!_schedules.Any())
return;
var firstJob = _schedules.First();
if (firstJob.NextRun <= Now)
{
RunJob(firstJob);
if (firstJob.CalculateNextRun == null)
{
// probably a ToRunNow().DelayFor() job, there's no CalculateNextRun
}
else
{
firstJob.NextRun = firstJob.CalculateNextRun(Now.AddMilliseconds(1));
}
if (firstJob.NextRun <= Now || firstJob.PendingRunOnce)
{
_schedules.Remove(firstJob);
}
firstJob.PendingRunOnce = false;
ScheduleJobs();
return;
}
var interval = firstJob.NextRun - Now;
if (interval <= TimeSpan.Zero)
{
ScheduleJobs();
return;
}
else
{
if (interval.TotalMilliseconds > _maxTimerInterval)
interval = TimeSpan.FromMilliseconds(_maxTimerInterval);
_timer.Change(interval, interval);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "That's OK since we're raising the JobException event with it."),
SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "https://blogs.msdn.microsoft.com/pfxteam/2012/03/25/do-i-need-to-dispose-of-tasks/")]
internal static void RunJob(Schedule schedule)
{
if (schedule.Disabled)
return;
lock (_running)
{
if (schedule.Reentrant != null &&
_running.Any(t => ReferenceEquals(t.Item1.Reentrant, schedule.Reentrant)))
return;
}
Tuple<Schedule, Task> tuple = null;
var task = new Task(() =>
{
var start = Now;
if (JobStart != null)
{
JobStart(
new JobStartInfo
{
Name = schedule.Name,
StartTime = start,
}
);
}
var stopwatch = new Stopwatch();
try
{
stopwatch.Start();
schedule.Jobs.ForEach(action => Task.Factory.StartNew(action).Wait());
}
catch (Exception e)
{
if (JobException != null)
{
var aggregate = e as AggregateException;
if (aggregate != null && aggregate.InnerExceptions.Count == 1)
e = aggregate.InnerExceptions.Single();
JobException(
new JobExceptionInfo
{
Name = schedule.Name,
Exception = e,
}
);
}
}
finally
{
lock (_running)
{
_running.Remove(tuple);
}
if (JobEnd != null)
{
JobEnd(
new JobEndInfo
{
Name = schedule.Name,
StartTime = start,
Duration = stopwatch.Elapsed,
NextRun = schedule.NextRun,
}
);
}
}
}, TaskCreationOptions.PreferFairness);
tuple = new Tuple<Schedule, Task>(schedule, task);
lock (_running)
{
_running.Add(tuple);
}
task.Start();
}
#endregion
}
}