♻️ Refactored the queue
This commit is contained in:
@@ -5,19 +5,21 @@ using DysonNetwork.Shared.Proto;
|
||||
using DysonNetwork.Shared.Registry;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DysonNetwork.Pusher.Services;
|
||||
|
||||
public class PusherServiceGrpc(
|
||||
EmailService emailService,
|
||||
QueueService queueService,
|
||||
WebSocketService websocket,
|
||||
PushService pushService,
|
||||
AccountClientHelper accountsHelper
|
||||
AccountClientHelper accountsHelper,
|
||||
EmailService emailService
|
||||
) : PusherService.PusherServiceBase
|
||||
{
|
||||
public override async Task<Empty> SendEmail(SendEmailRequest request, ServerCallContext context)
|
||||
{
|
||||
await emailService.SendEmailAsync(
|
||||
await queueService.EnqueueEmail(
|
||||
request.Email.ToName,
|
||||
request.Email.ToAddress,
|
||||
request.Email.Subject,
|
||||
@@ -47,13 +49,16 @@ public class PusherServiceGrpc(
|
||||
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
|
||||
ErrorMessage = request.Packet.ErrorMessage
|
||||
};
|
||||
|
||||
foreach (var userId in request.UserIds)
|
||||
{
|
||||
websocket.SendPacketToAccount(userId, packet);
|
||||
|
||||
}
|
||||
|
||||
return Task.FromResult(new Empty());
|
||||
}
|
||||
|
||||
public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDeviceRequest request,
|
||||
public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDeviceRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var packet = new Connection.WebSocketPacket
|
||||
@@ -75,29 +80,38 @@ public class PusherServiceGrpc(
|
||||
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
|
||||
ErrorMessage = request.Packet.ErrorMessage
|
||||
};
|
||||
|
||||
foreach (var deviceId in request.DeviceIds)
|
||||
{
|
||||
websocket.SendPacketToDevice(deviceId, packet);
|
||||
|
||||
}
|
||||
|
||||
return Task.FromResult(new Empty());
|
||||
}
|
||||
|
||||
public override async Task<Empty> SendPushNotificationToUser(SendPushNotificationToUserRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var account = await accountsHelper.GetAccount(Guid.Parse(request.UserId));
|
||||
await pushService.SendNotification(
|
||||
account,
|
||||
request.Notification.Topic,
|
||||
request.Notification.Title,
|
||||
request.Notification.Subtitle,
|
||||
request.Notification.Body,
|
||||
request.Notification.HasMeta
|
||||
var notification = new Notification.Notification
|
||||
{
|
||||
Topic = request.Notification.Topic,
|
||||
Title = request.Notification.Title,
|
||||
Subtitle = request.Notification.Subtitle,
|
||||
Content = request.Notification.Body,
|
||||
Meta = request.Notification.HasMeta
|
||||
? GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Notification.Meta) ?? []
|
||||
: [],
|
||||
request.Notification.ActionUri,
|
||||
request.Notification.IsSilent,
|
||||
: []
|
||||
};
|
||||
|
||||
if (request.Notification.ActionUri is not null)
|
||||
notification.Meta["action_uri"] = request.Notification.ActionUri;
|
||||
|
||||
await queueService.EnqueuePushNotification(
|
||||
notification,
|
||||
Guid.Parse(request.UserId),
|
||||
request.Notification.IsSavable
|
||||
);
|
||||
|
||||
return new Empty();
|
||||
}
|
||||
|
||||
@@ -114,10 +128,18 @@ public class PusherServiceGrpc(
|
||||
? GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Notification.Meta) ?? []
|
||||
: [],
|
||||
};
|
||||
|
||||
if (request.Notification.ActionUri is not null)
|
||||
notification.Meta["action_uri"] = request.Notification.ActionUri;
|
||||
var accounts = request.UserIds.Select(Guid.Parse).ToList();
|
||||
await pushService.SendNotificationBatch(notification, accounts, request.Notification.IsSavable);
|
||||
|
||||
var tasks = request.UserIds
|
||||
.Select(userId => queueService.EnqueuePushNotification(
|
||||
notification,
|
||||
Guid.Parse(userId),
|
||||
request.Notification.IsSavable
|
||||
));
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
return new Empty();
|
||||
}
|
||||
|
||||
|
135
DysonNetwork.Pusher/Services/QueueBackgroundService.cs
Normal file
135
DysonNetwork.Pusher/Services/QueueBackgroundService.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System.Text.Json;
|
||||
using DysonNetwork.Pusher.Email;
|
||||
using DysonNetwork.Pusher.Notification;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using DysonNetwork.Shared.Registry;
|
||||
using NATS.Client.Core;
|
||||
|
||||
namespace DysonNetwork.Pusher.Services;
|
||||
|
||||
public class QueueBackgroundService(
|
||||
INatsConnection nats,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<QueueBackgroundService> logger,
|
||||
IConfiguration configuration
|
||||
)
|
||||
: BackgroundService
|
||||
{
|
||||
private const string QueueName = "pusher.queue";
|
||||
private const string QueueGroup = "pusher.workers";
|
||||
private readonly int _consumerCount = configuration.GetValue<int?>("ConsumerCount") ?? Environment.ProcessorCount;
|
||||
private readonly List<Task> _consumerTasks = [];
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
logger.LogInformation("Starting {ConsumerCount} queue consumers", _consumerCount);
|
||||
|
||||
// Start multiple consumers
|
||||
for (var i = 0; i < _consumerCount; i++)
|
||||
_consumerTasks.Add(Task.Run(() => RunConsumerAsync(stoppingToken), stoppingToken));
|
||||
|
||||
// Wait for all consumers to complete
|
||||
await Task.WhenAll(_consumerTasks);
|
||||
}
|
||||
|
||||
private async Task RunConsumerAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
logger.LogInformation("Queue consumer started");
|
||||
|
||||
await foreach (var msg in nats.SubscribeAsync<QueueMessage>(
|
||||
QueueName,
|
||||
queueGroup: QueueGroup,
|
||||
cancellationToken: stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
await ProcessMessageAsync(msg, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// Normal shutdown
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error in queue consumer");
|
||||
// Add a small delay to prevent tight error loops
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async ValueTask ProcessMessageAsync(NatsMsg<QueueMessage> msg, CancellationToken cancellationToken)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var message = msg.Data;
|
||||
|
||||
logger.LogDebug("Processing message of type {MessageType}", message.Type);
|
||||
|
||||
try
|
||||
{
|
||||
switch (message.Type)
|
||||
{
|
||||
case QueueMessageType.Email:
|
||||
await ProcessEmailMessageAsync(message, scope, cancellationToken);
|
||||
break;
|
||||
|
||||
case QueueMessageType.PushNotification:
|
||||
await ProcessPushNotificationMessageAsync(message, scope, cancellationToken);
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.LogWarning("Unknown message type: {MessageType}", message.Type);
|
||||
break;
|
||||
}
|
||||
|
||||
await msg.ReplyAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error processing message of type {MessageType}", message.Type);
|
||||
// Don't rethrow to prevent the message from being retried indefinitely
|
||||
// In a production scenario, you might want to implement a dead-letter queue
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessEmailMessageAsync(QueueMessage message, IServiceScope scope,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var emailService = scope.ServiceProvider.GetRequiredService<EmailService>();
|
||||
var emailMessage = JsonSerializer.Deserialize<EmailMessage>(message.Data)
|
||||
?? throw new InvalidOperationException("Invalid email message format");
|
||||
|
||||
await emailService.SendEmailAsync(
|
||||
emailMessage.ToName,
|
||||
emailMessage.ToAddress,
|
||||
emailMessage.Subject,
|
||||
emailMessage.Body);
|
||||
}
|
||||
|
||||
private static async Task ProcessPushNotificationMessageAsync(QueueMessage message, IServiceScope scope,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var pushService = scope.ServiceProvider.GetRequiredService<PushService>();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueBackgroundService>>();
|
||||
|
||||
var notification = JsonSerializer.Deserialize<Notification.Notification>(message.Data);
|
||||
if (notification == null)
|
||||
{
|
||||
logger.LogError("Invalid push notification data format");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogDebug("Processing push notification for account {AccountId}", notification.AccountId);
|
||||
await pushService.DeliverPushNotification(notification, cancellationToken);
|
||||
logger.LogDebug("Successfully processed push notification for account {AccountId}", notification.AccountId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error processing push notification for account {AccountId}", notification.AccountId);
|
||||
// Don't rethrow to prevent the message from being retried indefinitely
|
||||
}
|
||||
}
|
||||
}
|
61
DysonNetwork.Pusher/Services/QueueService.cs
Normal file
61
DysonNetwork.Pusher/Services/QueueService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Text.Json;
|
||||
using NATS.Client.Core;
|
||||
|
||||
namespace DysonNetwork.Pusher.Services;
|
||||
|
||||
public class QueueService(INatsConnection nats)
|
||||
{
|
||||
private const string QueueName = "pusher_queue";
|
||||
|
||||
public async Task EnqueueEmail(string toName, string toAddress, string subject, string body)
|
||||
{
|
||||
var message = new QueueMessage
|
||||
{
|
||||
Type = QueueMessageType.Email,
|
||||
Data = JsonSerializer.Serialize(new EmailMessage
|
||||
{
|
||||
ToName = toName,
|
||||
ToAddress = toAddress,
|
||||
Subject = subject,
|
||||
Body = body
|
||||
})
|
||||
};
|
||||
await nats.PublishAsync(QueueName, message);
|
||||
}
|
||||
|
||||
public async Task EnqueuePushNotification(Notification.Notification notification, Guid userId, bool isSavable = false)
|
||||
{
|
||||
// Update the account ID in case it wasn't set
|
||||
notification.AccountId = userId;
|
||||
|
||||
var message = new QueueMessage
|
||||
{
|
||||
Type = QueueMessageType.PushNotification,
|
||||
TargetId = userId.ToString(),
|
||||
Data = JsonSerializer.Serialize(notification)
|
||||
};
|
||||
|
||||
await nats.PublishAsync(QueueName, message);
|
||||
}
|
||||
}
|
||||
|
||||
public class QueueMessage
|
||||
{
|
||||
public QueueMessageType Type { get; set; }
|
||||
public string? TargetId { get; set; }
|
||||
public string Data { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public enum QueueMessageType
|
||||
{
|
||||
Email,
|
||||
PushNotification
|
||||
}
|
||||
|
||||
public class EmailMessage
|
||||
{
|
||||
public string ToName { get; set; } = string.Empty;
|
||||
public string ToAddress { get; set; } = string.Empty;
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
public string Body { get; set; } = string.Empty;
|
||||
}
|
Reference in New Issue
Block a user