Bll MqService
This commit is contained in:
14
PARR.API.sln
14
PARR.API.sln
@@ -28,7 +28,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.EsppTemplateSyncWorker
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.EsppTemplateSync", "PARR.EsppTemplateSync\PARR.EsppTemplateSync.csproj", "{39FD9AAD-0792-46B2-A935-42486A993C32}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.EsppTemplateSync", "PARR.EsppTemplateSync\PARR.EsppTemplateSync.csproj", "{39FD9AAD-0792-46B2-A935-42486A993C32}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.MockData", "PARR.MockData\PARR.MockData.csproj", "{B02E5B16-59BD-4F02-9CC4-6873409DBD84}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.MockData", "PARR.MockData\PARR.MockData.csproj", "{B02E5B16-59BD-4F02-9CC4-6873409DBD84}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.GeneratorTemplates", "PARR.GeneratorTemplates\PARR.GeneratorTemplates.csproj", "{79EC7AD8-9A16-4806-881A-E5632C48C461}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.GeneratorTemplatesWorker", "PARR.GeneratorTemplatesWorker\PARR.GeneratorTemplatesWorker.csproj", "{BE11D005-D3FE-4F29-93A2-7379E729C78D}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -74,6 +78,14 @@ Global
|
|||||||
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Release|Any CPU.Build.0 = Release|Any CPU
|
{B02E5B16-59BD-4F02-9CC4-6873409DBD84}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{79EC7AD8-9A16-4806-881A-E5632C48C461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{79EC7AD8-9A16-4806-881A-E5632C48C461}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{79EC7AD8-9A16-4806-881A-E5632C48C461}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{79EC7AD8-9A16-4806-881A-E5632C48C461}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{BE11D005-D3FE-4F29-93A2-7379E729C78D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{BE11D005-D3FE-4F29-93A2-7379E729C78D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{BE11D005-D3FE-4F29-93A2-7379E729C78D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{BE11D005-D3FE-4F29-93A2-7379E729C78D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ using PARR.API.Contracts.V1.Requests;
|
|||||||
using PARR.API.Contracts.V1.Responses.Base;
|
using PARR.API.Contracts.V1.Responses.Base;
|
||||||
using PARR.API.Controllers.V1.Base;
|
using PARR.API.Controllers.V1.Base;
|
||||||
using PARR.API.Services.Interfaces;
|
using PARR.API.Services.Interfaces;
|
||||||
|
using PARR.API.Settings;
|
||||||
using PARR.BLL.Domain.Mq;
|
using PARR.BLL.Domain.Mq;
|
||||||
|
using PARR.BLL.Services.Interfaces;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace PARR.API.Controllers.V1
|
namespace PARR.API.Controllers.V1
|
||||||
@@ -13,18 +15,21 @@ namespace PARR.API.Controllers.V1
|
|||||||
public class GeneratorTemplateController : BaseApiController
|
public class GeneratorTemplateController : BaseApiController
|
||||||
{
|
{
|
||||||
private readonly IValidator<GeneratorTemplateRequest> validator;
|
private readonly IValidator<GeneratorTemplateRequest> validator;
|
||||||
private readonly IMqGeneratorTemplateService mqGeneratorTemplateService;
|
private readonly IMqService mqService;
|
||||||
private readonly IUriService uriService;
|
private readonly IUriService uriService;
|
||||||
|
private readonly MqSettings mqSettings;
|
||||||
|
|
||||||
public GeneratorTemplateController(
|
public GeneratorTemplateController(
|
||||||
IValidator<GeneratorTemplateRequest> validator,
|
IValidator<GeneratorTemplateRequest> validator,
|
||||||
IMqGeneratorTemplateService mqGeneratorTemplateService,
|
IMqService mqService,
|
||||||
IUriService uriService
|
IUriService uriService,
|
||||||
|
MqSettings mqSettings
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
this.mqGeneratorTemplateService = mqGeneratorTemplateService;
|
this.mqService = mqService;
|
||||||
this.uriService = uriService;
|
this.uriService = uriService;
|
||||||
|
this.mqSettings = mqSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -38,8 +43,6 @@ namespace PARR.API.Controllers.V1
|
|||||||
if (!resultValidate.IsValid)
|
if (!resultValidate.IsValid)
|
||||||
return BadRequest(new Response(resultValidate.Errors));
|
return BadRequest(new Response(resultValidate.Errors));
|
||||||
|
|
||||||
// Todo: проверить есть ли в бд такие ворк и апп
|
|
||||||
|
|
||||||
var obj = new GeneratorTemplateMq
|
var obj = new GeneratorTemplateMq
|
||||||
{
|
{
|
||||||
Action = request.Action,
|
Action = request.Action,
|
||||||
@@ -50,9 +53,9 @@ namespace PARR.API.Controllers.V1
|
|||||||
|
|
||||||
var msg = JsonSerializer.Serialize(obj);
|
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 = $"Ошибка при отправке данных." } }));
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при отправке данных." } }));
|
||||||
|
|
||||||
var createdUri = uriService.GetAllUri(ApiRoutes.Template.GetAll);
|
var createdUri = uriService.GetAllUri(ApiRoutes.Template.GetAll);
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ namespace PARR.API.Installers
|
|||||||
});
|
});
|
||||||
|
|
||||||
services.AddTransient<IClientService, ClientService>();
|
services.AddTransient<IClientService, ClientService>();
|
||||||
services.AddTransient<IMqGeneratorTemplateService, MqGeneratorTemplateService>();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace PARR.API.Services.Interfaces
|
|
||||||
{
|
|
||||||
public interface IMqGeneratorTemplateService
|
|
||||||
{
|
|
||||||
bool SendMsg(string msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
PARR.BLL/Domain/Mq/MqSendResult.cs
Normal file
18
PARR.BLL/Domain/Mq/MqSendResult.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace PARR.BLL.Domain.Mq
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Результат отправки очереди в MQ
|
||||||
|
/// </summary>
|
||||||
|
public class MqSendResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Результат отправки
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccess { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Неотправленные сообщения
|
||||||
|
/// </summary>
|
||||||
|
public string[]? NotSendMessages { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace PARR.BLL
|
|||||||
|
|
||||||
|
|
||||||
services.AddTransient<IFileService, FileService>();
|
services.AddTransient<IFileService, FileService>();
|
||||||
|
services.AddTransient<IMqService, MqService>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,34 @@
|
|||||||
using PARR.API.Services.Interfaces;
|
using Microsoft.Extensions.Logging;
|
||||||
using PARR.API.Settings;
|
using PARR.BLL.Contracts.Interfaces;
|
||||||
|
using PARR.BLL.Domain.Mq;
|
||||||
|
using PARR.BLL.Services.Interfaces;
|
||||||
using RabbitMQ.Client;
|
using RabbitMQ.Client;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace PARR.API.Services.Implementations
|
namespace PARR.BLL.Services.Implementations
|
||||||
{
|
{
|
||||||
public class MqGeneratorTemplateService : IMqGeneratorTemplateService
|
internal class MqService : IMqService
|
||||||
{
|
{
|
||||||
private readonly MqSettings mqSettings;
|
private readonly ILogger<MqService> logger;
|
||||||
private readonly ILogger<MqGeneratorTemplateService> logger;
|
|
||||||
|
|
||||||
public MqGeneratorTemplateService(MqSettings mqSettings, ILogger<MqGeneratorTemplateService> logger)
|
public MqService(ILogger<MqService> logger)
|
||||||
{
|
{
|
||||||
this.mqSettings = mqSettings;
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SendMsg(string msg)
|
public MqSendResult Send(IMqSettings mqSettings, string[] msgList)
|
||||||
{
|
{
|
||||||
var factory = new ConnectionFactory
|
var factory = new ConnectionFactory
|
||||||
{
|
{
|
||||||
HostName = mqSettings.GenerateTemplates.HostName,
|
HostName = mqSettings.HostName,
|
||||||
UserName = mqSettings.GenerateTemplates.User,
|
UserName = mqSettings.User,
|
||||||
Password = mqSettings.GenerateTemplates.Password
|
Password = mqSettings.Password
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// индекс текущей отправки в очередь из массива msgList
|
||||||
|
// нужен для формирования списка неотправленных сообщений
|
||||||
|
var sendIdx = 0;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var connection = factory.CreateConnection())
|
using (var connection = factory.CreateConnection())
|
||||||
@@ -41,33 +45,42 @@ namespace PARR.API.Services.Implementations
|
|||||||
//В целом эти параметры можно посмотреть в админке RabbitMQ
|
//В целом эти параметры можно посмотреть в админке RabbitMQ
|
||||||
|
|
||||||
channel.QueueDeclare(
|
channel.QueueDeclare(
|
||||||
queue: mqSettings.GenerateTemplates.QueueName,
|
queue: mqSettings.QueueName,
|
||||||
durable: true,
|
durable: true,
|
||||||
exclusive: false,
|
exclusive: false,
|
||||||
autoDelete: false,
|
autoDelete: false,
|
||||||
arguments: args
|
arguments: args
|
||||||
);
|
);
|
||||||
|
|
||||||
var body = Encoding.UTF8.GetBytes(msg);
|
|
||||||
|
|
||||||
var props = channel.CreateBasicProperties();
|
var props = channel.CreateBasicProperties();
|
||||||
//храним на диске
|
//храним на диске
|
||||||
props.DeliveryMode = 2;
|
props.DeliveryMode = 2;
|
||||||
//время жизни, мс
|
//время жизни, мс
|
||||||
//props.Expiration = "60000";
|
//props.Expiration = "60000";
|
||||||
|
|
||||||
channel.BasicPublish(exchange: "", routingKey: mqSettings.GenerateTemplates.QueueName, basicProperties: props, body: body);
|
foreach (var msg in msgList)
|
||||||
|
{
|
||||||
|
var body = Encoding.UTF8.GetBytes(msg);
|
||||||
|
channel.BasicPublish(exchange: "", routingKey: mqSettings.QueueName, basicProperties: props, body: body);
|
||||||
|
|
||||||
|
sendIdx++;
|
||||||
|
logger.LogDebug($"Отправлено сообщение в очередь: {mqSettings.QueueName}, {msg}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.LogDebug($"Отправлено сообщение в очередь: {mqSettings.GenerateTemplates.QueueName}, {msg}");
|
return new MqSendResult { IsSuccess = true };
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.LogError(ex, $"Ошибка при отправке сообщения в очередь {mqSettings.GenerateTemplates.QueueName}, {msg}");
|
var notSendMsg = new List<string>();
|
||||||
|
|
||||||
return false;
|
// формируем список неотправленных элементов
|
||||||
|
for (int i = sendIdx; i < msgList.Length; i++)
|
||||||
|
notSendMsg.Add(msgList[i]);
|
||||||
|
|
||||||
|
logger.LogError(ex, $"Ошибка при отправке сообщения в очередь {mqSettings.QueueName}, {string.Join(',', notSendMsg)}");
|
||||||
|
|
||||||
|
return new MqSendResult { IsSuccess = false, NotSendMessages = notSendMsg.ToArray() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
10
PARR.BLL/Services/Interfaces/IMqService.cs
Normal file
10
PARR.BLL/Services/Interfaces/IMqService.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using PARR.BLL.Contracts.Interfaces;
|
||||||
|
using PARR.BLL.Domain.Mq;
|
||||||
|
|
||||||
|
namespace PARR.BLL.Services.Interfaces
|
||||||
|
{
|
||||||
|
public interface IMqService
|
||||||
|
{
|
||||||
|
MqSendResult Send(IMqSettings mqSettings, string[] msgList);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs
Normal file
16
PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PARR.GeneratorTemplates
|
||||||
|
{
|
||||||
|
public static class GeneratorTemplateInstaller
|
||||||
|
{
|
||||||
|
//public static void InstallGeneratorTemplateServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user