2018-08-02 12:14:33 -04:00
|
|
|
|
using System;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using System.Linq;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Bit.Core;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Microsoft.WindowsAzure.Storage;
|
|
|
|
|
|
using Microsoft.WindowsAzure.Storage.Queue;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
|
2018-08-16 13:45:31 -04:00
|
|
|
|
namespace Bit.Notifications
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
2018-08-02 23:13:06 -04:00
|
|
|
|
public class AzureQueueHostedService : IHostedService, IDisposable
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger _logger;
|
2018-08-16 13:45:31 -04:00
|
|
|
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private Task _executingTask;
|
|
|
|
|
|
private CancellationTokenSource _cts;
|
|
|
|
|
|
private CloudQueue _queue;
|
|
|
|
|
|
|
2018-08-16 13:45:31 -04:00
|
|
|
|
public AzureQueueHostedService(
|
2018-08-21 09:37:09 -04:00
|
|
|
|
ILogger<AzureQueueHostedService> logger,
|
2018-08-16 13:45:31 -04:00
|
|
|
|
IHubContext<NotificationsHub> hubContext,
|
2018-08-02 17:23:37 -04:00
|
|
|
|
GlobalSettings globalSettings)
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_hubContext = hubContext;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
|
_executingTask = ExecuteAsync(_cts.Token);
|
|
|
|
|
|
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
2018-08-02 12:14:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
if(_executingTask == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
_cts.Cancel();
|
|
|
|
|
|
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2018-08-02 12:14:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public void Dispose()
|
2018-08-21 09:37:09 -04:00
|
|
|
|
{ }
|
2018-08-02 12:14:33 -04:00
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
2018-08-16 13:45:31 -04:00
|
|
|
|
var storageAccount = CloudStorageAccount.Parse(_globalSettings.Notifications.ConnectionString);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
var queueClient = storageAccount.CreateCloudQueueClient();
|
2018-08-16 13:35:16 -04:00
|
|
|
|
_queue = queueClient.GetQueueReference("notifications");
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
2018-08-24 11:32:53 -04:00
|
|
|
|
while(!cancellationToken.IsCancellationRequested)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2019-05-07 22:06:05 -04:00
|
|
|
|
try
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2019-05-07 22:06:05 -04:00
|
|
|
|
var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1),
|
|
|
|
|
|
null, null, cancellationToken);
|
|
|
|
|
|
if(messages.Any())
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2019-05-07 22:06:05 -04:00
|
|
|
|
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);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-05-07 22:06:05 -04:00
|
|
|
|
catch(Exception e)
|
2018-08-24 11:32:53 -04:00
|
|
|
|
{
|
2019-05-07 22:06:05 -04:00
|
|
|
|
_logger.LogError("Error processing messages.", e);
|
2018-08-24 11:32:53 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
2019-07-11 15:46:42 -04:00
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Done processing.");
|
2018-08-02 12:14:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|