/
QueueProcessor.cs
293 lines (266 loc) · 12.7 KB
/
QueueProcessor.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
namespace Microsoft.Azure.WebJobs.Host.Queues
{
/// <summary>
/// This class defines a strategy used for processing queue messages.
/// </summary>
/// <remarks>
/// Custom <see cref="QueueProcessor"/> implementations can be registered by implementing
/// a custom <see cref="IQueueProcessorFactory"/> and setting it via <see cref="JobHostQueuesConfiguration.QueueProcessorFactory"/>.
/// </remarks>
public class QueueProcessor
{
private readonly CloudQueue _queue;
private readonly CloudQueue _poisonQueue;
private readonly TraceWriter _trace;
private readonly ILogger _logger;
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="context">The factory context.</param>
public QueueProcessor(QueueProcessorFactoryContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
_queue = context.Queue;
_poisonQueue = context.PoisonQueue;
_trace = context.Trace;
_logger = context.Logger;
MaxDequeueCount = context.MaxDequeueCount;
BatchSize = context.BatchSize;
NewBatchThreshold = context.NewBatchThreshold;
VisibilityTimeout = context.VisibilityTimeout;
MaxPollingInterval = context.MaxPollingInterval;
}
/// <summary>
/// Event raised when a message is added to the poison queue.
/// </summary>
public event EventHandler<PoisonMessageEventArgs> MessageAddedToPoisonQueue;
/// <summary>
/// Gets or sets the number of queue messages to retrieve and process in parallel.
/// </summary>
public int BatchSize { get; protected set; }
/// <summary>
/// Gets or sets the number of times to try processing a message before moving it to the poison queue.
/// </summary>
public int MaxDequeueCount { get; protected set; }
/// <summary>
/// Gets or sets the threshold at which a new batch of messages will be fetched.
/// </summary>
public int NewBatchThreshold { get; protected set; }
/// <summary>
/// Gets or sets the longest period of time to wait before checking for a message to arrive when a queue remains
/// empty.
/// </summary>
public TimeSpan MaxPollingInterval { get; protected set; }
/// <summary>
/// Gets or sets the default message visibility timeout that will be used
/// for messages that fail processing.
/// </summary>
public TimeSpan VisibilityTimeout { get; protected set; }
/// <summary>
/// This method is called when there is a new message to process, before the job function is invoked.
/// This allows any preprocessing to take place on the message before processing begins.
/// </summary>
/// <param name="message">The message to process.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns>True if the message processing should continue, false otherwise.</returns>
public virtual async Task<bool> BeginProcessingMessageAsync(CloudQueueMessage message, CancellationToken cancellationToken)
{
if (message.DequeueCount > MaxDequeueCount)
{
await HandlePoisonMessageAsync(message, cancellationToken);
return await Task.FromResult<bool>(false);
}
return await Task.FromResult<bool>(true);
}
/// <summary>
/// This method completes processing of the specified message, after the job function has been invoked.
/// </summary>
/// <remarks>
/// If the message was processed successfully, the message should be deleted. If message processing failed, the
/// message should be release back to the queue, or if the maximum dequeue count has been exceeded, the message
/// should be moved to the poison queue (if poison queue handling is configured for the queue).
/// </remarks>
/// <param name="message">The message to complete processing for.</param>
/// <param name="result">The <see cref="FunctionResult"/> from the job invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns></returns>
public virtual async Task CompleteProcessingMessageAsync(CloudQueueMessage message, FunctionResult result, CancellationToken cancellationToken)
{
if (result.Succeeded)
{
await DeleteMessageAsync(message, cancellationToken);
}
else if (_poisonQueue != null)
{
if (message.DequeueCount >= MaxDequeueCount)
{
await HandlePoisonMessageAsync(message, cancellationToken);
}
else
{
await ReleaseMessageAsync(message, result, VisibilityTimeout, cancellationToken);
}
}
else
{
// For queues without a corresponding poison queue, leave the message invisible when processing
// fails to prevent a fast infinite loop.
// Specifically, don't call ReleaseMessage(message)
}
}
private async Task HandlePoisonMessageAsync(CloudQueueMessage message, CancellationToken cancellationToken)
{
if (_poisonQueue != null)
{
// These values may change if the message is inserted into another queue. We'll store them here and make sure
// the message always has the original values before we pass it to a customer-facing method.
string id = message.Id;
string popReceipt = message.PopReceipt;
await CopyMessageToPoisonQueueAsync(message, _poisonQueue, cancellationToken);
// TEMP: Re-evaluate these property updates when we update Storage SDK: https://github.com/Azure/azure-webjobs-sdk/issues/1144
message.UpdateChangedProperties(id, popReceipt);
await DeleteMessageAsync(message, cancellationToken);
}
}
/// <summary>
/// Moves the specified message to the poison queue.
/// </summary>
/// <param name="message">The poison message</param>
/// <param name="poisonQueue">The poison queue to copy the message to</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns></returns>
protected virtual async Task CopyMessageToPoisonQueueAsync(CloudQueueMessage message, CloudQueue poisonQueue, CancellationToken cancellationToken)
{
string msg = string.Format(CultureInfo.InvariantCulture, "Message has reached MaxDequeueCount of {0}. Moving message to queue '{1}'.", MaxDequeueCount, poisonQueue.Name);
_trace.Warning(msg, TraceSource.Execution);
_logger?.LogWarning(msg);
await AddMessageAndCreateIfNotExistsAsync(poisonQueue, message, cancellationToken);
var eventArgs = new PoisonMessageEventArgs(message, poisonQueue);
OnMessageAddedToPoisonQueue(eventArgs);
}
/// <summary>
/// Release the specified failed message back to the queue.
/// </summary>
/// <param name="message">The message to release</param>
/// <param name="result">The <see cref="FunctionResult"/> from the job invocation.</param>
/// <param name="visibilityTimeout">The visibility timeout to set for the message</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns></returns>
protected virtual async Task ReleaseMessageAsync(CloudQueueMessage message, FunctionResult result, TimeSpan visibilityTimeout, CancellationToken cancellationToken)
{
try
{
// We couldn't process the message. Let someone else try.
await _queue.UpdateMessageAsync(message, visibilityTimeout, MessageUpdateFields.Visibility, cancellationToken);
}
catch (StorageException exception)
{
if (exception.IsBadRequestPopReceiptMismatch())
{
// Someone else already took over the message; no need to do anything.
return;
}
else if (exception.IsNotFoundMessageOrQueueNotFound() ||
exception.IsConflictQueueBeingDeletedOrDisabled())
{
// The message or queue is gone, or the queue is down; no need to release the message.
return;
}
else
{
throw;
}
}
}
/// <summary>
/// Delete the specified message.
/// </summary>
/// <param name="message">The message to delete.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns></returns>
protected virtual async Task DeleteMessageAsync(CloudQueueMessage message, CancellationToken cancellationToken)
{
try
{
await _queue.DeleteMessageAsync(message, cancellationToken);
}
catch (StorageException exception)
{
// For consistency, the exceptions handled here should match UpdateQueueMessageVisibilityCommand.
if (exception.IsBadRequestPopReceiptMismatch())
{
// If someone else took over the message; let them delete it.
string msg = $"Unable to delete queue message '{message.Id}' because the {nameof(CloudQueueMessage.PopReceipt)} did not match. This could indicate that the function has modified the message and may be expected.";
_trace.Verbose(msg);
_logger.LogDebug(msg);
return;
}
else if (exception.IsNotFoundMessageOrQueueNotFound())
{
string msg = $"Unable to delete queue message '{message.Id}' because either the message or the queue '{_queue.Name}' was not found.";
_trace.Verbose(msg);
_logger.LogDebug(msg);
}
else if (exception.IsConflictQueueBeingDeletedOrDisabled())
{
// The message or queue is gone, or the queue is down; no need to delete the message.
string msg = $"Unable to delete queue message '{message.Id}' because the queue `{_queue.Name}` is either disabled or being deleted.";
_trace.Verbose(msg);
_logger.LogDebug(msg);
return;
}
else
{
throw;
}
}
}
/// <summary>
/// Called to raise the MessageAddedToPoisonQueue event
/// </summary>
/// <param name="e">The event arguments</param>
protected internal virtual void OnMessageAddedToPoisonQueue(PoisonMessageEventArgs e)
{
MessageAddedToPoisonQueue?.Invoke(this, e);
}
private static async Task AddMessageAndCreateIfNotExistsAsync(CloudQueue queue, CloudQueueMessage message, CancellationToken cancellationToken)
{
if (queue == null)
{
throw new ArgumentNullException("queue");
}
bool isQueueNotFoundException = false;
try
{
await queue.AddMessageAsync(message, cancellationToken);
return;
}
catch (StorageException exception)
{
if (!exception.IsNotFoundQueueNotFound())
{
throw;
}
isQueueNotFoundException = true;
}
Debug.Assert(isQueueNotFoundException);
await queue.CreateIfNotExistsAsync(cancellationToken);
await queue.AddMessageAsync(message, cancellationToken);
}
}
}