예제 #1
0
        private async Task WaitUntilAllMessagesAreConsumedAsync(string[]?topicNames, TimeSpan?timeout = null)
        {
            using var cancellationTokenSource = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(30));

            var topics = topicNames == null
                ? _topics.ToList()
                : _topics.Where(topic => topicNames.Contains(topic.Name)).ToList();

            try
            {
                do
                {
                    await WaitUntilOutboxIsEmptyAsync(cancellationTokenSource.Token).ConfigureAwait(false);

                    await Task.WhenAll(
                        topics.Select(
                            topic =>
                            topic.WaitUntilAllMessagesAreConsumedAsync(cancellationTokenSource.Token)))
                    .ConfigureAwait(false);
                }while (await _outboxReader.GetLengthAsync().ConfigureAwait(false) > 0); // Loop until the outbox is empty since the consumers may produce new messages
            }
            catch (OperationCanceledException)
            {
                _logger.LogWarning("The timeout elapsed before all messages could be consumed and processed.");
            }
        }
        /// <inheritdoc cref="IOutboundQueueHealthCheckService.CheckIsHealthyAsync" />
        public async Task <bool> CheckIsHealthyAsync(TimeSpan?maxAge = null, int?maxQueueLength = null)
        {
            if (maxQueueLength != null &&
                await _queueReader.GetLengthAsync().ConfigureAwait(false) > maxQueueLength)
            {
                return(false);
            }

            return(await _queueReader.GetMaxAgeAsync().ConfigureAwait(false) <= (maxAge ?? DefaultMaxAge));
        }