forked from fluentscheduler/FluentScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cs
103 lines (87 loc) · 3.02 KB
/
Registry.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
namespace FluentScheduler
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// A registry of job schedules.
/// </summary>
public class Registry
{
private bool _allJobsConfiguredAsNonReentrant;
internal List<Schedule> Schedules { get; private set; }
/// <summary>
/// Default ctor.
/// </summary>
public Registry()
{
_allJobsConfiguredAsNonReentrant = false;
Schedules = new List<Schedule>();
}
/// <summary>
/// Sets all jobs in this schedule as non reentrant.
/// </summary>
public void NonReentrantAsDefault()
{
_allJobsConfiguredAsNonReentrant = true;
lock (((ICollection)Schedules).SyncRoot)
{
foreach (var schedule in Schedules)
schedule.NonReentrant();
}
}
/// <summary>
/// Schedules a new job in the registry.
/// </summary>
/// <param name="job">Job to run.</param>
public Schedule Schedule(Action job)
{
if (job == null)
throw new ArgumentNullException("job");
return Schedule(job, null);
}
/// <summary>
/// Schedules a new job in the registry.
/// </summary>
/// <param name="job">Job to run.</param>
public Schedule Schedule(IJob job)
{
if (job == null)
throw new ArgumentNullException("job");
return Schedule(JobManager.GetJobAction(job), null);
}
/// <summary>
/// Schedules a new job in the registry.
/// </summary>
/// <typeparam name="T">Job to schedule.</typeparam>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
Justification = "The 'T' requirement is on purpose.")]
public Schedule Schedule<T>() where T : IJob
{
return Schedule(JobManager.GetJobAction<T>(), typeof(T).Name);
}
/// <summary>
/// Schedules a new job in the registry.
/// </summary>
/// <param name="job">Factory method creating a IJob instance to run.</param>
public Schedule Schedule(Func<IJob> job)
{
if (job == null)
throw new ArgumentNullException("job");
return Schedule(JobManager.GetJobAction(job), null);
}
private Schedule Schedule(Action action, string name)
{
var schedule = new Schedule(action);
if (_allJobsConfiguredAsNonReentrant)
schedule.NonReentrant();
lock (((ICollection)Schedules).SyncRoot)
{
Schedules.Add(schedule);
}
schedule.Name = name;
return schedule;
}
}
}