-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathQueue.cs
More file actions
152 lines (130 loc) · 7.02 KB
/
Queue.cs
File metadata and controls
152 lines (130 loc) · 7.02 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
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
#nullable enable
namespace DurableTask.AzureStorage.Storage
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
using DurableTask.AzureStorage.Monitoring;
class Queue
{
readonly AzureStorageClient azureStorageClient;
readonly AzureStorageOrchestrationServiceStats stats;
readonly QueueClient queueClient;
// TODO: I don't love this map. I think we should use our own AzStorage Queue class to keep track of popReceipts instead. We may need to change quite a few interfaces in response though, so they take our Queue class instead of the Az Storage SDK Queue abstraction
readonly Dictionary<string, string> messageIdPopReceipts;
public Queue(AzureStorageClient azureStorageClient, QueueServiceClient queueServiceClient, string queueName)
{
this.azureStorageClient = azureStorageClient;
this.stats = this.azureStorageClient.Stats;
this.queueClient = queueServiceClient.GetQueueClient(queueName);
this.messageIdPopReceipts = new Dictionary<string, string>();
}
public string Name => this.queueClient.Name;
public Uri Uri => this.queueClient.Uri;
public async Task<int> GetApproximateMessagesCountAsync(CancellationToken cancellationToken = default)
{
QueueProperties properties = await this.queueClient.GetPropertiesAsync(cancellationToken).DecorateFailure();
return properties.ApproximateMessagesCount;
}
public async Task AddMessageAsync(string message, TimeSpan? visibilityDelay, Guid? clientRequestId = null, CancellationToken cancellationToken = default)
{
using IDisposable scope = OperationContext.CreateClientRequestScope(clientRequestId);
await this.queueClient
.SendMessageAsync(
message,
visibilityDelay,
TimeSpan.FromSeconds(-1), // Infinite time to live
cancellationToken)
.DecorateFailure();
this.stats.MessagesSent.Increment();
}
public async Task UpdateMessageAsync(QueueMessage queueMessage, TimeSpan visibilityTimeout, Guid? clientRequestId = null, CancellationToken cancellationToken = default)
{
string popReceipt = queueMessage.PopReceipt; // default case
if (this.messageIdPopReceipts.TryGetValue(queueMessage.MessageId, out string foundReceipt))
{
// TODO: we should log something if we cannot find a pop receipt
popReceipt = foundReceipt;
}
using IDisposable scope = OperationContext.CreateClientRequestScope(clientRequestId);
Response<UpdateReceipt> response = await this.queueClient
.UpdateMessageAsync(
queueMessage.MessageId,
popReceipt,
visibilityTimeout: visibilityTimeout,
cancellationToken: cancellationToken)
.DecorateFailure();
this.messageIdPopReceipts[queueMessage.MessageId] = response.Value.PopReceipt;
UpdateReceipt receipt = response.Value;
this.stats.MessagesUpdated.Increment();
}
public async Task DeleteMessageAsync(QueueMessage queueMessage, Guid? clientRequestId = null, CancellationToken cancellationToken = default)
{
using IDisposable scope = OperationContext.CreateClientRequestScope(clientRequestId);
await this.queueClient
.DeleteMessageAsync(
queueMessage.MessageId,
queueMessage.PopReceipt,
cancellationToken)
.DecorateFailure();
this.messageIdPopReceipts.Remove(queueMessage.MessageId);
}
public async Task<QueueMessage?> GetMessageAsync(TimeSpan visibilityTimeout, CancellationToken cancellationToken = default)
{
QueueMessage message = await this.queueClient.ReceiveMessageAsync(visibilityTimeout, cancellationToken).DecorateFailure();
if (message == null)
{
return null;
}
this.messageIdPopReceipts.Add(message.MessageId, message.PopReceipt);
this.stats.MessagesRead.Increment();
return message;
}
public async Task<bool> ExistsAsync(CancellationToken cancellationToken = default)
{
return await this.queueClient.ExistsAsync(cancellationToken).DecorateFailure();
}
public async Task<bool> CreateIfNotExistsAsync(CancellationToken cancellationToken = default)
{
// If we received null, then the response must have been a 409 (Conflict) and the queue must already exist
Response response = await this.queueClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken).DecorateFailure();
return response != null;
}
public async Task<bool> DeleteIfExistsAsync(CancellationToken cancellationToken = default)
{
return await this.queueClient.DeleteIfExistsAsync(cancellationToken).DecorateFailure();
}
public async Task<IReadOnlyCollection<QueueMessage>> GetMessagesAsync(int batchSize, TimeSpan visibilityTimeout, CancellationToken cancellationToken = default)
{
QueueMessage[] messages = await this.queueClient.ReceiveMessagesAsync(batchSize, visibilityTimeout, cancellationToken).DecorateFailure();
this.stats.MessagesRead.Increment(messages.Length);
return messages;
}
public async Task<IReadOnlyCollection<PeekedMessage>> PeekMessagesAsync(int batchSize, CancellationToken cancellationToken = default)
{
PeekedMessage[] messages = await this.queueClient.PeekMessagesAsync(batchSize, cancellationToken).DecorateFailure();
this.stats.MessagesRead.Increment(messages.Length);
return messages;
}
public async Task<PeekedMessage?> PeekMessageAsync(CancellationToken cancellationToken = default)
{
return await this.queueClient.PeekMessageAsync(cancellationToken).DecorateFailure();
}
}
}