forked from optimajet/WorkflowEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerManager.cs
More file actions
224 lines (193 loc) · 7.53 KB
/
TimerManager.cs
File metadata and controls
224 lines (193 loc) · 7.53 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using OptimaJet.Workflow.Core.Model;
namespace OptimaJet.Workflow.Core.Runtime
{
/// <summary>
/// Represent a timer information
/// </summary>
public class TimerToExecute
{
/// <summary>
/// Timer id
/// </summary>
public Guid TimerId { get; set; }
/// <summary>
/// Timer name <see cref="TimerDefinition.Name"/>
/// </summary>
public string Name { get; set; }
/// <summary>
/// Id of the process which owned the timer
/// </summary>
public Guid ProcessId { get; set; }
}
/// <summary>
/// Default timer manager <see cref="ITimerManager"/>
/// </summary>
public sealed class TimerManager : ITimerManager
{
private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private WorkflowRuntime _runtime;
private Timer _timer;
private bool _stopped = false;
/// <summary>
/// Register all timers for all outgouing timer transitions for current actvity of the specified process.
/// All timers registered before which are present in transitions will be rewrited except timers marked as NotOverrideIfExists <see cref="TimerDefinition"/>
/// </summary>
/// <param name="processInstance">Process instance whose timers need to be registered</param>
public void RegisterTimers(ProcessInstance processInstance)
{
var timersToRegister =
processInstance.ProcessScheme.GetTimerTransitionForActivity(processInstance.CurrentActivity)
.Where(t => t.Trigger.Timer != null).Select(t=>t.Trigger.Timer).ToList();
if (!timersToRegister.Any())
return;
if (!_stopped)
RefreshInterval(processInstance, timersToRegister);
else
{
RegisterTimers(processInstance, timersToRegister);
}
}
/// <summary>
/// Clear timers <see cref="ClearTimers"/> and then register new timers <see cref="RegisterTimers"/>
/// </summary>
/// <param name="processInstance">Process instance whose timers need to be cleared an registered</param>
public void ClearAndRegisterTimers(ProcessInstance processInstance)
{
ClearTimers(processInstance);
RegisterTimers(processInstance);
}
private void RefreshInterval(ProcessInstance processInstance, List<TimerDefinition> timersToRegister)
{
_lock.EnterUpgradeableReadLock();
try
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
RegisterTimers(processInstance, timersToRegister);
RefreshInterval();
}
finally
{
_lock.ExitUpgradeableReadLock();
}
}
private void RefreshInterval()
{
_lock.EnterWriteLock();
try
{
var next = _runtime.PersistenceProvider.GetCloseExecutionDateTime();
if (!next.HasValue)
return;
var now = _runtime.RuntimeDateTimeNow;
if (now < next)
{
var res = _timer.Change((long)(next.Value - now).TotalMilliseconds, Timeout.Infinite);
}
else
{
var res = _timer.Change(0, Timeout.Infinite);
}
}
finally
{
_lock.ExitWriteLock();
}
}
private void RegisterTimers(ProcessInstance processInstance, List<TimerDefinition> timersToRegister)
{
timersToRegister.ForEach(
t =>
_runtime.PersistenceProvider.RegisterTimer(processInstance.ProcessId, t.Name, GetNextExecutionDateTime(t),
t.NotOverrideIfExists));
}
private DateTime GetNextExecutionDateTime(TimerDefinition timerDefinition)
{
switch (timerDefinition.Type)
{
case TimerType.Date:
var date1 = DateTime.ParseExact(timerDefinition.Value,
_runtime.SchemeParsingCulture.DateTimeFormat.ShortDatePattern, CultureInfo.InvariantCulture);
return date1.Date;
case TimerType.DateAndTime:
var date2 = DateTime.ParseExact(timerDefinition.Value,
(string.Format("{0} {1}", _runtime.SchemeParsingCulture.DateTimeFormat.ShortDatePattern,
_runtime.SchemeParsingCulture.DateTimeFormat.LongTimePattern)), CultureInfo.InvariantCulture);
return date2;
case TimerType.Interval:
var interval = Int32.Parse(timerDefinition.Value);
return _runtime.RuntimeDateTimeNow.AddMilliseconds(interval);
case TimerType.Time:
var now = _runtime.RuntimeDateTimeNow;
var date3 = DateTime.ParseExact(timerDefinition.Value, _runtime.SchemeParsingCulture.DateTimeFormat.LongTimePattern, CultureInfo.InvariantCulture);
if (date3 < now)
date3 = date3.AddDays(1);
return date3;
}
return _runtime.RuntimeDateTimeNow;
}
/// <summary>
/// Clear all registerd timers except present in outgouing timer transitions for current actvity of the specified process and marked as NotOverrideIfExists <see cref="TimerDefinition"/>
/// </summary>
/// <param name="processInstance">Process instance whose timers need to be cleared</param>
public void ClearTimers(ProcessInstance processInstance)
{
var timersIgnoreList =
processInstance.ProcessScheme.GetTimerTransitionForActivity(processInstance.CurrentActivity)
.Where(t => t.Trigger.Timer != null && t.Trigger.Timer.NotOverrideIfExists).Select(t=>t.Trigger.Timer.Name).ToList();
_runtime.PersistenceProvider.ClearTimers(processInstance.ProcessId,timersIgnoreList);
}
public void Init(WorkflowRuntime runtime)
{
_runtime = runtime;
_timer = new Timer(OnTimer);
}
/// <summary>
/// Start the timer
/// </summary>
public void Start()
{
if (!_stopped)
return;
_stopped = false;
RefreshInterval();
}
/// <summary>
/// Stop the timer
/// </summary>
public void Stop()
{
_stopped = true;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
/// <summary>
/// Refresh interval of the timer
/// </summary>
public void Refresh()
{
if (_stopped)
return;
RefreshInterval();
}
private void OnTimer(object state)
{
var timers = _runtime.PersistenceProvider.GetTimersToExecute();
foreach (var timer in timers)
{
try
{
_runtime.ExecuteTimer(timer.ProcessId,timer.Name);
}
finally
{
_runtime.PersistenceProvider.ClearTimer(timer.TimerId);
}
}
RefreshInterval();
}
}
}