public async Task CompleteAsync(StorageQueueMessage message) { await Task.WhenAll( _queue.DeleteMessageAsync(message.QueueMessageId, message.PopReceipt), TryDeleteBlob(message.BlobMessageId)) .ConfigureAwait(false); }
public StorageQueueMessageStateHandler(IStorageQueueClient queueClient, StorageQueueMessage message, int deadLetterDeliveryLimit, IDependencyInjection messageScope) { DeadLetterDeliveryLimit = deadLetterDeliveryLimit; MessageScope = messageScope; _queueClient = queueClient; _message = message; }
public async Task DeadLetterAsync(StorageQueueMessage message) { var deadLetterMessage = new CloudQueueMessage(_serializer.Serialize(message.Properties)); await _dlQueue.AddMessageAsync(deadLetterMessage, TimeSpan.MaxValue, null, null, null) .ContinueWith(task => _queue.DeleteMessageAsync(message.QueueMessageId, message.PopReceipt), TaskContinuationOptions.OnlyOnRanToCompletion) .ConfigureAwait(false); }
public async Task AbandonByErrorAsync(StorageQueueMessage message, TimeSpan?visibilityTimeout) { visibilityTimeout = visibilityTimeout ?? TimeSpan.Zero; var result = await _queue.UpdateMessageAsync(message.QueueMessageId, message.PopReceipt, new BinaryData(_serializer.Serialize(message.Properties)), visibilityTimeout.Value).ConfigureAwait(false); message.PopReceipt = result.Value.PopReceipt; }
private async Task <List <StorageQueueMessage> > GetMessagesAsync <T>(int count, TimeSpan?lockDuration, CloudQueue queue) where T : IStorageQueueCommand { var messages = (await queue.GetMessagesAsync(count, lockDuration, null, null).ConfigureAwait(false)).ToList(); if (!messages.Any()) { return(new List <StorageQueueMessage>()); } var messageContainers = new List <StorageQueueMessage>(messages.Count); foreach (var queueMessage in messages) { try { var message = new StorageQueueMessage { QueueMessageId = queueMessage.Id, DequeueCount = queueMessage.DequeueCount, PopReceipt = queueMessage.PopReceipt, Properties = TryDeserializeProperties(queueMessage.AsString) }; var content = await _container.GetBlockBlobReference(message.BlobMessageId).DownloadTextAsync().ConfigureAwait(false); message.Message = _serializer.Deserialize <T>(content); messageContainers.Add(message); } catch (StorageException e) when(e.RequestInformation?.HttpStatusCode == 404) { //If we cannot find the message blob something is really wrong, delete the message right away await queue.DeleteMessageAsync(queueMessage.Id, queueMessage.PopReceipt); } } return(messageContainers); }
public async Task SetVisibilityTimeout(StorageQueueMessage message, TimeSpan timeToExtend, CancellationToken cancellationToken) { var cloudQueueMessage = new CloudQueueMessage(message.QueueMessageId, message.PopReceipt); await _queue.UpdateMessageAsync(cloudQueueMessage, timeToExtend, MessageUpdateFields.Visibility, null, null, cancellationToken).ConfigureAwait(false); message.PopReceipt = cloudQueueMessage.PopReceipt; }
public async Task AbandonByErrorAsync(StorageQueueMessage message, TimeSpan?visibilityTimeout) { visibilityTimeout = visibilityTimeout ?? TimeSpan.Zero; var cloudQueueMessage = new CloudQueueMessage(message.QueueMessageId, message.PopReceipt); cloudQueueMessage.SetMessageContent(_serializer.Serialize(message.Properties)); await _queue.UpdateMessageAsync(cloudQueueMessage, visibilityTimeout.Value, MessageUpdateFields.Content | MessageUpdateFields.Visibility).ConfigureAwait(false); }
public async Task SendAsync <T>(T message, TimeSpan?delay) where T : IStorageQueueCommand { var storageMessage = new StorageQueueMessage(message) { BlobMessageId = Guid.NewGuid().ToString() }; if (typeof(ICommandWithAttachment).IsAssignableFrom(typeof(T))) { if (_attachmentProvider == null) { throw new AttachmentProviderMissingException(); } var attachmentMessage = (ICommandWithAttachment)message; if (attachmentMessage.Attachment != null) { var attachmentIds = new List <string>(); var attachmentId = Guid.NewGuid().ToString("N"); await _attachmentProvider.UploadAttachmentAsync(_queueName, attachmentId, attachmentMessage.Attachment).ConfigureAwait(false); attachmentIds.Add(attachmentId); storageMessage.Properties[AttachmentUtility.AttachmentKey] = string.Join(",", attachmentIds); } } //Create hidden queue message var cloudMessage = new CloudQueueMessage(_serializer.Serialize(storageMessage.Properties)); await _queue.AddMessageAsync(cloudMessage, TimeSpan.MaxValue, TimeSpan.FromDays(1), null, null).ConfigureAwait(false); try { var blob = _container.GetBlockBlobReference(storageMessage.BlobMessageId); await blob.UploadTextAsync(_serializer.Serialize(message)).ConfigureAwait(false); //Make the queue message visible await _queue.UpdateMessageAsync(cloudMessage, delay ?? TimeSpan.Zero, MessageUpdateFields.Visibility).ConfigureAwait(false); } catch (Exception) { //If we cannot upload the blob the delete the hidden message and throw try { await _queue.DeleteMessageAsync(cloudMessage).ConfigureAwait(false); } catch (StorageException e) when(e.RequestInformation?.HttpStatusCode == 404) { //Cannot erase what's not there } throw; } }
public async Task SendAsync <T>(T message, TimeSpan?delay, CancellationToken cancellationToken = default) where T : IStorageQueueCommand { var storageMessage = new StorageQueueMessage(message) { BlobMessageId = Guid.NewGuid().ToString() }; if (typeof(ICommandWithAttachment).IsAssignableFrom(typeof(T))) { if (_attachmentProvider == null) { throw new AttachmentProviderMissingException(); } var attachmentMessage = (ICommandWithAttachment)message; if (attachmentMessage.Attachment != null) { var attachmentIds = new List <string>(); var attachmentId = Guid.NewGuid().ToString("N"); await _attachmentProvider.UploadAttachmentAsync(_queueName, attachmentId, attachmentMessage.Attachment, cancellationToken).ConfigureAwait(false); attachmentIds.Add(attachmentId); storageMessage.Properties[AttachmentUtility.AttachmentKey] = string.Join(",", attachmentIds); } } try { var blob = _container.GetBlobClient(storageMessage.BlobMessageId); using (var stream = new MemoryStream(_serializer.Serialize(message))) { await blob.UploadAsync(stream, cancellationToken).ConfigureAwait(false); } await _queue.SendMessageAsync(new BinaryData(_serializer.Serialize(storageMessage.Properties)), delay ?? TimeSpan.Zero, TimeSpan.FromSeconds(-1), cancellationToken).ConfigureAwait(false); } catch (Exception) { //If we cannot upload the blob or create the message try cleaning up and throw await TryDeleteBlob(storageMessage.BlobMessageId).ConfigureAwait(false); throw; } }
public async Task SendAsync <T>(T message, TimeSpan?delay) where T : IStorageQueueCommand { var storageMessage = new StorageQueueMessage(message) { BlobMessageId = Guid.NewGuid().ToString() }; if (typeof(ICommandWithAttachment).IsAssignableFrom(typeof(T))) { if (_attachmentProvider == null) { throw new AttachmentProviderMissingException(); } var attachmentMessage = (ICommandWithAttachment)message; if (attachmentMessage.Attachment != null) { var attachmentIds = new List <string>(); var attachmentId = Guid.NewGuid().ToString("N"); await _attachmentProvider.UploadAttachmentAsync(_queueName, attachmentId, attachmentMessage.Attachment).ConfigureAwait(false); attachmentIds.Add(attachmentId); storageMessage.Properties[AttachmentUtility.AttachmentKey] = string.Join(",", attachmentIds); } } var cloudMessage = new CloudQueueMessage(_serializer.Serialize(storageMessage.Properties)); try { var blob = _container.GetBlockBlobReference(storageMessage.BlobMessageId); await blob.UploadTextAsync(_serializer.Serialize(message)).ConfigureAwait(false); await _queue.AddMessageAsync(cloudMessage, TimeSpan.MaxValue, delay ?? TimeSpan.Zero, null, null).ConfigureAwait(false); } catch (Exception) { //If we cannot upload the blob or create the message try cleaning up and throw await TryDeleteBlob(storageMessage.BlobMessageId).ConfigureAwait(false); throw; } }
private async Task <List <StorageQueueMessage> > GetMessagesAsync <T>(int count, TimeSpan?lockDuration, QueueClient queue) where T : IStorageQueueCommand { var messages = (await queue.ReceiveMessagesAsync(count, lockDuration).ConfigureAwait(false)).Value; if (!messages.Any()) { return(new List <StorageQueueMessage>()); } var messageContainers = new List <StorageQueueMessage>(messages.Length); foreach (var queueMessage in messages) { try { var message = new StorageQueueMessage { QueueMessageId = queueMessage.MessageId, DequeueCount = (int)queueMessage.DequeueCount, PopReceipt = queueMessage.PopReceipt, Properties = TryDeserializeProperties(queueMessage.Body) }; using (var stream = new MemoryStream()) { await _container.GetBlobClient(message.BlobMessageId).DownloadToAsync(stream).ConfigureAwait(false); stream.Position = 0; message.Message = await _serializer.Deserialize <T>(stream).ConfigureAwait(false); messageContainers.Add(message); } } catch (RequestFailedException e) when(e.Status == 404) { //If we cannot find the message blob something is really wrong, delete the message right away await queue.DeleteMessageAsync(queueMessage.MessageId, queueMessage.PopReceipt); } } return(messageContainers); }
private async Task Handle(StorageQueueMessage message, CancellationToken cancellationToken) { await _processor.ProcessAsync(new StorageQueueMessageStateHandler <T>(_storageQueueClient, message, Settings.DeadLetterDeliveryLimit), cancellationToken).ConfigureAwait(false); }
public StorageQueueMessageStateHandler(IStorageQueueClient queueClient, StorageQueueMessage message, int deadLetterDeliveryLimit) { DeadLetterDeliveryLimit = deadLetterDeliveryLimit; _queueClient = queueClient; _message = message; }
public async Task CompleteAsync(StorageQueueMessage message) { await _queue.DeleteMessageAsync(message.QueueMessageId, message.PopReceipt) .ContinueWith(task => TryDeleteBlob(message.BlobMessageId), TaskContinuationOptions.OnlyOnRanToCompletion) .ConfigureAwait(false); }
public async Task DeadLetterAsync(StorageQueueMessage message) { await _dlQueue.SendMessageAsync(new BinaryData(_serializer.Serialize(message.Properties)), timeToLive : TimeSpan.FromSeconds(-1)) .ContinueWith(task => _queue.DeleteMessageAsync(message.QueueMessageId, message.PopReceipt), TaskContinuationOptions.OnlyOnRanToCompletion) .ConfigureAwait(false); }
public async Task SetVisibilityTimeout(StorageQueueMessage message, TimeSpan timeToExtend, CancellationToken cancellationToken) { var result = await _queue.UpdateMessageAsync(message.QueueMessageId, message.PopReceipt, visibilityTimeout : timeToExtend, cancellationToken : cancellationToken).ConfigureAwait(false); message.PopReceipt = result.Value.PopReceipt; }