1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 08:32:50 -05:00

Reference event service implementation (#811)

* Reference event service implementation

* Fix IReferenceable implementation of Id

* add structure to event body
This commit is contained in:
Chad Scharf
2020-07-07 12:01:34 -04:00
committed by GitHub
parent b4524fbcb6
commit 7af50172e0
12 changed files with 248 additions and 3 deletions

View File

@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Bit.Core.Models.Business;
using Newtonsoft.Json;
namespace Bit.Core.Services
{
public class AzureQueueReferenceEventService : IReferenceEventService
{
private const string _queueName = "reference-events";
private readonly QueueClient _queueClient;
private readonly GlobalSettings _globalSettings;
private readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
};
public AzureQueueReferenceEventService (
GlobalSettings globalSettings)
{
_queueClient = new QueueClient(globalSettings.Storage.ConnectionString, _queueName);
_globalSettings = globalSettings;
}
public async Task RaiseEventAsync(ReferenceEvent referenceEvent)
{
await SendMessageAsync(referenceEvent);
}
private async Task SendMessageAsync(ReferenceEvent referenceEvent)
{
if (_globalSettings.SelfHosted || string.IsNullOrWhiteSpace(referenceEvent.ReferenceId))
{
// Ignore for self-hosted, OR, where there is no ReferenceId
return;
}
try
{
var message = JsonConvert.SerializeObject(referenceEvent, _jsonSerializerSettings);
await _queueClient.SendMessageAsync(message);
}
catch
{
// Ignore failure
}
}
}
}