コード例 #1
0
        protected virtual async Task ProcessInitializingHandler(PartitionInitializingEventArgs initArgs)
        {
            var partitionId = initArgs.PartitionId;

            Logger.LogTrace($"Initializing partition {partitionId}");

            if (initArgs.CancellationToken.IsCancellationRequested)
            {
                // Log the condition where the initialization handler is called, an the PartitionInitializingEventArgs contains a cancellation request.
                Logger.LogTrace($"PartitionInitializingEventArgs contain a cancellation request {partitionId}");
            }

            try
            {
                var checkpoint = await CheckpointClient.GetCheckpointForPartitionAsync(partitionId, initArgs.CancellationToken);

                initArgs.DefaultStartingPosition = EventPosition.FromEnqueuedTime(checkpoint.LastProcessed);
                Logger.LogTrace($"Starting to read partition {partitionId} from checkpoint {checkpoint.LastProcessed}");
                Logger.LogMetric(EventMetrics.EventHubPartitionInitialized(partitionId), 1);
            }
#pragma warning disable CA1031
            catch (Exception ex)
#pragma warning restore CA1031
            {
                Logger.LogTrace($"Failed to initialize partition {partitionId} from checkpoint");

                EventHubExceptionProcessor.ProcessException(ex, Logger, errorMetricName: EventHubErrorCode.EventHubPartitionInitFailed.ToString());
            }
        }
コード例 #2
0
        public void GivenExceptionType_WhenCustomizeException_ThenCustomExceptionTypeReturned_Test(Type exType, object[] param, Type customExType)
        {
            var ex = Activator.CreateInstance(exType, param) as Exception;

            var(customEx, errName) = EventHubExceptionProcessor.CustomizeException(ex);

            Assert.IsType(customExType, customEx);
        }
コード例 #3
0
        public void GivenExceptionType_WhenProcessException_ThenExceptionLoggedAndEventHubErrorMetricLogged_Test(Exception ex, string expectedErrorMetricName, string expectedErrorSource = null, string expectedErrorTypeName = nameof(ErrorType.EventHubError))
        {
            var logger = Substitute.For <ITelemetryLogger>();

            EventHubExceptionProcessor.ProcessException(ex, logger);

            logger.ReceivedWithAnyArgs(1).LogError(ex);
            logger.Received(1).LogMetric(Arg.Is <Metric>(m =>
                                                         ValidateEventHubErrorMetricProperties(m, expectedErrorMetricName, expectedErrorTypeName, expectedErrorSource)),
                                         1);
        }
コード例 #4
0
        public void GivenExceptionTypeAndErrorMetricName_WhenProcessExpection_ThenExceptionLoggedAndErrorMetricNameLogged_Test(Type exType)
        {
            var logger = Substitute.For <ITelemetryLogger>();
            var ex     = Activator.CreateInstance(exType) as Exception;

            EventHubExceptionProcessor.ProcessException(ex, logger, errorMetricName: EventHubErrorCode.EventHubPartitionInitFailed.ToString());

            logger.Received(1).LogError(ex);
            logger.Received(1).LogMetric(Arg.Is <Metric>(m =>
                                                         m.Name.Equals(EventHubErrorCode.EventHubPartitionInitFailed.ToString()) &&
                                                         ValidateEventHubErrorMetricProperties(m, ErrorType.EventHubError)),
                                         1);
        }
コード例 #5
0
        public void GivenExceptionType_WhenProcessExpection_ThenExceptionLoggedAndEventHubErrorMetricLogged_Test(Type exType, object[] param, string expectedErrorMetricName, string expectedErrorTypeName = nameof(ErrorType.EventHubError))
        {
            var       logger = Substitute.For <ITelemetryLogger>();
            Exception ex     = Activator.CreateInstance(exType, param) as Exception;

            EventHubExceptionProcessor.ProcessException(ex, logger);

            logger.ReceivedWithAnyArgs(1).LogError(ex);
            logger.Received(1).LogMetric(Arg.Is <Metric>(m =>
                                                         m.Name.Equals(expectedErrorMetricName) &&
                                                         ValidateEventHubErrorMetricProperties(m, expectedErrorTypeName)),
                                         1);
        }
コード例 #6
0
        protected virtual Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
        {
            EventHubExceptionProcessor.ProcessException(eventArgs.Exception, Logger);

            return(Task.CompletedTask);
        }
コード例 #7
0
        public void GivenExceptionType_WhenCustomizeException_ThenCustomExceptionTypeReturned_Test(Exception ex, Type customExType)
        {
            var(customEx, errName) = EventHubExceptionProcessor.CustomizeException(ex);

            Assert.IsType(customExType, customEx);
        }
コード例 #8
0
        public async Task RunAsync(EventProcessorClient processor, CancellationToken ct)
        {
            EnsureArg.IsNotNull(processor);

            // Reset previous checkpoints corresponding to an older source event hub (i.e. applicable if the source event hub changes)
            await _checkpointClient.ResetCheckpointsAsync();

            // Processes two types of events
            // 1) Event hub events
            // 2) Maximum wait events. These are generated when we have not received an event hub
            //    event for a certain time period and this event is used to flush events in the current window.
            async Task ProcessEventHandler(ProcessEventArgs eventArgs)
            {
                IEventMessage evt;

                if (eventArgs.HasEvent)
                {
                    evt = EventMessageFactory.CreateEvent(eventArgs);
                }
                else
                {
                    evt = new MaximumWaitEvent(eventArgs.Partition.PartitionId, DateTime.UtcNow);
                }

                await _eventConsumerService.ConsumeEvent(evt);
            }

            // todo: consider retry
            Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
            {
                EventHubExceptionProcessor.ProcessException(eventArgs.Exception, _logger);

                return(Task.CompletedTask);
            }

            async Task ProcessInitializingHandler(PartitionInitializingEventArgs initArgs)
            {
                var partitionId = initArgs.PartitionId;

                _logger.LogTrace($"Initializing partition {partitionId}");

                try
                {
                    var checkpoint = await _checkpointClient.GetCheckpointForPartitionAsync(partitionId);

                    initArgs.DefaultStartingPosition = EventPosition.FromEnqueuedTime(checkpoint.LastProcessed);
                    _logger.LogTrace($"Starting to read partition {partitionId} from checkpoint {checkpoint.LastProcessed}");
                    _logger.LogMetric(EventMetrics.EventHubPartitionInitialized(partitionId), 1);
                }
#pragma warning disable CA1031
                catch (Exception ex)
#pragma warning restore CA1031
                {
                    _logger.LogTrace($"Failed to initialize partition {partitionId} from checkpoint");
                    EventHubExceptionProcessor.ProcessException(ex, _logger, errorMetricName: EventHubErrorCode.EventHubPartitionInitFailed.ToString());
                }
            }

            processor.ProcessEventAsync          += ProcessEventHandler;
            processor.ProcessErrorAsync          += ProcessErrorHandler;
            processor.PartitionInitializingAsync += ProcessInitializingHandler;

            try
            {
                Console.WriteLine($"Starting event hub processor at {DateTime.UtcNow}");
                await processor.StartProcessingAsync(ct);

                // Wait indefinitely until cancellation is requested
                ct.WaitHandle.WaitOne();

                await processor.StopProcessingAsync();
            }
            finally
            {
                processor.ProcessEventAsync          -= ProcessEventHandler;
                processor.ProcessErrorAsync          -= ProcessErrorHandler;
                processor.PartitionInitializingAsync -= ProcessInitializingHandler;
            }
        }