Пример #1
0
        internal void OnPartitionsRevoked()
        {
            lock (_channelsLock)
            {
                StopCore();
                AsyncHelper.RunSynchronously(() => WaitUntilConsumingStoppedAsync(CancellationToken.None));

                if (!Endpoint.Configuration.IsAutoCommitEnabled)
                {
                    CommitOffsets();
                }
            }

            SequenceStores.ForEach(store => store.Dispose());
            SequenceStores.Clear();
        }
Пример #2
0
        /// <inheritdoc cref="IConsumer.DisconnectAsync" />
        public async Task DisconnectAsync()
        {
            if (!IsConnected)
            {
                return;
            }

            IsDisconnecting = true;

            // Ensure that StopCore is called in any case to avoid deadlocks (when the consumer loop is initialized
            // but not started)
            if (IsConsuming)
            {
                Stop();
            }
            else
            {
                StopCore();
            }

            if (SequenceStores.Count > 0)
            {
                await SequenceStores
                .SelectMany(store => store)
                .ToList()
                .ForEachAsync(sequence => sequence.AbortAsync(SequenceAbortReason.ConsumerAborted))
                .ConfigureAwait(false);
            }

            using (var cancellationTokenSource = new CancellationTokenSource(ConsumerStopWaitTimeout))
            {
                _logger.LogTrace(IntegrationEventIds.LowLevelTracing, "Waiting until consumer stops...");

                try
                {
                    await WaitUntilConsumingStoppedAsync(cancellationTokenSource.Token).ConfigureAwait(false);

                    _logger.LogTrace(IntegrationEventIds.LowLevelTracing, "Consumer stopped.");
                }
                catch (OperationCanceledException)
                {
                    _logger.LogError(
                        IntegrationEventIds.LowLevelTracing,
                        "The timeout elapsed before the consumer stopped.");
                }
            }

            await DisconnectCoreAsync().ConfigureAwait(false);

            SequenceStores.ForEach(store => store.Dispose());
            SequenceStores.Clear();

            IsConnected = false;
            _statusInfo.SetDisconnected();
            _logger.LogDebug(
                IntegrationEventIds.ConsumerDisconnected,
                "Disconnected consumer from endpoint {endpoint}.",
                Endpoint.Name);

            IsDisconnecting = false;
        }