-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobInfo.cs
161 lines (130 loc) · 4.25 KB
/
JobInfo.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Puenktlich
{
public class JobInfo<T>
{
private readonly IJobRegistration<T> _job;
private readonly Scheduler _scheduler;
private readonly TriggerCollection _triggerCollection;
internal JobInfo(IJobRegistration<T> job, Scheduler scheduler)
{
_job = job;
_scheduler = scheduler;
_triggerCollection = new TriggerCollection((IJobRegistration<object>) _job, scheduler);
}
public bool IsRunning
{
get { return _job.IsRunning; }
}
public bool IsPaused
{
get { return _job.IsPaused; }
}
public TriggerCollection Triggers
{
get { return _triggerCollection; }
}
public DateTimeOffset? LastActualFireTime
{
get
{
if (_job.ExecutionContext.ActualFireTime > DateTimeOffset.MinValue)
{
return _job.ExecutionContext.ActualFireTime;
}
return null;
}
}
public DateTimeOffset? NextScheduledFireTime
{
get
{
if (_job.ExecutionContext.ScheduledFireTime < DateTimeOffset.MaxValue)
{
return _job.ExecutionContext.ScheduledFireTime;
}
return null;
}
}
public T Data
{
get { return _job.ExecutionContext.Data; }
}
public void Pause()
{
IJobRegistration<T> job = _scheduler.GetJob(Data);
job.IsPaused = true;
lock (job.TimerLock)
{
if (job.Timer == null) throw new ObjectDisposedException("Job");
job.Timer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
public void Resume()
{
IJobRegistration<T> job = _scheduler.GetJob(Data);
job.IsPaused = false;
RefreshJob(job);
}
private void RefreshJob(IJobRegistration<T> job)
{
_scheduler.RefreshJob((IJobRegistration<object>) job);
}
public class TriggerCollection : IEnumerable<ITrigger>
{
private readonly IJobRegistration<object> _job;
private readonly Scheduler _scheduler;
internal TriggerCollection(IJobRegistration<object> job, Scheduler scheduler)
{
_job = job;
_scheduler = scheduler;
}
public IEnumerator<ITrigger> GetEnumerator()
{
return GetSnapshot().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(ITrigger trigger)
{
IJobRegistration<object> job = _scheduler.GetJob(_job.ExecutionContext.Data);
lock (job.TriggersLock)
{
job.Triggers.Add(trigger);
}
_scheduler.RefreshJob(job);
}
public void Remove(ITrigger trigger)
{
IJobRegistration<object> job = _scheduler.GetJob(_job.ExecutionContext.Data);
lock (job.TriggersLock)
{
job.Triggers.Remove(trigger);
}
_scheduler.RefreshJob(job);
}
public void Clear()
{
IJobRegistration<object> job = _scheduler.GetJob(_job.ExecutionContext.Data);
lock (job.TriggersLock)
{
job.Triggers.Clear();
}
_scheduler.RefreshJob(job);
}
private List<ITrigger> GetSnapshot()
{
lock (_job.TriggersLock)
{
return _job.Triggers.ToList();
}
}
}
}
}