-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.linq
125 lines (107 loc) · 2.17 KB
/
test.linq
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
<Query Kind="Program" />
/*
Many multithreaded applications need timers to be maintained to handle thread and task scheduling.
These timers are system calls made to the OS, which keeps track of all the timer requests and
generates events to notify the applications. There can be several applications using this OS
feature at one point in time. Design this timer management component of the OS.
*/
void Main()
{
}
/*
class MinHeap<T>
{
void Add<T>(T n)
{
}
T Get<T>()
{
return null;
}
}
*/
class TimerData
{
public long NextEpoc;
public string Cron;
public Action Callback;
}
class CentralTimer
{
object TimerDataLock = new object();
MinHeap<Timers> Timers = new MinHeap<Timers>();
readonly ILogger<CentralTimer> Logger;
public CentralTimer(ILogger<CentralTimer> logger)
{
this.Logger = logger;
}
public void AddTimer(string cron, Action callback)
{
if(string.IsNullOrWhiteSpace(cron))
{
throw new ArgumentNullException(nameof(cron));
}
if(callback == null)
{
throw new ArgumentNullException(nameof(callback));
}
var newTd = new TimerData
{
NextEpoc = CalculateNextEpoc(cron),
Cron = cron,
Callback = callback
};
if(newTd.NextEpoc != -1)
{
lock (TimerDataLock)
{
Timers.Add(newTd);
}
}
else
{
throw new TimerException("Bad Timer.Timer will never execute");
}
}
// call this whenever OS Timer Runs
public void RunExpiredTimers()
{
try
{
lock(TimerDataLock)
{
var now = GetEpoc(DateTime.Now);
var peek = Timers.Peek();
while(peek != null && peek.NextEpoc > now)
{
var top = Timers.GetTop();
try
{
top?.Callback();
top.FailureCount = 0;
}
catch(Exception e)
{
this.Logger.LogException(e, "An unhandled exception was caught during the execution of a timer callback");
}
top.NextEpoc = CalculateNextEpoc(top.Cron);
if(top.NextEpoc != -1)
{
Timers.Add(top);
}
peek = Timers.Peek();
}
}
}
catch()
{
}
}
private long CalculateNextEpoc(string cron)
{
// based on cron string passed in, calculate the next time
// this timer should fire and return the output as
// epoc time
return 0;
}
}