Exemple #1
0
        /// <summary>
        /// Starts the loop for polling the SQS queue
        /// </summary>
        public async void Run()
        {
            Log.InfoFormat("[{0}] Starting message pump", Id);
            while (!_cancellationToken.IsCancellationRequested)
            {
                Log.TraceFormat("[{0}] Starting receiving call", Id);
                try
                {
                    IEnumerable <TransportMessage> recievedMessages = await _queue.GetMessages(_cancellationToken.Token);

                    Log.TraceFormat("[{1}] Received {0} messages", recievedMessages.Count(), Id);
                    foreach (TransportMessage message in recievedMessages)
                    {
                        Log.InfoFormat("[{1}] Received message of type '{0}'", message.MessageTypeName, Id);
                        _messageLogger.InboundLogMessage(message.Body, message.MessageTypeName, message.Id, message.AttemptNumber);
                        MessageProcessingResult result;
                        if (message.MessageParsingSucceeded)
                        {
                            Log.TraceFormat("[{0}] Processing message", Id);
                            result = _messageProcessor.ProcessMessage(message, _bus);
                            Log.TraceFormat("[{0}] Processed message - Error: {1}", Id, !result.WasSuccessful);
                        }
                        else
                        {
                            Log.ErrorFormat("[{1}] Failed to parse message of type {0}", message.Exception, message.MessageTypeName, Id);
                            result = new MessageProcessingResult()
                            {
                                WasSuccessful = false, Exception = new Exception("Message parse failure")
                            };
                        }

                        if (result.WasSuccessful)
                        {
                            Log.InfoFormat("[{0}] Removing message from the queue", Id);
                            _queue.RemoveMessage(message);
                        }
                        else if (message.AttemptNumber == _messageRetryCount)
                        {
                            Log.InfoFormat("[{0}] Message faulted ", Id);
                            _messageProcessor.ProcessFaultedMessage(message, _bus, result.Exception);
                        }

                        MessageStatistics stats = new MessageStatistics()
                        {
                            FinalAttempt       = message.AttemptNumber == _messageRetryCount,
                            HandlerRunTime     = result.Runtime,
                            MessageLength      = message.Body.Length,
                            MessageType        = message.MessageTypeName,
                            Success            = result.WasSuccessful,
                            PreviousRetryCount = message.AttemptNumber,
                        };
                        _messageProcessor.ProcessMessageStatistics(stats);
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("[{0}] Error occurred in message pump run", ex, Id);
                }
            }
        }