From 4e65275a2f9a2349ae654fa0e7e6fb24d4e12e04 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 7 May 2019 22:06:05 -0400 Subject: [PATCH] catch errors when processing notification messages --- src/Notifications/AzureQueueHostedService.cs | 36 +++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Notifications/AzureQueueHostedService.cs b/src/Notifications/AzureQueueHostedService.cs index a3af1f046b..520f75cfa9 100644 --- a/src/Notifications/AzureQueueHostedService.cs +++ b/src/Notifications/AzureQueueHostedService.cs @@ -60,19 +60,39 @@ namespace Bit.Notifications while(!cancellationToken.IsCancellationRequested) { - var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1), - null, null, cancellationToken); - if(messages.Any()) + try { - foreach(var message in messages) + var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1), + null, null, cancellationToken); + if(messages.Any()) { - await HubHelpers.SendNotificationToHubAsync(message.AsString, _hubContext, cancellationToken); - await _queue.DeleteMessageAsync(message); + foreach(var message in messages) + { + try + { + await HubHelpers.SendNotificationToHubAsync( + message.AsString, _hubContext, cancellationToken); + await _queue.DeleteMessageAsync(message); + } + catch(Exception e) + { + _logger.LogError("Error processing dequeued message: " + + $"{message.Id} x{message.DequeueCount}.", e); + if(message.DequeueCount > 2) + { + await _queue.DeleteMessageAsync(message); + } + } + } + } + else + { + await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } } - else + catch(Exception e) { - await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); + _logger.LogError("Error processing messages.", e); } } }