2020-07-07 12:01:34 -04:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2020-08-03 15:22:38 -04:00
|
|
|
|
_queueClient = new QueueClient(globalSettings.Events.ConnectionString, _queueName);
|
2020-07-07 12:01:34 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RaiseEventAsync(ReferenceEvent referenceEvent)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendMessageAsync(referenceEvent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task SendMessageAsync(ReferenceEvent referenceEvent)
|
|
|
|
|
|
{
|
2020-07-20 15:19:46 -04:00
|
|
|
|
if (_globalSettings.SelfHosted)
|
2020-07-07 12:01:34 -04:00
|
|
|
|
{
|
2020-07-20 15:19:46 -04:00
|
|
|
|
// Ignore for self-hosted
|
2020-07-07 12:01:34 -04:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = JsonConvert.SerializeObject(referenceEvent, _jsonSerializerSettings);
|
|
|
|
|
|
await _queueClient.SendMessageAsync(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// Ignore failure
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|