2023-03-21 18:55:21 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-11 10:10:53 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using MSAdminUsuarios.Context;
|
|
|
|
|
using MSAdminUsuarios.Controllers;
|
|
|
|
|
using MSAdminUsuarios.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using RabbitMQ.Client.Events;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace MSAdminUsuarios.Utils
|
|
|
|
|
{
|
|
|
|
|
public static class RabbitMQService
|
|
|
|
|
{
|
|
|
|
|
private static string[] _queues = Array.Empty<string>();
|
|
|
|
|
private static string _exchange = "MSAdminUsuarios";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IModel GetRabbitMQChannel(IServiceProvider serviceProvider, string projectName)
|
|
|
|
|
{
|
|
|
|
|
//var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
|
|
|
|
//var logger = loggerFactory.CreateLogger("Rabbit1");
|
|
|
|
|
|
|
|
|
|
var connection = serviceProvider.GetService<IConnection>();
|
|
|
|
|
|
|
|
|
|
if (connection == null)
|
|
|
|
|
throw new Exception("Connection is null.");
|
|
|
|
|
|
|
|
|
|
var channel = connection.CreateModel();
|
|
|
|
|
|
|
|
|
|
//Declare exchange if it doesnt already exist
|
|
|
|
|
var exchangeName = projectName;
|
|
|
|
|
_exchange = exchangeName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channel.ExchangeDeclare(
|
|
|
|
|
exchange: exchangeName,
|
|
|
|
|
type: ExchangeType.Topic,
|
|
|
|
|
durable: true,
|
|
|
|
|
autoDelete: false,
|
|
|
|
|
arguments: null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_queues = new string[]
|
|
|
|
|
{
|
|
|
|
|
$"{exchangeName}.PerfilesPorUsuario",
|
|
|
|
|
$"{exchangeName}.Usuarios"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var queue in _queues)
|
|
|
|
|
{
|
|
|
|
|
channel.QueueDeclare(
|
|
|
|
|
queue: queue,
|
|
|
|
|
durable: true,
|
|
|
|
|
exclusive: false,
|
|
|
|
|
autoDelete: false,
|
|
|
|
|
arguments: null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
channel.QueueBind(
|
|
|
|
|
queue: queue,
|
|
|
|
|
exchange: exchangeName,
|
|
|
|
|
routingKey: $"{queue}.*"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
channel.QueueBind(
|
|
|
|
|
queue: $"{exchangeName}.Usuarios",
|
|
|
|
|
exchange: exchangeName,
|
|
|
|
|
routingKey: $"{exchangeName}.Usuarios.*.*"
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ListenForIntegrationEvents(string projectName, WebApplicationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
RabbitMQConfig mqConfig = builder.Configuration.GetSection("RabbitMQConfig").Get<RabbitMQConfig>();
|
|
|
|
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
2023-11-07 08:35:29 -05:00
|
|
|
|
try
|
2023-03-21 18:55:21 -05:00
|
|
|
|
{
|
2023-11-07 08:35:29 -05:00
|
|
|
|
//var logger = loggerFactory.CreateLogger("Rabbit2");
|
|
|
|
|
//logger.LogInformation("Inicia Rabbitmq con");
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
ConnectionFactory factory = new()
|
|
|
|
|
{
|
|
|
|
|
HostName = mqConfig.HostName,
|
|
|
|
|
UserName = mqConfig.UserName,
|
|
|
|
|
Password = mqConfig.Password,
|
|
|
|
|
};
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
IConnection connection = factory.CreateConnection();
|
|
|
|
|
IModel channel = connection.CreateModel();
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
EventingBasicConsumer consumer = new(channel);
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
consumer.Received += RabbitMQService.ConsumeRabbitMQEvent;
|
|
|
|
|
|
|
|
|
|
List<string> queues = new()
|
2023-03-21 18:55:21 -05:00
|
|
|
|
{
|
|
|
|
|
$"{projectName}.PerfilesPorUsuario",
|
|
|
|
|
$"{projectName}.Usuarios",
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-07 08:35:29 -05:00
|
|
|
|
foreach (var queue in queues)
|
|
|
|
|
{
|
|
|
|
|
channel.BasicConsume(
|
|
|
|
|
queue: queue,
|
|
|
|
|
autoAck: false,
|
|
|
|
|
consumer: consumer
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-03-21 18:55:21 -05:00
|
|
|
|
{
|
2023-11-07 08:35:29 -05:00
|
|
|
|
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2023-03-21 18:55:21 -05:00
|
|
|
|
}
|
2023-11-07 08:35:29 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static async void ConsumeRabbitMQEvent(object? sender, BasicDeliverEventArgs ea)
|
|
|
|
|
{
|
|
|
|
|
var body = ea.Body.ToArray();
|
|
|
|
|
var message = Encoding.UTF8.GetString(body);
|
|
|
|
|
|
|
|
|
|
var route = ea.RoutingKey;
|
|
|
|
|
|
|
|
|
|
if (message == null || message.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Datos no recibidos");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (route == null || route.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("RouteKey no recibida");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var controller = route.Split(".");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var consumer = (EventingBasicConsumer)sender!;
|
|
|
|
|
var model = consumer.Model;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
|
|
|
|
var logger = loggerFactory.CreateLogger("RMQ.MsAdminUsuarios");
|
|
|
|
|
|
|
|
|
|
ModelContext context = new();
|
|
|
|
|
|
|
|
|
|
if (controller[1] == "PerfilesPorUsuario")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<PERFILPORUSUARIO[]>(message);
|
|
|
|
|
|
|
|
|
|
var PPUCtrl = new PerfilesPorUsuarioController(context);
|
|
|
|
|
var result = await PPUCtrl.GuardarPerfilesPorUsuario(data!);
|
2023-05-11 10:10:53 -05:00
|
|
|
|
logger.LogWarning(20,result.ToString());
|
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Temporalmente solo logeamos el error en lugar de rechazar
|
2023-05-11 10:10:53 -05:00
|
|
|
|
logger.LogCritical(1, ex.ToString());
|
|
|
|
|
logger.LogCritical(2, ex.Message);
|
|
|
|
|
logger.LogCritical(3, ex.InnerException?.ToString());
|
2023-03-21 18:55:21 -05:00
|
|
|
|
logger.LogCritical(ExMessage(ex));
|
|
|
|
|
//model.BasicReject(ea.DeliveryTag, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (controller[1] == "Usuarios")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-21 15:11:09 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
var UsuariosCtrl = new UsuariosController(context);
|
2023-09-21 15:11:09 -05:00
|
|
|
|
if (controller[2] == "post")
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.GuardarUsuarios(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
if (controller[2] == "patch")
|
|
|
|
|
{
|
|
|
|
|
if(controller.Length == 3)
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.EditarUsuarios(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
if (controller[3] == "Firma")
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.EditarFirma(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Temporalmente solo logeamos el error en lugar de rechazar
|
|
|
|
|
logger.LogCritical(ExMessage(ex));
|
|
|
|
|
//model.BasicReject(ea.DeliveryTag, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ExMessage(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.Append(ex.Message)
|
|
|
|
|
.Append(ex.InnerException);
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|