Files
server/src/Core/Services/Implementations/AzureQueueReferenceEventService.cs

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

49 lines
1.4 KiB
C#
Raw Normal View History

using System.Text;
using System.Text.Json;
using Azure.Storage.Queues;
using Bit.Core.Models.Business;
using Bit.Core.Settings;
using Bit.Core.Utilities;
namespace Bit.Core.Services;
2022-08-29 14:53:16 -04:00
public class AzureQueueReferenceEventService : IReferenceEventService
{
private const string _queueName = "reference-events";
2022-08-29 14:53:16 -04:00
private readonly QueueClient _queueClient;
private readonly GlobalSettings _globalSettings;
2022-08-29 14:53:16 -04:00
public AzureQueueReferenceEventService(
GlobalSettings globalSettings)
{
_queueClient = new QueueClient(globalSettings.Events.ConnectionString, _queueName);
_globalSettings = globalSettings;
2022-08-29 14:53:16 -04:00
}
public async Task RaiseEventAsync(ReferenceEvent referenceEvent)
2022-08-29 14:53:16 -04:00
{
await SendMessageAsync(referenceEvent);
2022-08-29 14:53:16 -04:00
}
private async Task SendMessageAsync(ReferenceEvent referenceEvent)
2022-08-29 14:53:16 -04:00
{
if (_globalSettings.SelfHosted)
{
// Ignore for self-hosted
return;
}
try
{
var message = JsonSerializer.Serialize(referenceEvent, JsonHelpers.IgnoreWritingNullAndCamelCase);
// Messages need to be base64 encoded
var encodedMessage = Convert.ToBase64String(Encoding.UTF8.GetBytes(message));
await _queueClient.SendMessageAsync(encodedMessage);
}
catch
{
// Ignore failure
}
}
}