1
0
mirror of https://github.com/bitwarden/server synced 2025-12-30 23:23:37 +00:00

Turn on file scoped namespaces (#2225)

This commit is contained in:
Justin Baur
2022-08-29 14:53:16 -04:00
committed by GitHub
parent 7c4521e0b4
commit 34fb4cca2a
1206 changed files with 73816 additions and 75022 deletions

View File

@@ -8,100 +8,99 @@ using Microsoft.Azure.ServiceBus.Management;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Bit.Core.HostedServices
namespace Bit.Core.HostedServices;
public class ApplicationCacheHostedService : IHostedService, IDisposable
{
public class ApplicationCacheHostedService : IHostedService, IDisposable
private readonly InMemoryServiceBusApplicationCacheService _applicationCacheService;
private readonly IOrganizationRepository _organizationRepository;
protected readonly ILogger<ApplicationCacheHostedService> _logger;
private readonly SubscriptionClient _subscriptionClient;
private readonly ManagementClient _managementClient;
private readonly string _subName;
private readonly string _topicName;
public ApplicationCacheHostedService(
IApplicationCacheService applicationCacheService,
IOrganizationRepository organizationRepository,
ILogger<ApplicationCacheHostedService> logger,
GlobalSettings globalSettings)
{
private readonly InMemoryServiceBusApplicationCacheService _applicationCacheService;
private readonly IOrganizationRepository _organizationRepository;
protected readonly ILogger<ApplicationCacheHostedService> _logger;
private readonly SubscriptionClient _subscriptionClient;
private readonly ManagementClient _managementClient;
private readonly string _subName;
private readonly string _topicName;
_topicName = globalSettings.ServiceBus.ApplicationCacheTopicName;
_subName = CoreHelpers.GetApplicationCacheServiceBusSubcriptionName(globalSettings);
_applicationCacheService = applicationCacheService as InMemoryServiceBusApplicationCacheService;
_organizationRepository = organizationRepository;
_logger = logger;
_managementClient = new ManagementClient(globalSettings.ServiceBus.ConnectionString);
_subscriptionClient = new SubscriptionClient(globalSettings.ServiceBus.ConnectionString,
_topicName, _subName);
}
public ApplicationCacheHostedService(
IApplicationCacheService applicationCacheService,
IOrganizationRepository organizationRepository,
ILogger<ApplicationCacheHostedService> logger,
GlobalSettings globalSettings)
public virtual async Task StartAsync(CancellationToken cancellationToken)
{
try
{
_topicName = globalSettings.ServiceBus.ApplicationCacheTopicName;
_subName = CoreHelpers.GetApplicationCacheServiceBusSubcriptionName(globalSettings);
_applicationCacheService = applicationCacheService as InMemoryServiceBusApplicationCacheService;
_organizationRepository = organizationRepository;
_logger = logger;
_managementClient = new ManagementClient(globalSettings.ServiceBus.ConnectionString);
_subscriptionClient = new SubscriptionClient(globalSettings.ServiceBus.ConnectionString,
_topicName, _subName);
await _managementClient.CreateSubscriptionAsync(new SubscriptionDescription(_topicName, _subName)
{
DefaultMessageTimeToLive = TimeSpan.FromDays(14),
LockDuration = TimeSpan.FromSeconds(30),
EnableDeadLetteringOnFilterEvaluationExceptions = true,
EnableDeadLetteringOnMessageExpiration = true,
}, new RuleDescription("default", new SqlFilter($"sys.Label != '{_subName}'")));
}
public virtual async Task StartAsync(CancellationToken cancellationToken)
{
try
catch (MessagingEntityAlreadyExistsException) { }
_subscriptionClient.RegisterMessageHandler(ProcessMessageAsync,
new MessageHandlerOptions(ExceptionReceivedHandlerAsync)
{
await _managementClient.CreateSubscriptionAsync(new SubscriptionDescription(_topicName, _subName)
{
DefaultMessageTimeToLive = TimeSpan.FromDays(14),
LockDuration = TimeSpan.FromSeconds(30),
EnableDeadLetteringOnFilterEvaluationExceptions = true,
EnableDeadLetteringOnMessageExpiration = true,
}, new RuleDescription("default", new SqlFilter($"sys.Label != '{_subName}'")));
}
catch (MessagingEntityAlreadyExistsException) { }
_subscriptionClient.RegisterMessageHandler(ProcessMessageAsync,
new MessageHandlerOptions(ExceptionReceivedHandlerAsync)
{
MaxConcurrentCalls = 2,
AutoComplete = false,
});
MaxConcurrentCalls = 2,
AutoComplete = false,
});
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
await _subscriptionClient.CloseAsync();
try
{
await _managementClient.DeleteSubscriptionAsync(_topicName, _subName, cancellationToken);
}
catch { }
}
public virtual async Task StopAsync(CancellationToken cancellationToken)
public virtual void Dispose()
{ }
private async Task ProcessMessageAsync(Message message, CancellationToken cancellationToken)
{
if (message.Label != _subName && _applicationCacheService != null)
{
await _subscriptionClient.CloseAsync();
try
switch ((ApplicationCacheMessageType)message.UserProperties["type"])
{
await _managementClient.DeleteSubscriptionAsync(_topicName, _subName, cancellationToken);
}
catch { }
}
public virtual void Dispose()
{ }
private async Task ProcessMessageAsync(Message message, CancellationToken cancellationToken)
{
if (message.Label != _subName && _applicationCacheService != null)
{
switch ((ApplicationCacheMessageType)message.UserProperties["type"])
{
case ApplicationCacheMessageType.UpsertOrganizationAbility:
var upsertedOrgId = (Guid)message.UserProperties["id"];
var upsertedOrg = await _organizationRepository.GetByIdAsync(upsertedOrgId);
if (upsertedOrg != null)
{
await _applicationCacheService.BaseUpsertOrganizationAbilityAsync(upsertedOrg);
}
break;
case ApplicationCacheMessageType.DeleteOrganizationAbility:
await _applicationCacheService.BaseDeleteOrganizationAbilityAsync(
(Guid)message.UserProperties["id"]);
break;
default:
break;
}
}
if (!cancellationToken.IsCancellationRequested)
{
await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
case ApplicationCacheMessageType.UpsertOrganizationAbility:
var upsertedOrgId = (Guid)message.UserProperties["id"];
var upsertedOrg = await _organizationRepository.GetByIdAsync(upsertedOrgId);
if (upsertedOrg != null)
{
await _applicationCacheService.BaseUpsertOrganizationAbilityAsync(upsertedOrg);
}
break;
case ApplicationCacheMessageType.DeleteOrganizationAbility:
await _applicationCacheService.BaseDeleteOrganizationAbilityAsync(
(Guid)message.UserProperties["id"]);
break;
default:
break;
}
}
private Task ExceptionReceivedHandlerAsync(ExceptionReceivedEventArgs args)
if (!cancellationToken.IsCancellationRequested)
{
_logger.LogError(args.Exception, "Message handler encountered an exception.");
return Task.FromResult(0);
await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}
}
private Task ExceptionReceivedHandlerAsync(ExceptionReceivedEventArgs args)
{
_logger.LogError(args.Exception, "Message handler encountered an exception.");
return Task.FromResult(0);
}
}