private async Task ProcessPendingMessages(CancellationToken cancellationToken) { var messages = await _outboxRepository.ReadMessagesToProcess(cancellationToken); foreach (var message in messages) { try { await _publisher.PublishAsync(message, cancellationToken); await _outboxRepository.MarkAsSentAsync(message, cancellationToken); } catch (Exception e) { _logger.LogError(e, $"an error has occurred while processing Saga State outbox: {e.Message}"); } } }
public async Task ProcessPendingMessagesAsync(CancellationToken cancellationToken = default) { var messages = await _outboxRepository.ReadMessagesToProcess(cancellationToken); foreach (var message in messages) { try { var lockId = await _outboxRepository.LockAsync(message, cancellationToken); await _publisher.PublishAsync(message, cancellationToken); await _outboxRepository.ReleaseAsync(message, lockId, cancellationToken); } catch (LockException e) { _logger.LogDebug(e, $"message '{message.GetType().FullName}' - '{message.Id}' was already locked by another producer. {e.Message}"); } catch (Exception e) { _logger.LogError(e, $"an error has occurred while processing Saga State outbox: {e.Message}"); } } }