forked from Dawn-of-Light/DOLSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleScheduler.cs
352 lines (308 loc) · 8.81 KB
/
SimpleScheduler.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
/*
* DAWN OF LIGHT - The first free open source DAoC server emulator
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
using System;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Timers;
using log4net;
namespace DOL.GS.Scheduler
{
/// <summary>
/// Simple Timer Based Scheduler Running Tasks Concurrently.
/// </summary>
public class SimpleScheduler
{
/// <summary>
/// Defines a logger for this class.
/// </summary>
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Retrieve Current Timestamp used by Scheduler
/// </summary>
public static long Ticks { get { return Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000); } }
/// <summary>
/// Internal Timer Object
/// </summary>
private Timer Timer { get; set; }
/// <summary>
/// Timers Pending for Schedule.
/// </summary>
private ConcurrentBag<Tuple<long, ScheduledTask>> PendingTimers { get; set; }
/// <summary>
/// Scheduled Timers Ordered by Due Time
/// </summary>
private SortedDictionary<long, List<ScheduledTask>> ScheduledTimers { get; set; }
/// <summary>
/// UnScheduled Tasks Currently Running.
/// </summary>
private List<Tuple<long, Task>> RunningTimers { get; set; }
/// <summary>
/// Collection Locking Object
/// </summary>
private readonly object SchedulerLock = new object();
/// <summary>
/// Next Timer Tick Due Time
/// </summary>
private long NextDueTick = long.MaxValue;
/// <summary>
/// Create a new Instance of <see cref="SimpleScheduler"/>
/// </summary>
public SimpleScheduler()
{
PendingTimers = new ConcurrentBag<Tuple<long, ScheduledTask>>();
ScheduledTimers = new SortedDictionary<long, List<ScheduledTask>>();
RunningTimers = new List<Tuple<long, Task>>();
Timer = new Timer();
Timer.Interval = 1;
Timer.AutoReset = false;
Timer.Elapsed += OnTick;
Timer.Enabled = true;
}
/// <summary>
/// Timer Event Starting All Due Tasks
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void OnTick(object source, ElapsedEventArgs e)
{
System.Threading.Interlocked.Exchange(ref NextDueTick, long.MaxValue);
var nextDueTime = long.MaxValue;
lock (SchedulerLock)
{
ResolveRunningTimers();
ResolvePendingTimers();
ConsumePendingScheduledTasks();
// Schedule Next Tick
var nextTask = ScheduledTimers.FirstOrDefault();
if (nextTask.Value != null)
nextDueTime = nextTask.Key;
}
SignalNextDueTick(nextDueTime);
}
/// <summary>
/// Start All Tasks
/// </summary>
private void ConsumePendingScheduledTasks()
{
var StartedTicksIndex = new List<long>();
foreach (KeyValuePair<long, List<ScheduledTask>> tasks in ScheduledTimers.Where(ent => ent.Key <= Ticks))
{
StartedTicksIndex.Add(tasks.Key);
foreach (ScheduledTask taskEntry in tasks.Value)
{
// scope copy for thread safety
var timerTask = taskEntry;
var task = Task.Factory.StartNew(() => LaunchScheduledTask(timerTask));
RunningTimers.Add(new Tuple<long, Task>(Ticks, task));
}
}
foreach (var key in StartedTicksIndex)
ScheduledTimers.Remove(key);
}
/// <summary>
/// Start a Task Method
/// </summary>
/// <param name="task"></param>
private void LaunchScheduledTask(ScheduledTask task)
{
int delay = 0;
var start = Ticks;
try
{
delay = task.Run();
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error("Exception In Simple Scheduler Task: ", ex);
}
if (log.IsWarnEnabled)
{
var runningTime = Ticks - start;
if (runningTime > 500)
log.WarnFormat("Simple Scheduler Task (interval:{1}) execution took {0}ms! ({2}.{3})", runningTime, delay, task.Method.GetMethodInfo().ReflectedType, task.Method.GetMethodInfo().Name);
}
if (delay > 0 && task.Active)
Start(task, delay);
}
/// <summary>
/// Resolve Pending Timer to be Inserted in Scheduler
/// </summary>
private void ResolvePendingTimers()
{
while (!PendingTimers.IsEmpty)
{
Tuple<long, ScheduledTask> task;
if (PendingTimers.TryTake(out task))
{
List<ScheduledTask> scheduled;
if (!ScheduledTimers.TryGetValue(task.Item1, out scheduled))
{
scheduled = new List<ScheduledTask>();
ScheduledTimers.Add(task.Item1, scheduled);
}
scheduled.Add(task.Item2);
}
}
}
/// <summary>
/// Resolve Running Tasks and Clean Up Timers
/// </summary>
private void ResolveRunningTimers()
{
foreach(var task in RunningTimers.ToArray())
{
if (task.Item2.IsFaulted)
{
if (log.IsErrorEnabled)
log.Error("Exception in Simple Scheduler Faulted Task: ", task.Item2.Exception);
RunningTimers.Remove(task);
}
else if (task.Item2.IsCompleted || task.Item2.IsCanceled)
{
RunningTimers.Remove(task);
}
}
}
/// <summary>
/// Compare Next Due Time to Value and Update Timer Interval accordingly
/// </summary>
/// <param name="value"></param>
private void SignalNextDueTick(long value)
{
long initialValue;
do
{
initialValue = System.Threading.Interlocked.Read(ref NextDueTick);
if (value >= initialValue) return;
}
while (System.Threading.Interlocked.CompareExchange(ref NextDueTick, value, initialValue) != initialValue);
Timer.Interval = Math.Min(int.MaxValue, Math.Max(1, value - Ticks));
}
#region Scheduler API
/// <summary>
/// Trigger a Scheduled Method to be run withing given delay
/// </summary>
/// <param name="method">Method to be scheduled, return next interval or 0 for single run</param>
/// <param name="delay">delay in ms for the Method to be started</param>
/// <returns>The Scheduled Task Object</returns>
public ScheduledTask Start(Func<int> method, int delay)
{
if (delay < 1)
throw new ArgumentException("Task Delay should be greater than 0.", "delay");
var task = new ScheduledTask(method);
Start(task, delay);
return task;
}
/// <summary>
/// Add or Re Schedule Task Object
/// </summary>
/// <param name="task">Scheduled Task Object to Add</param>
/// <param name="delay">Delay before Scheduled Task is triggered</param>
private void Start(ScheduledTask task, int delay)
{
var dueTime = Ticks + delay;
PendingTimers.Add(new Tuple<long, ScheduledTask>(dueTime, task));
SignalNextDueTick(dueTime);
}
/// <summary>
/// Shutdown the Scheduler
/// </summary>
public void Shutdown()
{
System.Threading.Interlocked.Exchange(ref NextDueTick, long.MinValue);
Timer.Interval = int.MaxValue;
Timer.Enabled = false;
foreach (var task in RunningTimers)
task.Item2.Wait();
}
#endregion
}
/// <summary>
/// Reference Class for Holding a Task Method
/// </summary>
public sealed class ScheduledTask
{
/// <summary>
/// Task's Method Reference
/// </summary>
internal Func<int> Method { get; private set; }
/// <summary>
/// Lock Object Synchronzing Stopping Task
/// </summary>
private readonly object LockObject = new object();
/// <summary>
/// Task Active Flag
/// </summary>
private bool m_active = true;
/// <summary>
/// Is This Task still Active ?
/// </summary>
public bool Active
{
get
{
lock (LockObject)
return m_active;
}
}
/// <summary>
/// Create a new Instance of <see cref="ScheduledTask"/>
/// </summary>
/// <param name="Method"></param>
internal ScheduledTask(Func<int> Method)
{
this.Method = Method;
}
/// <summary>
/// Stop and Unschedule Task
/// Lock and Wait for Task End if currently Running.
/// </summary>
public void Stop()
{
lock(LockObject)
m_active = false;
}
/// <summary>
/// Run Task Synchronously Locked
/// </summary>
/// <returns></returns>
internal int Run()
{
var result = 0;
lock(LockObject)
{
try
{
if (Active)
result = Method();
}
finally
{
m_active &= result > 0;
}
}
return result;
}
}
}