Ejemplo n.º 1
0
        public async Task Connect(CancellationToken cancellationToken)
        {
            var logContext   = _context.LogContext;
            var inputAddress = _context.InputAddress;

            LogContext.SetCurrentIfNull(logContext);

            _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            async Task ProcessEventAsync(ProcessEventArgs arg)
            {
                LogContext.SetCurrentIfNull(logContext);

                try
                {
                    await _executor.Push(() => _transport.Handle(arg, cancellationToken), _cancellationTokenSource.Token).ConfigureAwait(false);
                }
                catch (OperationCanceledException e) when(e.CancellationToken == _cancellationTokenSource.Token)
                {
                }
            }

            async Task ProcessErrorAsync(ProcessErrorEventArgs arg)
            {
                var faultedEvent = new ReceiveTransportFaultedEvent(inputAddress, arg.Exception);

                await _context.TransportObservers.Faulted(faultedEvent).ConfigureAwait(false);

                await _context.EndpointObservers.Faulted(new ReceiveEndpointFaultedEvent(faultedEvent, this)).ConfigureAwait(false);
            }

            _processor.ProcessEventAsync += ProcessEventAsync;
            _processor.ProcessErrorAsync += ProcessErrorAsync;

            try
            {
                await _blobContainerClient.CreateIfNotExistsAsync(cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            catch (RequestFailedException exception)
            {
                LogContext.Warning?.Log(exception, "Azure Blob Container does not exist: {Address}", _blobContainerClient.Uri);
            }

            LogContext.Debug?.Log("EventHub processor starting: {EventHubName}", _processor.EventHubName);

            await _processor.StartProcessingAsync(cancellationToken).ConfigureAwait(false);

            await _context.TransportObservers.Ready(new ReceiveTransportReadyEvent(inputAddress)).ConfigureAwait(false);

            var endpointReadyEvent = new ReceiveEndpointReadyEvent(_context.InputAddress, this, true);

            _started.TrySetResult(endpointReadyEvent);

            await _context.EndpointObservers.Ready(endpointReadyEvent).ConfigureAwait(false);
        }
        public Task Start(CancellationToken cancellationToken)
        {
            var logContext = _context.LogContext;

            LogContext.SetCurrentIfNull(logContext);

            _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            // TODO this feels like it needs to be more resilient, right now one error and it exits

            _consumerTask = Task.Run(async() =>
            {
                var inputAddress = _context.InputAddress;
                await _context.TransportObservers.Ready(new ReceiveTransportReadyEvent(inputAddress)).ConfigureAwait(false);

                var endpointReadyEvent = new ReceiveEndpointReadyEvent(_context.InputAddress, this, true);
                _started.TrySetResult(endpointReadyEvent);

                await _context.EndpointObservers.Ready(endpointReadyEvent).ConfigureAwait(false);

                LogContext.Debug?.Log("Kafka consumer started: {InputAddress}", _context.InputAddress);

                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    LogContext.SetCurrentIfNull(logContext);
                    try
                    {
                        ConsumeResult <TKey, TValue> message = _consumer.Consume(_cancellationTokenSource.Token);

                        await _executor.Push(() => _transport.Handle(message, cancellationToken), _cancellationTokenSource.Token).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException e) when(e.CancellationToken == _cancellationTokenSource.Token)
                    {
                    }
                    catch (Exception exception)
                    {
                        var faultedEvent = new ReceiveTransportFaultedEvent(inputAddress, exception);

                        await _context.TransportObservers.Faulted(faultedEvent).ConfigureAwait(false);
                        await _context.EndpointObservers.Faulted(new ReceiveEndpointFaultedEvent(faultedEvent, this)).ConfigureAwait(false);

                        throw;
                    }
                }
            }, cancellationToken);

            _consumer.Subscribe(_topic);

            return(_consumerTask.IsCompleted ? _consumerTask : TaskUtil.Completed);
        }