Bll MqService
This commit is contained in:
@@ -5,7 +5,9 @@ using PARR.API.Contracts.V1.Requests;
|
||||
using PARR.API.Contracts.V1.Responses.Base;
|
||||
using PARR.API.Controllers.V1.Base;
|
||||
using PARR.API.Services.Interfaces;
|
||||
using PARR.API.Settings;
|
||||
using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.API.Controllers.V1
|
||||
@@ -13,18 +15,21 @@ namespace PARR.API.Controllers.V1
|
||||
public class GeneratorTemplateController : BaseApiController
|
||||
{
|
||||
private readonly IValidator<GeneratorTemplateRequest> validator;
|
||||
private readonly IMqGeneratorTemplateService mqGeneratorTemplateService;
|
||||
private readonly IMqService mqService;
|
||||
private readonly IUriService uriService;
|
||||
private readonly MqSettings mqSettings;
|
||||
|
||||
public GeneratorTemplateController(
|
||||
IValidator<GeneratorTemplateRequest> validator,
|
||||
IMqGeneratorTemplateService mqGeneratorTemplateService,
|
||||
IUriService uriService
|
||||
IMqService mqService,
|
||||
IUriService uriService,
|
||||
MqSettings mqSettings
|
||||
)
|
||||
{
|
||||
this.validator = validator;
|
||||
this.mqGeneratorTemplateService = mqGeneratorTemplateService;
|
||||
this.mqService = mqService;
|
||||
this.uriService = uriService;
|
||||
this.mqSettings = mqSettings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -38,8 +43,6 @@ namespace PARR.API.Controllers.V1
|
||||
if (!resultValidate.IsValid)
|
||||
return BadRequest(new Response(resultValidate.Errors));
|
||||
|
||||
// Todo: проверить есть ли в бд такие ворк и апп
|
||||
|
||||
var obj = new GeneratorTemplateMq
|
||||
{
|
||||
Action = request.Action,
|
||||
@@ -50,9 +53,9 @@ namespace PARR.API.Controllers.V1
|
||||
|
||||
var msg = JsonSerializer.Serialize(obj);
|
||||
|
||||
var result = mqGeneratorTemplateService.SendMsg(msg);
|
||||
var result = mqService.Send(mqSettings.GenerateTemplates, new[] { msg });
|
||||
|
||||
if (!result)
|
||||
if (!result.IsSuccess)
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при отправке данных." } }));
|
||||
|
||||
var createdUri = uriService.GetAllUri(ApiRoutes.Template.GetAll);
|
||||
|
||||
@@ -20,7 +20,6 @@ namespace PARR.API.Installers
|
||||
});
|
||||
|
||||
services.AddTransient<IClientService, ClientService>();
|
||||
services.AddTransient<IMqGeneratorTemplateService, MqGeneratorTemplateService>();
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
using PARR.API.Services.Interfaces;
|
||||
using PARR.API.Settings;
|
||||
using RabbitMQ.Client;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.API.Services.Implementations
|
||||
{
|
||||
public class MqGeneratorTemplateService : IMqGeneratorTemplateService
|
||||
{
|
||||
private readonly MqSettings mqSettings;
|
||||
private readonly ILogger<MqGeneratorTemplateService> logger;
|
||||
|
||||
public MqGeneratorTemplateService(MqSettings mqSettings, ILogger<MqGeneratorTemplateService> logger)
|
||||
{
|
||||
this.mqSettings = mqSettings;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public bool SendMsg(string msg)
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
HostName = mqSettings.GenerateTemplates.HostName,
|
||||
UserName = mqSettings.GenerateTemplates.User,
|
||||
Password = mqSettings.GenerateTemplates.Password
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var connection = factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
//https://www.rabbitmq.com/lazy-queues.html
|
||||
//Рекомендовано при работе порциями использовать ленивые очереди. сообщения не используют память
|
||||
//Формируем соответствующий аргумент
|
||||
var args = new Dictionary<string, object> { { "x-queue-mode", "lazy" } };
|
||||
|
||||
//Объявляем очередь с которой будем работать.
|
||||
//Если такой очереди ещё нет, то создатся.
|
||||
//Если нет, нужно параметры типа durable должны совпадать иначе будет ошибка.
|
||||
//В целом эти параметры можно посмотреть в админке RabbitMQ
|
||||
|
||||
channel.QueueDeclare(
|
||||
queue: mqSettings.GenerateTemplates.QueueName,
|
||||
durable: true,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: args
|
||||
);
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(msg);
|
||||
|
||||
var props = channel.CreateBasicProperties();
|
||||
//храним на диске
|
||||
props.DeliveryMode = 2;
|
||||
//время жизни, мс
|
||||
//props.Expiration = "60000";
|
||||
|
||||
channel.BasicPublish(exchange: "", routingKey: mqSettings.GenerateTemplates.QueueName, basicProperties: props, body: body);
|
||||
}
|
||||
|
||||
logger.LogDebug($"Отправлено сообщение в очередь: {mqSettings.GenerateTemplates.QueueName}, {msg}");
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при отправке сообщения в очередь {mqSettings.GenerateTemplates.QueueName}, {msg}");
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace PARR.API.Services.Interfaces
|
||||
{
|
||||
public interface IMqGeneratorTemplateService
|
||||
{
|
||||
bool SendMsg(string msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user