public async void GivenBatchServiceRunning_WhenFlushWindowHasNotPassedAndMaxWaitEventReceived_ThenDoNotFlush_Test()
        {
            var eventReader = new EventBatchingService(_eventConsumerService, _options, _checkpointClient, _logger);

            var firstEventTime = DateTime.UtcNow.AddSeconds(-30);
            var firstEvent     = new EventMessage("0", new ReadOnlyMemory <byte>(), 1, 1, firstEventTime, new Dictionary <string, object>(), new ReadOnlyDictionary <string, object>(new Dictionary <string, object>()));
            await eventReader.ConsumeEvent(firstEvent);

            var maxWaitEvent = new MaximumWaitEvent("0", DateTime.UtcNow.AddSeconds(-10));
            await eventReader.ConsumeEvent(maxWaitEvent);

            await _eventConsumerService.Received(0).ConsumeEvents(Arg.Any <IEnumerable <EventMessage> >());
        }
示例#2
0
        // 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.
        protected virtual async Task ProcessEventHandler(ProcessEventArgs eventArgs)
        {
            if (eventArgs.CancellationToken.IsCancellationRequested)
            {
                // The event arguments contain a cancellation token that the EventProcessorClient uses to signal the handler that processing should cease as soon as possible.
                // This is most commonly seen when the EventProcessorClient is stopping or has encountered an unrecoverable problem.
                Logger.LogTrace($"ProcessEventArgs contain a cancellation request {eventArgs.Partition.PartitionId}");
                return;
            }

            IEventMessage evt;

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

            await EventConsumerService.ConsumeEvent(evt);
        }
示例#3
0
        public async Task RunAsync(EventProcessorClient processor, CancellationToken ct)
        {
            EnsureArg.IsNotNull(processor);

            // 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)
            {
                _logger.LogError(eventArgs.Exception);
                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(), 1);
                }
#pragma warning disable CA1031
                catch (Exception ex)
#pragma warning restore CA1031
                {
                    _logger.LogTrace($"Failed to initialize partition {partitionId} from checkpoint");
                    _logger.LogError(ex);
                }
            }

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

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

                while (!ct.IsCancellationRequested)
                {
                }

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