/
QueuesOptions.cs
234 lines (211 loc) · 9.08 KB
/
QueuesOptions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Globalization;
using Azure.Storage.Queues;
using Microsoft.Azure.WebJobs.Extensions.Storage.Common;
using Microsoft.Azure.WebJobs.Extensions.Storage.Common.Listeners;
using Microsoft.Azure.WebJobs.Host.Scale;
using Microsoft.Azure.WebJobs.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Host
{
#if STORAGE_WEBJOBS_PUBLIC_QUEUE_PROCESSOR
/// <summary>
/// Represents configuration for <see cref="QueueTriggerAttribute"/>.
/// </summary>
public class QueuesOptions : IOptionsFormatter
#else
internal class QueuesOptions : IOptionsFormatter
#endif
{
private const int DefaultMaxDequeueCount = 5;
private const int DefaultBatchSize = 16;
// Azure Queues currently limits the number of messages retrieved to 32. We enforce this constraint here because
// the runtime error message the user would receive from the SDK otherwise is not as helpful.
internal const int MaxBatchSize = 32;
private int _batchSize = DefaultBatchSize;
private int _newBatchThreshold;
private int _processorCount = 1;
private TimeSpan _maxPollingInterval = QueuePollingIntervals.DefaultMaximum;
private TimeSpan _visibilityTimeout = TimeSpan.Zero;
private int _maxDequeueCount = DefaultMaxDequeueCount;
private QueueMessageEncoding _messageEncoding = QueueMessageEncoding.Base64;
/// <summary>
/// Initializes a new instance of the <see cref="QueuesOptions"/> class.
/// </summary>
public QueuesOptions()
{
_newBatchThreshold = -1;
_processorCount = SkuUtility.ProcessorCount;
}
/// <summary>
/// Gets or sets the number of queue messages to retrieve from the queue (per job method).
/// Must be in range within 1 and 32. The default is 16.
///
/// <remarks>
/// Both the <see cref="NewBatchThreshold"/> and <see cref="BatchSize"/> settings control how many messages are being processed in parallel.
/// The job keeps requesting messages in batches of <see cref="BatchSize"/> size until number of messages currently being processed
/// is above <see cref="NewBatchThreshold"/>. Then the job requests new batch of messages only if number of currently processed messages
/// drops at or below <see cref="NewBatchThreshold"/>.
///
/// The maximum number of messages processed in parallel by the job is <see cref="NewBatchThreshold"/> plus <see cref="BatchSize"/>. These manually
/// configured options aren't used when Dynamic Concurrency is enabled. See <see cref="ConcurrencyOptions.DynamicConcurrencyEnabled"/> for details.
/// When dynamic concurrency is enabled, the host will increase/decrease function concurrency dynamically as needed.
/// </remarks>
/// </summary>
public int BatchSize
{
get { return _batchSize; }
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
if (value > MaxBatchSize)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_batchSize = value;
}
}
/// <summary>
/// Gets or sets the threshold at which a new batch of messages will be fetched (per job method).
/// Must be zero or positive integer. If not set then it defaults to <code>BatchSize/2*processorCount</code>.
///
/// <remarks>
/// Both the <see cref="NewBatchThreshold"/> and <see cref="BatchSize"/> settings control how many messages are being processed in parallel.
/// The job keeps requesting messages in batches of <see cref="BatchSize"/> size until number of messages currently being processed
/// is above <see cref="NewBatchThreshold"/>. Then the job requests new batch of messages only if number of currently processed messages
/// drops at or below <see cref="NewBatchThreshold"/>.
///
/// The maximum number of messages processed in parallel by the job is <see cref="NewBatchThreshold"/> plus <see cref="BatchSize"/>.
/// </remarks>
/// </summary>
public int NewBatchThreshold
{
get
{
if (_newBatchThreshold == -1)
{
// if this hasn't been set explicitly, default it
return (_batchSize / 2) * _processorCount;
}
return _newBatchThreshold;
}
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_newBatchThreshold = value;
}
}
/// <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 { return _maxPollingInterval; }
set
{
if (value < QueuePollingIntervals.Minimum)
{
string message = String.Format(CultureInfo.CurrentCulture,
"MaxPollingInterval must not be less than {0}.", QueuePollingIntervals.Minimum);
throw new ArgumentException(message, nameof(value));
}
_maxPollingInterval = value;
}
}
/// <summary>
/// Gets or sets the number of times to try processing a message before moving it to the poison queue (where
/// possible).
/// </summary>
/// <remarks>
/// Some queues do not have corresponding poison queues, and this property does not apply to them. Specifically,
/// there are no corresponding poison queues for any queue whose name already ends in "-poison" or any queue
/// whose name is already too long to add a "-poison" suffix.
/// </remarks>
public int MaxDequeueCount
{
get { return _maxDequeueCount; }
set
{
if (value < 1)
{
throw new ArgumentException("MaxDequeueCount must not be less than 1.", nameof(value));
}
_maxDequeueCount = value;
}
}
/// <summary>
/// Gets or sets the default message visibility timeout that will be used
/// for messages that fail processing. The default is TimeSpan.Zero. To increase
/// the time delay between retries, increase this value.
/// </summary>
/// <remarks>
/// When message processing fails, the message will remain in the queue and
/// its visibility will be updated with this value. The message will then be
/// available for reprocessing after this timeout expires.
/// </remarks>
public TimeSpan VisibilityTimeout
{
get
{
return _visibilityTimeout;
}
set
{
_visibilityTimeout = value;
}
}
/// <summary>
/// Gets or sets a message encoding that determines how queue message body is represented in HTTP requests and responses.
/// The default is <see cref="QueueMessageEncoding.Base64"/>.
/// </summary>
public QueueMessageEncoding MessageEncoding
{
get
{
return _messageEncoding;
}
set
{
_messageEncoding = value;
}
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
string IOptionsFormatter.Format()
{
JObject options = new JObject
{
{ nameof(BatchSize), BatchSize },
{ nameof(NewBatchThreshold), NewBatchThreshold },
{ nameof(MaxPollingInterval), MaxPollingInterval },
{ nameof(MaxDequeueCount), MaxDequeueCount },
{ nameof(VisibilityTimeout), VisibilityTimeout },
{ nameof(MessageEncoding), MessageEncoding.ToString() }
};
return options.ToString(Formatting.Indented);
}
internal QueuesOptions Clone()
{
QueuesOptions copy = new QueuesOptions();
// making copy of private members, i.e. the _newBatchThreshold can be "unset" - copying that via properties would always set it.
copy._batchSize = _batchSize;
copy._maxDequeueCount = _maxDequeueCount;
copy._maxPollingInterval = _maxPollingInterval;
copy._messageEncoding = _messageEncoding;
copy._newBatchThreshold = _newBatchThreshold;
copy._visibilityTimeout = _visibilityTimeout;
return copy;
}
}
}