Exemple #1
0
        public override async Task Receive(MessageRetrieved retrieved, MessageWrapper message)
        {
            var body = message.Body ?? new byte[0];

            try
            {
                using (var tokenSource = new CancellationTokenSource())
                {
                    var pushContext = new MessageContext(message.Id, new Dictionary <string, string>(message.Headers), body, new TransportTransaction(), tokenSource, new ContextBag());
                    await pipeline(pushContext).ConfigureAwait(false);

                    if (tokenSource.IsCancellationRequested)
                    {
                        // if the pipeline cancelled the execution, nack the message to go back to the queue
                        await retrieved.Nack().ConfigureAwait(false);
                    }
                    else
                    {
                        // the pipeline hasn't been cancelled, the message should be acked
                        await retrieved.Ack().ConfigureAwait(false);
                    }
                }
            }
            catch (LeaseTimeoutException)
            {
                // The lease has expired and cannot be used any longer to Ack or Nack the message.
                // see original issue: https://github.com/Azure/azure-storage-net/issues/285
                throw;
            }
            catch (Exception ex)
            {
                var context = CreateErrorContext(retrieved, message, ex, body);
                ErrorHandleResult immediateRetry;

                try
                {
                    immediateRetry = await errorPipe(context).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Logger.Warn("The error pipeline wasn't able to handle the exception.", e);
                    await retrieved.Nack().ConfigureAwait(false);

                    return;
                }

                if (immediateRetry == ErrorHandleResult.RetryRequired)
                {
                    // For an immediate retry, the error is logged and the message is returned to the queue to preserve the DequeueCount.
                    // There is no in memory retry as scale-out scenarios would be handled improperly.
                    Logger.Warn("Azure Storage Queue transport failed pushing a message through pipeline. The message will be requeued", ex);
                    await retrieved.Nack().ConfigureAwait(false);
                }
                else
                {
                    // Just acknowledge the message as it's handled by the core retry.
                    await retrieved.Ack().ConfigureAwait(false);
                }
            }
        }
Exemple #2
0
        async Task InnerReceive(MessageRetrieved retrieved)
        {
            try
            {
                var message = await retrieved.Unwrap().ConfigureAwait(false);

                addressing.ApplyMappingToAliases(message.Headers);

                await receiveStrategy.Receive(retrieved, message).ConfigureAwait(false);
            }
            catch (LeaseTimeoutException ex)
            {
                Logger.Warn("Dispatching the message took longer than a visibility timeout. The message will reappear in the queue and will be obtained again.", ex);
            }
            catch (SerializationException ex)
            {
                Logger.Warn(ex.Message, ex);
            }
            catch (Exception ex)
            {
                Logger.Warn("Azure Storage Queue transport failed pushing a message through pipeline", ex);
            }
            finally
            {
                concurrencyLimiter.Release();
            }
        }
        async Task ProcessMessageSwallowExceptionsAndReleaseConcurrencyLimiter(MessageRetrieved retrieved, CancellationToken processingCancellationToken)
        {
            try
            {
                var message = await retrieved.Unwrap(processingCancellationToken).ConfigureAwait(false);

                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Unwrapped message ID: '{0}'", message.Id);
                }

                await receiveStrategy.Receive(retrieved, message, processingCancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex) when(ex.IsCausedBy(processingCancellationToken))
            {
                Logger.Debug("Message receiving canceled.", ex);
            }
            catch (LeaseTimeoutException ex)
            {
                Logger.Warn("Dispatching the message took longer than a visibility timeout. The message will reappear in the queue and will be obtained again.", ex);
            }
            catch (SerializationException ex)
            {
                Logger.Error(ex.Message, ex);
            }
            catch (Exception ex)
            {
                Logger.Warn("Azure Storage Queue transport failed pushing a message through pipeline", ex);
            }
            finally
            {
                concurrencyLimiter.Release();
            }
        }
        public override async Task Receive(MessageRetrieved retrieved, MessageWrapper message, CancellationToken cancellationToken = default)
        {
            Logger.DebugFormat("Pushing received message (ID: '{0}') through pipeline.", message.Id);
            var body       = message.Body ?? new byte[0];
            var contextBag = new ContextBag();

            try
            {
                var pushContext = new MessageContext(message.Id, new Dictionary <string, string>(message.Headers), body, new TransportTransaction(), contextBag);
                await onMessage(pushContext, cancellationToken).ConfigureAwait(false);

                await retrieved.Ack(cancellationToken).ConfigureAwait(false);
            }
            catch (LeaseTimeoutException)
            {
                // The lease has expired and cannot be used any longer to Ack or Nack the message.
                // see original issue: https://github.com/Azure/azure-storage-net/issues/285
                throw;
            }
            catch (Exception ex) when(!ex.IsCausedBy(cancellationToken))
            {
                var context = CreateErrorContext(retrieved, message, ex, body, contextBag);

                try
                {
                    var errorHandleResult = await onError(context, cancellationToken).ConfigureAwait(false);

                    if (errorHandleResult == ErrorHandleResult.RetryRequired)
                    {
                        // For an immediate retry, the error is logged and the message is returned to the queue to preserve the DequeueCount.
                        // There is no in memory retry as scale-out scenarios would be handled improperly.
                        Logger.Warn("Azure Storage Queue transport failed pushing a message through pipeline. The message will be requeued", ex);
                        await retrieved.Nack(cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        // Just acknowledge the message as it's handled by the core retry.
                        await retrieved.Ack(cancellationToken).ConfigureAwait(false);
                    }
                }
                catch (Exception onErrorEx) when(!onErrorEx.IsCausedBy(cancellationToken))
                {
                    criticalErrorAction($"Failed to execute recoverability policy for message with native ID: `{message.Id}`", onErrorEx, cancellationToken);

                    try
                    {
                        await retrieved.Nack(cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception nackEx) when(!nackEx.IsCausedBy(cancellationToken))
                    {
                        Logger.Warn($"Failed to release visibility timeout after message with native ID `{message.Id}` failed to execute recoverability policy. The message will be available again when the visibility timeout expires.", nackEx);
                    }
                }
            }
        }
        public override async Task Receive(MessageRetrieved retrieved, MessageWrapper message)
        {
            await retrieved.Ack().ConfigureAwait(false);

            var body = message.Body ?? new byte[0];

            try
            {
                var pushContext = new MessageContext(message.Id, new Dictionary <string, string>(message.Headers), body, new TransportTransaction(), new CancellationTokenSource(), new ContextBag());
                await pipeline(pushContext).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Warn("Azure Storage Queue transport failed pushing a message through pipeline", ex);

                var context = CreateErrorContext(retrieved, message, ex, body);

                // The exception is pushed through the error pipeline in a fire and forget manner.
                // There's no call to onCriticalError if errorPipe fails. Exceptions are handled on the transport level.
                await errorPipe(context).ConfigureAwait(false);
            }
        }
 public abstract Task Receive(MessageRetrieved retrieved, MessageWrapper message);
        protected static ErrorContext CreateErrorContext(MessageRetrieved retrieved, MessageWrapper message, Exception ex, byte[] body)
        {
            var context = new ErrorContext(ex, message.Headers, message.Id, body, new TransportTransaction(), retrieved.DequeueCount);

            return(context);
        }
 public abstract Task Receive(MessageRetrieved retrieved, MessageWrapper message, CancellationToken cancellationToken = default);