Beispiel #1
0
        private async Task <bool> HandleMessageWithRetries(SerializedMessage message, TransportTransaction transportTransaction, int processingAttempt)
        {
            try
            {
                var receiveCancellationTokenSource = new CancellationTokenSource();
                var(messageId, headers, body) = message.Deserialize();

                var pushContext = new MessageContext(messageId, headers, body, transportTransaction, receiveCancellationTokenSource, new ContextBag());

                await _onMessage(pushContext).ConfigureAwait(false);

                return(!receiveCancellationTokenSource.IsCancellationRequested);
            }
            catch (Exception e)
            {
                var(messageId, headers, body) = message.Deserialize();
                var errorContext = new ErrorContext(e, headers, messageId, body, transportTransaction, processingAttempt);

                processingAttempt++;

                try
                {
                    var errorHandlingResult = await _onError(errorContext).ConfigureAwait(false);

                    if (errorHandlingResult == ErrorHandleResult.RetryRequired)
                    {
                        return(await HandleMessageWithRetries(message, transportTransaction, processingAttempt).ConfigureAwait(false));
                    }
                }
                catch (Exception exception)
                {
                    _criticalError.Raise($"Failed to execute recoverability policy for message with native ID: `{messageId}`", exception);

                    return(await HandleMessageWithRetries(message, transportTransaction, processingAttempt).ConfigureAwait(false));
                }

                return(true);
            }
        }
Beispiel #2
0
        private async Task ProcessMessage(SerializedMessage message)
        {
            await _concurrencyLimiter.WaitAsync(_cancellationTokenSource.Token).ConfigureAwait(false);

            var task = Task.Run(async() =>
            {
                try
                {
                    await ProcessMessageWithTransaction(message).ConfigureAwait(false);
                }
                finally
                {
                    _concurrencyLimiter.Release();
                }
            }, _cancellationTokenSource.Token);

            _ = task.ContinueWith(t =>
            {
                _runningReceiveTasks.TryRemove(t, out _);
            }, TaskContinuationOptions.ExecuteSynchronously);

            _ = _runningReceiveTasks.AddOrUpdate(task, task, (k, v) => task);
        }
        public static (string messagEId, Dictionary <string, string> headers, byte[] body) Deserialize(this SerializedMessage message)
        {
            var messageId = message.MessageId;
            var headers   = JsonConvert.DeserializeObject <Dictionary <string, string> >(message.Headers);
            var body      = message.Body;

            return(messageId, headers, body);
        }
Beispiel #4
0
 public bool TryDequeue(out SerializedMessage message)
 {
     return(_queue.TryDequeue(out message));
 }
Beispiel #5
0
 /// <summary>
 /// Adds a message to the queue.
 /// </summary>
 /// <param name="message">The message to enqueue</param>
 public void AddMessage(SerializedMessage message)
 {
     _queue.Enqueue(message);
 }