mirror of
https://github.com/bitwarden/server
synced 2025-12-24 12:13:17 +00:00
Revert filescoped (#2227)
* Revert "Add git blame entry (#2226)" This reverts commit239286737d. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a.
This commit is contained in:
@@ -3,90 +3,91 @@ using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Bit.Notifications;
|
||||
|
||||
public class AzureQueueHostedService : IHostedService, IDisposable
|
||||
namespace Bit.Notifications
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
private Task _executingTask;
|
||||
private CancellationTokenSource _cts;
|
||||
private QueueClient _queueClient;
|
||||
|
||||
public AzureQueueHostedService(
|
||||
ILogger<AzureQueueHostedService> logger,
|
||||
IHubContext<NotificationsHub> hubContext,
|
||||
GlobalSettings globalSettings)
|
||||
public class AzureQueueHostedService : IHostedService, IDisposable
|
||||
{
|
||||
_logger = logger;
|
||||
_hubContext = hubContext;
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
private readonly ILogger _logger;
|
||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
_executingTask = ExecuteAsync(_cts.Token);
|
||||
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
||||
}
|
||||
private Task _executingTask;
|
||||
private CancellationTokenSource _cts;
|
||||
private QueueClient _queueClient;
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_executingTask == null)
|
||||
public AzureQueueHostedService(
|
||||
ILogger<AzureQueueHostedService> logger,
|
||||
IHubContext<NotificationsHub> hubContext,
|
||||
GlobalSettings globalSettings)
|
||||
{
|
||||
return;
|
||||
_logger = logger;
|
||||
_hubContext = hubContext;
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
_logger.LogWarning("Stopping service.");
|
||||
_cts.Cancel();
|
||||
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{ }
|
||||
|
||||
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
_executingTask = ExecuteAsync(_cts.Token);
|
||||
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_executingTask == null)
|
||||
{
|
||||
var messages = await _queueClient.ReceiveMessagesAsync(32);
|
||||
if (messages.Value?.Any() ?? false)
|
||||
return;
|
||||
}
|
||||
_logger.LogWarning("Stopping service.");
|
||||
_cts.Cancel();
|
||||
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{ }
|
||||
|
||||
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var message in messages.Value)
|
||||
var messages = await _queueClient.ReceiveMessagesAsync(32);
|
||||
if (messages.Value?.Any() ?? false)
|
||||
{
|
||||
try
|
||||
foreach (var message in messages.Value)
|
||||
{
|
||||
await HubHelpers.SendNotificationToHubAsync(
|
||||
message.DecodeMessageText(), _hubContext, cancellationToken);
|
||||
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error processing dequeued message: " +
|
||||
$"{message.MessageId} x{message.DequeueCount}. {e.Message}", e);
|
||||
if (message.DequeueCount > 2)
|
||||
try
|
||||
{
|
||||
await HubHelpers.SendNotificationToHubAsync(
|
||||
message.DecodeMessageText(), _hubContext, cancellationToken);
|
||||
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error processing dequeued message: " +
|
||||
$"{message.MessageId} x{message.DequeueCount}. {e.Message}", e);
|
||||
if (message.DequeueCount > 2)
|
||||
{
|
||||
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception e)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
|
||||
_logger.LogError("Error processing messages.", e);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error processing messages.", e);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogWarning("Done processing.");
|
||||
_logger.LogWarning("Done processing.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user