2018-08-02 18:14:33 +02:00
|
|
|
|
using System;
|
2018-08-02 23:23:37 +02:00
|
|
|
|
using System.Linq;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-08-02 23:23:37 +02:00
|
|
|
|
using Bit.Core;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-01-10 14:33:13 +01:00
|
|
|
|
using Azure.Storage.Queues;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
|
2018-08-16 19:45:31 +02:00
|
|
|
|
namespace Bit.Notifications
|
2018-08-02 18:14:33 +02:00
|
|
|
|
{
|
2018-08-03 05:13:06 +02:00
|
|
|
|
public class AzureQueueHostedService : IHostedService, IDisposable
|
2018-08-02 18:14:33 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2018-08-16 19:45:31 +02:00
|
|
|
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
2018-08-02 23:23:37 +02:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
|
2018-08-02 23:23:37 +02:00
|
|
|
|
private Task _executingTask;
|
|
|
|
|
private CancellationTokenSource _cts;
|
2020-01-10 14:33:13 +01:00
|
|
|
|
private QueueClient _queueClient;
|
2018-08-02 23:23:37 +02:00
|
|
|
|
|
2018-08-16 19:45:31 +02:00
|
|
|
|
public AzureQueueHostedService(
|
2018-08-21 15:37:09 +02:00
|
|
|
|
ILogger<AzureQueueHostedService> logger,
|
2018-08-16 19:45:31 +02:00
|
|
|
|
IHubContext<NotificationsHub> hubContext,
|
2018-08-02 23:23:37 +02:00
|
|
|
|
GlobalSettings globalSettings)
|
2018-08-02 18:14:33 +02:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_hubContext = hubContext;
|
2018-08-02 23:23:37 +02:00
|
|
|
|
_globalSettings = globalSettings;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2018-08-02 23:23:37 +02:00
|
|
|
|
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
_executingTask = ExecuteAsync(_cts.Token);
|
|
|
|
|
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
2018-08-02 18:14:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 23:23:37 +02:00
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
2018-08-02 18:14:33 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (_executingTask == null)
|
2018-08-02 23:23:37 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-12 03:48:44 +02:00
|
|
|
|
_logger.LogWarning("Stopping service.");
|
2018-08-02 23:23:37 +02:00
|
|
|
|
_cts.Cancel();
|
|
|
|
|
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2018-08-02 18:14:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 23:23:37 +02:00
|
|
|
|
public void Dispose()
|
2018-08-21 15:37:09 +02:00
|
|
|
|
{ }
|
2018-08-02 18:14:33 +02:00
|
|
|
|
|
2018-08-02 23:23:37 +02:00
|
|
|
|
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
2018-08-02 18:14:33 +02:00
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
_queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
|
2020-03-27 19:36:37 +01:00
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
2018-08-02 23:23:37 +02:00
|
|
|
|
{
|
2019-05-08 04:06:05 +02:00
|
|
|
|
try
|
2018-08-02 23:23:37 +02:00
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
var messages = await _queueClient.ReceiveMessagesAsync(32);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (messages.Value?.Any() ?? false)
|
2018-08-02 23:23:37 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
foreach (var message in messages.Value)
|
2019-05-08 04:06:05 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await HubHelpers.SendNotificationToHubAsync(
|
2020-01-10 14:33:13 +01:00
|
|
|
|
message.MessageText, _hubContext, cancellationToken);
|
|
|
|
|
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
2019-05-08 04:06:05 +02:00
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
catch (Exception e)
|
2019-05-08 04:06:05 +02:00
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Error processing dequeued message: " +
|
2020-03-06 03:50:39 +01:00
|
|
|
|
$"{message.MessageId} x{message.DequeueCount}. {e.Message}", e);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (message.DequeueCount > 2)
|
2019-05-08 04:06:05 +02:00
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
2019-05-08 04:06:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
|
2018-08-02 23:23:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
catch (Exception e)
|
2018-08-24 17:32:53 +02:00
|
|
|
|
{
|
2019-05-08 04:06:05 +02:00
|
|
|
|
_logger.LogError("Error processing messages.", e);
|
2018-08-24 17:32:53 +02:00
|
|
|
|
}
|
2018-08-02 23:23:37 +02:00
|
|
|
|
}
|
2019-07-11 21:46:42 +02:00
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Done processing.");
|
2018-08-02 18:14:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|