Files
server/src/EventsProcessor/AzureQueueHostedService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
4.2 KiB
C#
Raw Normal View History

using System.Text.Json;
2020-01-10 08:33:13 -05:00
using Azure.Storage.Queues;
2019-08-04 20:04:04 -04:00
using Bit.Core;
using Bit.Core.Models.Data;
using Bit.Core.Services;
using Bit.Core.Utilities;
namespace Bit.EventsProcessor;
2022-08-29 16:06:55 -04:00
public class AzureQueueHostedService : IHostedService, IDisposable
{
private readonly ILogger<AzureQueueHostedService> _logger;
private readonly IConfiguration _configuration;
2022-08-29 16:06:55 -04:00
private Task _executingTask;
private CancellationTokenSource _cts;
private QueueClient _queueClient;
private IEventWriteService _eventWriteService;
2022-08-29 16:06:55 -04:00
public AzureQueueHostedService(
ILogger<AzureQueueHostedService> logger,
IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
2022-08-29 16:06:55 -04:00
}
public Task StartAsync(CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
_logger.LogInformation(Constants.BypassFiltersEventId, "Starting service.");
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
2020-01-10 08:33:13 -05:00
_executingTask = ExecuteAsync(_cts.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
2022-08-29 16:06:55 -04:00
}
public async Task StopAsync(CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
if (_executingTask == null)
{
return;
}
2019-07-11 21:48:44 -04:00
_logger.LogWarning("Stopping service.");
_cts.Cancel();
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
cancellationToken.ThrowIfCancellationRequested();
2022-08-29 16:06:55 -04:00
}
public void Dispose()
{ }
private async Task ExecuteAsync(CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
var storageConnectionString = _configuration["azureStorageConnectionString"];
if (string.IsNullOrWhiteSpace(storageConnectionString))
{
return;
}
var repo = new Core.Repositories.TableStorage.EventRepository(storageConnectionString);
_eventWriteService = new RepositoryEventWriteService(repo);
_queueClient = new QueueClient(storageConnectionString, "event");
while (!cancellationToken.IsCancellationRequested)
{
try
{
2020-01-10 08:33:13 -05:00
var messages = await _queueClient.ReceiveMessagesAsync(32);
if (messages.Value?.Any() ?? false)
{
foreach (var message in messages.Value)
{
await ProcessQueueMessageAsync(message.DecodeMessageText(), cancellationToken);
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
}
}
else
{
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
}
catch (Exception e)
2022-08-29 16:06:55 -04:00
{
_logger.LogError(e, "Exception occurred: " + e.Message);
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
2022-08-29 16:06:55 -04:00
}
}
2019-07-11 15:46:42 -04:00
_logger.LogWarning("Done processing.");
2022-08-29 16:06:55 -04:00
}
2019-07-11 15:46:42 -04:00
public async Task ProcessQueueMessageAsync(string message, CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
2019-07-11 15:46:42 -04:00
if (_eventWriteService == null || message == null || message.Length == 0)
2022-08-29 16:06:55 -04:00
{
2019-07-11 15:46:42 -04:00
return;
}
try
{
_logger.LogInformation("Processing message.");
var events = new List<IEvent>();
using var jsonDocument = JsonDocument.Parse(message);
var root = jsonDocument.RootElement;
if (root.ValueKind == JsonValueKind.Array)
{
var indexedEntities = root.ToObject<List<EventMessage>>()
.SelectMany(e => EventTableEntity.IndexEvent(e));
events.AddRange(indexedEntities);
}
else if (root.ValueKind == JsonValueKind.Object)
{
var eventMessage = root.ToObject<EventMessage>();
events.AddRange(EventTableEntity.IndexEvent(eventMessage));
}
2022-08-29 16:06:55 -04:00
await _eventWriteService.CreateManyAsync(events);
_logger.LogInformation("Processed message.");
2022-08-29 16:06:55 -04:00
}
catch (JsonException)
2022-08-29 16:06:55 -04:00
{
_logger.LogError("JsonReaderException: Unable to parse message.");
}
}
}