private async Task <bool> ReceiveAndProcessEventsAsync(EventStreamUpdated notification, IEventStreamConsumer consumer) { var retryProcessing = true; var commitProcessing = false; var receivingResult = await consumer.ReceiveEventsAsync(); if (receivingResult == ReceivingResultCode.EventsReceived) { var processingResult = await TryProcessEventFromConsumerAsync(consumer, notification.ToVersion); retryProcessing = !processingResult.IsSuccessful; commitProcessing = processingResult.ShouldCommitProcessing; } else if (receivingResult == ReceivingResultCode.EmptyStream) { retryProcessing = false; } if (commitProcessing) { await consumer.CommitProcessedStreamVersionAsync(); } if (retryProcessing) { ListenerLogger.Warning( "Processing notification ({NotificationId}, {NotificationType}) was unsuccessful {Code}. Going to try later.", notification.NotificationId, notification.NotificationType, receivingResult); } return(retryProcessing); }
protected override async Task <EventProcessingResult> TryProcessEventFromConsumerAsync( IEventStreamConsumer consumer, StreamVersion notificationStreamVersion) { foreach (var journaledEvent in consumer.EnumerateEvents()) { var failed = false; try { await ProcessEventAsync(journaledEvent); } catch (Exception exception) { ListenerLogger.Error( exception, "Processing event {EventId} of type {EventType} from stream {Stream} failed.", journaledEvent.EventId, journaledEvent.EventTypeName, consumer.StreamName); failed = true; } if (failed) { await consumer.CommitProcessedStreamVersionAsync(skipCurrent : true); return(new EventProcessingResult(false, false)); } } return(new EventProcessingResult(true, true)); }
protected override async Task <EventProcessingResult> TryProcessEventFromConsumerAsync( IEventStreamConsumer consumer, StreamVersion notificationStreamVersion) { try { await ProcessEventBatchAsync(consumer.EnumerateEvents().ToArray()); return(new EventProcessingResult(true, true)); } catch (Exception exception) { ListenerLogger.Error( exception, "Processing event batch from stream {Stream} failed.", consumer.StreamName); return(new EventProcessingResult(false, false)); } }
public async Task On(EventStreamUpdated notification) { Require.NotNull(notification, "notification"); AssertSubscriptionWasBound(); m_processingCountdown.AddCount(); var retryProcessing = false; try { var consumer = await m_subscription.CreateSubscriptionConsumerAsync(notification.StreamName); retryProcessing = await ReceiveAndProcessEventsAsync(notification, consumer); await consumer.CloseAsync(); } catch (Exception exception) { ListenerLogger.Error( exception, "Processing notification ({NotificationId}, {NotificationType}) from {Stream} failed.", notification.NotificationId, notification.NotificationType, notification.StreamName); retryProcessing = true; } finally { m_processingCountdown.Signal(); } if (retryProcessing) { await m_subscription.RetryNotificationProcessingAsync(notification); } }