| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using ClientManager.Shared.Contracts.Events; |
| | | 4 | | using ClientManager.Shared.Messaging; |
| | | 5 | | using Microsoft.AspNetCore.SignalR; |
| | | 6 | | using RabbitMQ.Client; |
| | | 7 | | using RabbitMQ.Client.Events; |
| | | 8 | | |
| | | 9 | | namespace ClientManager.API.Services; |
| | | 10 | | |
| | 0 | 11 | | public class EventForwarder(IMessageBrokerFactory messageBrokerFactory, IHubContext<NotificationHub> hub, ILogger<EventF |
| | | 12 | | { |
| | 0 | 13 | | readonly IMessageBrokerFactory _messageBrokerFactory = messageBrokerFactory; |
| | 0 | 14 | | readonly IHubContext<NotificationHub> _hub = hub; |
| | 0 | 15 | | readonly ILogger<EventForwarder> _logger = logger; |
| | 0 | 16 | | readonly IReadOnlyDictionary<string, Type> _eventTypesCache = DiscoverEventTypes().ToDictionary(t => t.Name, t => t) |
| | 0 | 17 | | readonly IReadOnlyDictionary<string, Type> _interfaceCache = DiscoverResponseInterfaces().ToDictionary(t => t.Name, |
| | | 18 | | |
| | | 19 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 20 | | { |
| | 0 | 21 | | var eventTypes = new[] { "ClientCreated" }; |
| | 0 | 22 | | foreach (var eventType in eventTypes) |
| | 0 | 23 | | await SubscribeAsync(eventType, stoppingToken); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | async Task SubscribeAsync(string queueName, CancellationToken cancellationToken) |
| | | 27 | | { |
| | 0 | 28 | | var channel = await _messageBrokerFactory.GetConsumeChannelAsync(queueName); |
| | 0 | 29 | | var consumer = new AsyncEventingBasicConsumer(channel); |
| | | 30 | | |
| | 0 | 31 | | consumer.ReceivedAsync += async (_, ea) => |
| | 0 | 32 | | { |
| | 0 | 33 | | var body = Encoding.UTF8.GetString(ea.Body.ToArray()); |
| | 0 | 34 | | var eventType = _eventTypesCache[queueName]; |
| | 0 | 35 | | |
| | 0 | 36 | | var envelope = JsonSerializer.Deserialize<MessageEnvelope<object>>(body); |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(envelope); |
| | 0 | 38 | | |
| | 0 | 39 | | var jsonElement = (JsonElement)envelope.Message; |
| | 0 | 40 | | var messageEvent = jsonElement.Deserialize(eventType); |
| | 0 | 41 | | |
| | 0 | 42 | | var eventInterface = _interfaceCache[queueName]; |
| | 0 | 43 | | var responseDto = eventInterface.GetMethod("ToResponse")!.Invoke(messageEvent, null); |
| | 0 | 44 | | _logger.LogInformation( |
| | 0 | 45 | | "Forwarding event\n\t {@Event}\n\t [correlationId: {correlationId}, causationId: {causationId}]", |
| | 0 | 46 | | responseDto, |
| | 0 | 47 | | envelope.CorrelationId, |
| | 0 | 48 | | envelope.CausationId |
| | 0 | 49 | | ); |
| | 0 | 50 | | await _hub.Clients.All.SendAsync(responseDto!.GetType().Name, responseDto, cancellationToken); |
| | 0 | 51 | | await channel.BasicAckAsync(ea.DeliveryTag, false, cancellationToken); |
| | 0 | 52 | | }; |
| | | 53 | | |
| | 0 | 54 | | await channel.BasicConsumeAsync(queueName, autoAck: false, consumer); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | static IEnumerable<Type> DiscoverEventTypes() => |
| | 0 | 58 | | AppDomain |
| | 0 | 59 | | .CurrentDomain.GetAssemblies() |
| | 0 | 60 | | .SelectMany(assembly => assembly.GetTypes()) |
| | 0 | 61 | | .Where(type => typeof(IEvent).IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface) |
| | 0 | 62 | | .ToList(); |
| | | 63 | | |
| | | 64 | | static IEnumerable<Type> DiscoverResponseInterfaces() => |
| | 0 | 65 | | AppDomain |
| | 0 | 66 | | .CurrentDomain.GetAssemblies() |
| | 0 | 67 | | .SelectMany(assembly => assembly.GetTypes()) |
| | 0 | 68 | | .Where(type => |
| | 0 | 69 | | type.GetInterfaces().Any(iface => iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IEve |
| | 0 | 70 | | && !type.IsAbstract |
| | 0 | 71 | | && !type.IsInterface |
| | 0 | 72 | | ) |
| | 0 | 73 | | .ToList(); |
| | | 74 | | } |