diff --git a/PARR.BLL/Services/Implementations/MqService.cs b/PARR.BLL/Services/Implementations/MqService.cs index 6d74876d..ff0882f3 100644 --- a/PARR.BLL/Services/Implementations/MqService.cs +++ b/PARR.BLL/Services/Implementations/MqService.cs @@ -3,6 +3,7 @@ using PARR.BLL.Contracts.Interfaces; using PARR.BLL.Domain.Mq; using PARR.BLL.Services.Interfaces; using RabbitMQ.Client; +using RabbitMQ.Client.Events; using System.Text; namespace PARR.BLL.Services.Implementations @@ -11,6 +12,8 @@ namespace PARR.BLL.Services.Implementations { private readonly ILogger logger; + public event ReceivedHandler? Received; + public MqService(ILogger logger) { this.logger = logger; @@ -83,5 +86,61 @@ namespace PARR.BLL.Services.Implementations return new MqSendResult { IsSuccess = false, NotSendMessages = notSendMsg.ToArray() }; } } + + + //TODO: rename ListenQueue + public void InitConsumer(IMqSettings mqSettings) + { + logger.LogInformation($"Устанавливаю соединение с RabbitMQ: {mqSettings.HostName}, {mqSettings.QueueName}"); + + var facory = new ConnectionFactory + { + HostName = mqSettings.HostName, + UserName = mqSettings.User, + Password = mqSettings.Password, + AutomaticRecoveryEnabled = true, + DispatchConsumersAsync = true + }; + + try + { + var connection = facory.CreateConnection(); + var channel = connection.CreateModel(); + + var args = new Dictionary { { "x-queue-mode", "lazy" } }; + + channel.QueueDeclare( + queue: mqSettings.QueueName, + durable: true, + exclusive: false, + autoDelete: false, + arguments: args + ); + + logger.LogInformation($"Соединение с RabbitMQ установлено: {mqSettings.HostName}, {mqSettings.QueueName}"); + + + var consumer = new AsyncEventingBasicConsumer(channel); + consumer.Received += async (ch, ea) => + { + var content = Encoding.UTF8.GetString(ea.Body.ToArray()); + logger.LogDebug($"Получено сообщение: {content}"); + + //https://metanit.com/sharp/tutorial/3.14.php + Received?.Invoke(content); + + channel.BasicAck(ea.DeliveryTag, false); + await Task.Yield(); + }; + + channel.BasicConsume(mqSettings.QueueName, false, consumer); + + } + catch (Exception ex) + { + logger.LogError(ex, $"Ошибка при получении сообщений из очереди: {mqSettings.HostName}, {mqSettings.QueueName}"); + } + } + } } diff --git a/PARR.BLL/Services/Interfaces/IMqService.cs b/PARR.BLL/Services/Interfaces/IMqService.cs index ab825e3e..6f7cc5ba 100644 --- a/PARR.BLL/Services/Interfaces/IMqService.cs +++ b/PARR.BLL/Services/Interfaces/IMqService.cs @@ -3,8 +3,13 @@ using PARR.BLL.Domain.Mq; namespace PARR.BLL.Services.Interfaces { + public delegate void ReceivedHandler(string msg); + public interface IMqService { + event ReceivedHandler? Received; + + void InitConsumer(IMqSettings mqSettings); MqSendResult Send(IMqSettings mqSettings, string[] msgList); } } diff --git a/PARR.GeneratorTemplates/GeneratorTemplate.cs b/PARR.GeneratorTemplates/GeneratorTemplate.cs new file mode 100644 index 00000000..147eef2f --- /dev/null +++ b/PARR.GeneratorTemplates/GeneratorTemplate.cs @@ -0,0 +1,39 @@ +using Microsoft.Extensions.Logging; +using PARR.BLL.Services.Interfaces; +using PARR.GeneratorTemplates.Settings; + +namespace PARR.GeneratorTemplates +{ + internal class GeneratorTemplate : IGeneratorTemplate + { + private readonly MqSettings mqSettings; + private readonly IMqService mqService; + private readonly ILogger logger; + + public GeneratorTemplate( + MqSettings mqSettings, + IMqService mqService, + ILogger logger + ) + { + this.mqSettings = mqSettings; + this.mqService = mqService; + this.logger = logger; + } + + public void Start() + { + mqService.InitConsumer(mqSettings); + + mqService.Received += (msg) => + { + logger.LogInformation($"Привет откуда надо!!! ${msg}"); + }; + } + + public void Stop() + { + + } + } +} diff --git a/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs b/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs index 4007a483..4b8d1acf 100644 --- a/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs +++ b/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs @@ -1,16 +1,41 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using PARR.BLL; +using PARR.DAL; +using PARR.GeneratorTemplates.Settings; namespace PARR.GeneratorTemplates { public static class GeneratorTemplateInstaller { - //public static void InstallGeneratorTemplateServices(this IServiceCollection services, IConfiguration configuration) - //{ + //1 + public static void InstallGeneratorTemplateServices(this IServiceCollection services, IConfiguration configuration) + { + services.InstallDalServices(configuration); + services.InstallBllServices(configuration); + + var mqSettings = new MqSettings(); + configuration.GetSection(nameof(MqSettings)).Bind(mqSettings); + services.AddSingleton(mqSettings); + + //add other services + services.AddTransient(); + } + + //2 + public static IConfigurationBuilder AddGeneratorTemplateConfigurations(this IConfigurationBuilder builder, IServiceCollection services) + { + builder.AddDalConfigurations(services); + + return builder; + } + + //3 + public static void AddGeneratorTemplateSettings(this IServiceCollection services, IConfiguration configuration) + { + services.AddDallSettings(configuration); + } + - //} } } diff --git a/PARR.GeneratorTemplates/IGeneratorTemplate.cs b/PARR.GeneratorTemplates/IGeneratorTemplate.cs new file mode 100644 index 00000000..89c2f408 --- /dev/null +++ b/PARR.GeneratorTemplates/IGeneratorTemplate.cs @@ -0,0 +1,8 @@ +namespace PARR.GeneratorTemplates +{ + public interface IGeneratorTemplate + { + void Start(); + void Stop(); + } +} diff --git a/PARR.GeneratorTemplates/PARR.GeneratorTemplates.csproj b/PARR.GeneratorTemplates/PARR.GeneratorTemplates.csproj index cfadb03d..21190861 100644 --- a/PARR.GeneratorTemplates/PARR.GeneratorTemplates.csproj +++ b/PARR.GeneratorTemplates/PARR.GeneratorTemplates.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/PARR.GeneratorTemplates/Settings/MqSettings.cs b/PARR.GeneratorTemplates/Settings/MqSettings.cs new file mode 100644 index 00000000..97d78349 --- /dev/null +++ b/PARR.GeneratorTemplates/Settings/MqSettings.cs @@ -0,0 +1,12 @@ +using PARR.BLL.Contracts.Interfaces; + +namespace PARR.GeneratorTemplates.Settings +{ + internal class MqSettings : IMqSettings + { + public string HostName { get; set; } = string.Empty; + public string QueueName { get; set; } = string.Empty; + public string User { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/PARR.GeneratorTemplatesWorker/PARR.GeneratorTemplatesWorker.csproj b/PARR.GeneratorTemplatesWorker/PARR.GeneratorTemplatesWorker.csproj index 3385ae46..bb916995 100644 --- a/PARR.GeneratorTemplatesWorker/PARR.GeneratorTemplatesWorker.csproj +++ b/PARR.GeneratorTemplatesWorker/PARR.GeneratorTemplatesWorker.csproj @@ -15,4 +15,8 @@ + + + + diff --git a/PARR.GeneratorTemplatesWorker/Program.cs b/PARR.GeneratorTemplatesWorker/Program.cs index a3ea5e29..b9739fda 100644 --- a/PARR.GeneratorTemplatesWorker/Program.cs +++ b/PARR.GeneratorTemplatesWorker/Program.cs @@ -1,9 +1,26 @@ +using PARR.GeneratorTemplates; using PARR.GeneratorTemplatesWorker; using Serilog; + IHost host = Host.CreateDefaultBuilder(args) - .ConfigureServices(services => + .ConfigureServices((hostContext, services) => { + services.InstallGeneratorTemplateServices(hostContext.Configuration); + //hostContext.Configuration.AddGeneratorTemplateConfigurations(services); + //services.AddGeneratorTemplateSettings(hostContext.Configuration); + + //services.AddHostedService(); + }) + //.ConfigureAppConfiguration((hostContext, configBuilder) => + //{ + //TODO: !!! !!! !!! не смог прикрутить AddGeneratorTemplateConfigurations, не могу прокинуть туда сервисы + // configBuilder.AddGeneratorTemplateConfigurations(hostContext.Services); + //}) + .ConfigureServices((hostContext, services) => + { + //services.AddGeneratorTemplateSettings(hostContext.Configuration); + services.AddHostedService(); }) .UseSerilog((hostContext, services, config) => diff --git a/PARR.GeneratorTemplatesWorker/Worker.cs b/PARR.GeneratorTemplatesWorker/Worker.cs index 9baaa069..7f1ec28a 100644 --- a/PARR.GeneratorTemplatesWorker/Worker.cs +++ b/PARR.GeneratorTemplatesWorker/Worker.cs @@ -1,21 +1,34 @@ +using PARR.GeneratorTemplates; + namespace PARR.GeneratorTemplatesWorker { public class Worker : BackgroundService { private readonly ILogger _logger; + private readonly IGeneratorTemplate generatorTemplate; - public Worker(ILogger logger) + public Worker(ILogger logger, IGeneratorTemplate generatorTemplate) { _logger = logger; + this.generatorTemplate = generatorTemplate; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - while (!stoppingToken.IsCancellationRequested) - { - _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); - await Task.Delay(1000, stoppingToken); - } + // todo: обернуть в таск? + generatorTemplate.Start(); + //while (!stoppingToken.IsCancellationRequested) + //{ + // _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + // await Task.Delay(1000, stoppingToken); + //} + } + + public override Task StopAsync(CancellationToken cancellationToken) + { + generatorTemplate.Stop(); + + return base.StopAsync(cancellationToken); } } } \ No newline at end of file diff --git a/PARR.GeneratorTemplatesWorker/appsettings.Development.json b/PARR.GeneratorTemplatesWorker/appsettings.Development.json index b2dcdb67..992d2719 100644 --- a/PARR.GeneratorTemplatesWorker/appsettings.Development.json +++ b/PARR.GeneratorTemplatesWorker/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } + }, + "MqSettings": { + "HostName": "10.99.253.216" } } diff --git a/PARR.GeneratorTemplatesWorker/appsettings.json b/PARR.GeneratorTemplatesWorker/appsettings.json index d250dadc..a28563b8 100644 --- a/PARR.GeneratorTemplatesWorker/appsettings.json +++ b/PARR.GeneratorTemplatesWorker/appsettings.json @@ -22,5 +22,11 @@ } } ] + }, + "MqSettings": { + "HostName": "parr-rabbitmq", + "QueueName": "parr-generate-templates", + "User": "generate_templates_worker", + "Password": "Dhgdf%d&609sssa" } }