forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandlerWorker.cs
More file actions
235 lines (202 loc) · 7.86 KB
/
Copy pathMessageHandlerWorker.cs
File metadata and controls
235 lines (202 loc) · 7.86 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
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Threading;
using ServiceStack.Logging;
using ServiceStack.Redis;
using ServiceStack.Text;
namespace ServiceStack.Messaging.Redis
{
internal class MessageHandlerWorker : IDisposable
{
private static readonly ILog Log = LogManager.GetLogger(typeof(MessageHandlerWorker));
readonly object msgLock = new object();
private readonly IMessageHandler messageHandler;
private readonly IRedisClientsManager clientsManager;
public string QueueName { get; set; }
private int status;
public int Status
{
get { return status; }
}
private Thread bgThread;
private int timesStarted = 0;
private bool receivedNewMsgs = false;
public Action<MessageHandlerWorker, Exception> errorHandler { get; set; }
private DateTime lastMsgProcessed;
public DateTime LastMsgProcessed
{
get { return lastMsgProcessed; }
}
private int totalMessagesProcessed;
public int TotalMessagesProcessed
{
get { return totalMessagesProcessed; }
}
private int msgNotificationsReceived;
public int MsgNotificationsReceived
{
get { return msgNotificationsReceived; }
}
public MessageHandlerWorker(
IRedisClientsManager clientsManager, IMessageHandler messageHandler, string queueName,
Action<MessageHandlerWorker, Exception> errorHandler)
{
this.clientsManager = clientsManager;
this.messageHandler = messageHandler;
this.QueueName = queueName;
this.errorHandler = errorHandler;
}
public MessageHandlerWorker Clone()
{
return new MessageHandlerWorker(clientsManager, messageHandler, QueueName, errorHandler);
}
public void NotifyNewMessage()
{
Interlocked.Increment(ref msgNotificationsReceived);
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started)
{
if (Monitor.TryEnter(msgLock))
{
Monitor.Pulse(msgLock);
Monitor.Exit(msgLock);
}
else
{
receivedNewMsgs = true;
}
}
}
public void Start()
{
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started)
return;
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Disposed)
throw new ObjectDisposedException("MQ Host has been disposed");
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Stopping)
KillBgThreadIfExists();
if (Interlocked.CompareExchange(ref status, WorkerStatus.Starting, WorkerStatus.Stopped) == WorkerStatus.Stopped)
{
Log.Debug("Starting MQ Handler Worker: {0}...".Fmt(QueueName));
//Should only be 1 thread past this point
bgThread = new Thread(Run) {
Name = "{0}: {1}".Fmt(GetType().Name, QueueName),
IsBackground = true,
};
bgThread.Start();
}
}
public void ForceRestart()
{
KillBgThreadIfExists();
Start();
}
private void Run()
{
if (Interlocked.CompareExchange(ref status, WorkerStatus.Started, WorkerStatus.Starting) != WorkerStatus.Starting) return;
timesStarted++;
try
{
lock (msgLock)
{
while (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started)
{
receivedNewMsgs = false;
using (var mqClient = new RedisMessageQueueClient(clientsManager))
{
var msgsProcessedThisTime = messageHandler.ProcessQueue(mqClient, QueueName,
() => Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started);
totalMessagesProcessed += msgsProcessedThisTime;
if (msgsProcessedThisTime > 0)
lastMsgProcessed = DateTime.UtcNow;
}
if (!receivedNewMsgs)
Monitor.Wait(msgLock);
}
}
}
catch (Exception ex)
{
//Ignore handling rare, but expected exceptions from KillBgThreadIfExists()
if (ex is ThreadInterruptedException || ex is ThreadAbortException)
{
Log.Warn("Received {0} in Worker: {1}".Fmt(ex.GetType().Name, QueueName));
return;
}
Stop();
if (this.errorHandler != null) this.errorHandler(this, ex);
}
finally
{
//If it's in an invalid state, Dispose() this worker.
if (Interlocked.CompareExchange(ref status, WorkerStatus.Stopped, WorkerStatus.Stopping) != WorkerStatus.Stopping)
{
Dispose();
}
}
}
public void Stop()
{
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Disposed)
return;
if (Interlocked.CompareExchange(ref status, WorkerStatus.Stopping, WorkerStatus.Started) == WorkerStatus.Started)
{
Log.Debug("Stopping MQ Handler Worker: {0}...".Fmt(QueueName));
Thread.Sleep(100);
lock (msgLock)
{
Monitor.Pulse(msgLock);
}
}
}
private void KillBgThreadIfExists()
{
try
{
if (bgThread != null && bgThread.IsAlive)
{
//give it a small chance to die gracefully
if (!bgThread.Join(500))
{
//Ideally we shouldn't get here, but lets try our hardest to clean it up
Log.Warn("Interrupting previous Background Worker: " + bgThread.Name);
bgThread.Interrupt();
if (!bgThread.Join(TimeSpan.FromSeconds(3)))
{
Log.Warn(bgThread.Name + " just wont die, so we're now aborting it...");
bgThread.Abort();
}
}
}
}
finally
{
bgThread = null;
status = WorkerStatus.Stopped;
}
}
public virtual void Dispose()
{
if (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Disposed)
return;
Stop();
if (Interlocked.CompareExchange(ref status, WorkerStatus.Disposed, WorkerStatus.Stopped) != WorkerStatus.Stopped)
Interlocked.CompareExchange(ref status, WorkerStatus.Disposed, WorkerStatus.Stopping);
try
{
KillBgThreadIfExists();
}
catch (Exception ex)
{
Log.Error("Error Disposing MessageHandlerWorker for: " + QueueName, ex);
}
}
public IMessageHandlerStats GetStats()
{
return messageHandler.GetStats();
}
public string GetStatus()
{
return "[Worker: {0}, Status: {1}, ThreadStatus: {2}, LastMsgAt: {3}]"
.Fmt(QueueName, WorkerStatus.ToString(status), bgThread.ThreadState, LastMsgProcessed);
}
}
}