/// <summary>
        /// Fetches any pending events from the current transaction and publishes these through the event bus one by one.
        /// Each event passes through several states, making it easier to track when and if an event fails to be published.
        /// If any failures are detected, publishing that event will NOT be retried (no logic for handling this exists at this point).
        /// </summary>
        /// <param name="transactionId">The transaction id of the DbContextTransaction the given events where created during.</param>
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            var pendingEvents = await _integrationEventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            foreach (var logEvent in pendingEvents)
            {
                try
                {
                    await _integrationEventLogService.MarkEventAsInProgressAsync(logEvent.EventId);

                    _logger.LogDebug("Event {0} status marked as in progress.", logEvent.EventId);

                    await _eventBus.Publish(logEvent.IntegrationEvent, logEvent.EventType);

                    _logger.LogDebug("Published event {0} with content: {1}", logEvent.EventId, logEvent.Content);

                    await _integrationEventLogService.MarkEventAsPublishedAsync(logEvent.EventId);

                    _logger.LogDebug("Event {0} status marked as published.", logEvent.EventId);
                }
                catch (Exception ex)
                {
                    await _integrationEventLogService.MarkEventAsFailedAsync(logEvent.EventId);

                    _logger.LogError(ex, "Event {0} status marked as failed", logEvent.EventId);
                }
            }
        }
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            foreach (var logEvt in pendingLogEvents)
            {
                _logger.LogInformation(
                    "----- Publishing integration event: {IntegrationEventId} - ({@IntegrationEvent})", logEvt.Id,
                    logEvt.IntegrationEvent);

                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.Id);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.Id);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId}", logEvt.Id);

                    await _eventLogService.MarkEventAsFailedAsync(logEvt.Id);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Publish the events through the event bus async
        /// </summary>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            // Retrieve all the pending events to be published
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            // Loop through each integration event
            foreach (var logEvt in pendingLogEvents)
            {
                // Log publishing the event
                _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", logEvt.EventId, Program.AppName, logEvt.IntegrationEvent);

                try
                {
                    // Mark the event as in progress
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    // Publish the event
                    _eventBus.Publish(logEvt.IntegrationEvent);
                    // Mark the event as published
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception ex)
                {
                    // In case of exception, log the exception
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId} from {AppName}", logEvt.EventId, Program.AppName);
                    // Mark the event as failed
                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }
        public async Task PublishEventsThroughEventBusAsync()
        {
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();

            foreach (var logEvt in pendingLogEvents)
            {
                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception)
                {
                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }
        public async Task PublishEventsThroughEventBusAsync()
        {
            IEnumerable <IntegrationEventLogEntry> pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();

            foreach (var logEvt in pendingLogEvents)
            {
                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId} from ProductService", logEvt.EventId);

                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }