private async Task PublishPendingIntegrationEvents(CancellationToken cancellationToken) { var pendingLogEvents = await _eventLogService .RetrieveScopedEventsPendingToPublishAsync(); foreach (var logEvt in pendingLogEvents) { try { await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId); _logger.LogInformation("----- Publishing integration event: {IntegrationEventId}", logEvt.EventId); await _eventsPublisher.Publish(logEvt.IntegrationEvent, cancellationToken); _logger.LogInformation("----- Published integration event: {IntegrationEventId}", logEvt.EventId); await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId); } catch (Exception ex) { _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId}", logEvt.EventId); await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId); } } }