mirror of
https://github.com/bitwarden/server.git
synced 2026-02-02 23:23:15 +08:00
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
|
|
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
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|