2022-06-29 19:46:41 -04:00
|
|
|
|
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;
|
2019-03-02 12:15:28 -05:00
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
|
using Bit.Core.Services;
|
2021-07-02 17:11:33 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.EventsProcessor;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-03-02 12:15:28 -05:00
|
|
|
|
public class AzureQueueHostedService : IHostedService, IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<AzureQueueHostedService> _logger;
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-03-02 12:15:28 -05:00
|
|
|
|
private Task _executingTask;
|
|
|
|
|
|
private CancellationTokenSource _cts;
|
|
|
|
|
|
private QueueClient _queueClient;
|
|
|
|
|
|
private IEventWriteService _eventWriteService;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-03-02 12:15:28 -05:00
|
|
|
|
public AzureQueueHostedService(
|
|
|
|
|
|
ILogger<AzureQueueHostedService> logger,
|
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_configuration = configuration;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-02 12:15:28 -05:00
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, "Starting service.");
|
|
|
|
|
|
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
2020-01-10 08:33:13 -05:00
|
|
|
|
_executingTask = ExecuteAsync(_cts.Token);
|
2019-03-02 12:15:28 -05:00
|
|
|
|
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-02 12:15:28 -05:00
|
|
|
|
if (_executingTask == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-07-11 21:48:44 -04:00
|
|
|
|
_logger.LogWarning("Stopping service.");
|
2019-03-02 12:15:28 -05:00
|
|
|
|
_cts.Cancel();
|
|
|
|
|
|
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-02 12:15:28 -05: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);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (messages.Value?.Any() ?? false)
|
2019-03-02 12:15:28 -05:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var message in messages.Value)
|
2019-07-11 15:43:20 -04:00
|
|
|
|
{
|
|
|
|
|
|
await ProcessQueueMessageAsync(message.DecodeMessageText(), cancellationToken);
|
|
|
|
|
|
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
2019-03-02 12:15:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else
|
2019-03-02 12:15:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
catch (Exception e)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-07-11 15:43:20 -04:00
|
|
|
|
_logger.LogError(e, "Exception occurred: " + e.Message);
|
2019-03-02 12:15:28 -05:00
|
|
|
|
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;
|
2019-03-02 12:15:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
_logger.LogInformation("Processing message.");
|
|
|
|
|
|
var events = new List<IEvent>();
|
2019-03-02 12:15:28 -05:00
|
|
|
|
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using var jsonDocument = JsonDocument.Parse(message);
|
|
|
|
|
|
var root = jsonDocument.RootElement;
|
|
|
|
|
|
if (root.ValueKind == JsonValueKind.Array)
|
2019-03-02 12:15:28 -05:00
|
|
|
|
{
|
2022-01-21 09:36:25 -05:00
|
|
|
|
var indexedEntities = root.ToObject<List<EventMessage>>()
|
2019-03-02 12:15:28 -05:00
|
|
|
|
.SelectMany(e => EventTableEntity.IndexEvent(e));
|
|
|
|
|
|
events.AddRange(indexedEntities);
|
|
|
|
|
|
}
|
2022-01-21 09:36:25 -05:00
|
|
|
|
else if (root.ValueKind == JsonValueKind.Object)
|
2019-03-02 12:15:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
var eventMessage = root.ToObject<EventMessage>();
|
|
|
|
|
|
events.AddRange(EventTableEntity.IndexEvent(eventMessage));
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-03-02 12:15:28 -05:00
|
|
|
|
await _eventWriteService.CreateManyAsync(events);
|
|
|
|
|
|
_logger.LogInformation("Processed message.");
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-01-21 09:36:25 -05:00
|
|
|
|
catch (JsonException)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-02 12:15:28 -05:00
|
|
|
|
_logger.LogError("JsonReaderException: Unable to parse message.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|