private async Task <IList <Message> > GetMessagesAsync(int count, CancellationToken stoppingToken) { stoppingToken.ThrowIfCancellationRequested(); using var receiveTimeout = new CancellationTokenSource(_readTimeout); IList <Message> messages; try { using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, receiveTimeout.Token); var context = new ReceiveMessagesContext { Count = count, QueueName = _sqsQueueReader.QueueName, RegionName = _sqsQueueReader.RegionSystemName, }; _requestMessageAttributeNames.Add("content"); messages = await _sqsMiddleware.RunAsync(context, async ct => await _sqsQueueReader .GetMessagesAsync(count, _sqsWaitTime, _requestMessageAttributeNames, ct) .ConfigureAwait(false), linkedCts.Token) .ConfigureAwait(false); } finally { if (receiveTimeout.Token.IsCancellationRequested) { _logger.LogInformation( "Timed out while receiving messages from queue '{QueueName}' in region '{Region}'.", _sqsQueueReader.QueueName, _sqsQueueReader.RegionSystemName); } } return(messages); }
protected override async Task <IList <Message> > RunInnerAsync( ReceiveMessagesContext context, Func <CancellationToken, Task <IList <Message> > > func, CancellationToken stoppingToken) { try { var results = await func(stoppingToken).ConfigureAwait(false); _logger.LogTrace( "Polled for messages on queue '{QueueName}' in region '{Region}', and received {MessageCount} messages.", context.QueueName, context.RegionName, results.Count); return(results); } catch (OperationCanceledException ex) { _logger.LogTrace( ex, "Request to get more messages from queue was canceled for queue '{QueueName}' in region '{Region}'," + "likely because there are no messages in the queue. " + "This might have also been caused by the application shutting down.", context.QueueName, context.RegionName); } #pragma warning disable CA1031 catch (Exception ex) #pragma warning restore CA1031 { _logger.LogError( ex, "Error receiving messages on queue '{QueueName}' in region '{Region}'.", context.QueueName, context.RegionName); } return(Array.Empty <Message>()); }