diff --git a/src/Paramore.Brighter.Extensions.Hosting/TimedOutboxArchiver.cs b/src/Paramore.Brighter.Extensions.Hosting/TimedOutboxArchiver.cs index 78a368ab1d..1dcc1f41a0 100644 --- a/src/Paramore.Brighter.Extensions.Hosting/TimedOutboxArchiver.cs +++ b/src/Paramore.Brighter.Extensions.Hosting/TimedOutboxArchiver.cs @@ -64,7 +64,6 @@ private void Archive(object state) catch (Exception e) { s_logger.LogError(e, "Error while sweeping the outbox."); - throw; } s_logger.LogInformation("Outbox Sweeper sleeping"); diff --git a/src/Paramore.Brighter/OutboxArchiver.cs b/src/Paramore.Brighter/OutboxArchiver.cs index 157008be4f..90f2419ef4 100644 --- a/src/Paramore.Brighter/OutboxArchiver.cs +++ b/src/Paramore.Brighter/OutboxArchiver.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Paramore.Brighter.Logging; namespace Paramore.Brighter { @@ -14,6 +16,7 @@ public class OutboxArchiver private IAmAnOutboxSync _outboxSync; private IAmAnOutboxAsync _outboxAsync; private IAmAnArchiveProvider _archiveProvider; + private readonly ILogger _logger = ApplicationLogging.CreateLogger(); public OutboxArchiver(IAmAnOutbox outbox,IAmAnArchiveProvider archiveProvider, int batchSize = 100) { @@ -34,20 +37,29 @@ public OutboxArchiver(IAmAnOutbox outbox,IAmAnArchiveProvider archivePr public void Archive(int minimumAge) { var activity = ApplicationTelemetry.ActivitySource.StartActivity(ARCHIVEOUTBOX, ActivityKind.Server); - var age = TimeSpan.FromHours(minimumAge); - - var messages = _outboxSync.DispatchedMessages(age.Milliseconds, _batchSize); - - foreach (var message in messages) + + try { - _archiveProvider.ArchiveMessage(message); + var messages = _outboxSync.DispatchedMessages(age.Milliseconds, _batchSize); + + foreach (var message in messages) + { + _archiveProvider.ArchiveMessage(message); + } + + _outboxSync.Delete(messages.Select(e => e.Id).ToArray()); + } + catch (Exception e) + { + _logger.LogError(e, "Error while archiving from the outbox"); + throw; + } + finally + { + if(activity?.DisplayName == ARCHIVEOUTBOX) + activity.Dispose(); } - - _outboxSync.Delete(messages.Select(e => e.Id).ToArray()); - - if(activity?.DisplayName == ARCHIVEOUTBOX) - activity.Dispose(); } /// @@ -60,18 +72,29 @@ public async Task ArchiveAsync(int minimumAge, CancellationToken cancellationTok var activity = ApplicationTelemetry.ActivitySource.StartActivity(ARCHIVEOUTBOX, ActivityKind.Server); var age = TimeSpan.FromHours(minimumAge); - - var messages = await _outboxAsync.DispatchedMessagesAsync(age.Milliseconds, _batchSize, cancellationToken: cancellationToken); - foreach (var message in messages) + try { - await _archiveProvider.ArchiveMessageAsync(message, cancellationToken); + var messages = await _outboxAsync.DispatchedMessagesAsync(age.Milliseconds, _batchSize, + cancellationToken: cancellationToken); + + foreach (var message in messages) + { + await _archiveProvider.ArchiveMessageAsync(message, cancellationToken); + } + + await _outboxAsync.DeleteAsync(cancellationToken, messages.Select(e => e.Id).ToArray()); + } + catch (Exception e) + { + _logger.LogError(e, "Error while archiving from the outbox"); + throw; + } + finally + { + if(activity?.DisplayName == ARCHIVEOUTBOX) + activity.Dispose(); } - - await _outboxAsync.DeleteAsync(cancellationToken, messages.Select(e => e.Id).ToArray()); - - if(activity?.DisplayName == ARCHIVEOUTBOX) - activity.Dispose(); } } }