diff --git a/PARR.API.sln b/PARR.API.sln index d731ecfe..ce7ca48a 100644 --- a/PARR.API.sln +++ b/PARR.API.sln @@ -28,7 +28,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.EsppTemplateSyncWorker EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.EsppTemplateSync", "PARR.EsppTemplateSync\PARR.EsppTemplateSync.csproj", "{39FD9AAD-0792-46B2-A935-42486A993C32}" 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 Global 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PARR.API/Controllers/V1/GeneratorTemplateController.cs b/PARR.API/Controllers/V1/GeneratorTemplateController.cs index 8deb8b11..a3438f85 100644 --- a/PARR.API/Controllers/V1/GeneratorTemplateController.cs +++ b/PARR.API/Controllers/V1/GeneratorTemplateController.cs @@ -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 validator; - private readonly IMqGeneratorTemplateService mqGeneratorTemplateService; + private readonly IMqService mqService; private readonly IUriService uriService; + private readonly MqSettings mqSettings; public GeneratorTemplateController( IValidator 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; } /// @@ -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 { new ErrorModel { Message = $"Ошибка при отправке данных." } })); var createdUri = uriService.GetAllUri(ApiRoutes.Template.GetAll); diff --git a/PARR.API/Installers/ApiServicesInstaller.cs b/PARR.API/Installers/ApiServicesInstaller.cs index a097cd3a..4ff3bc14 100644 --- a/PARR.API/Installers/ApiServicesInstaller.cs +++ b/PARR.API/Installers/ApiServicesInstaller.cs @@ -20,7 +20,6 @@ namespace PARR.API.Installers }); services.AddTransient(); - services.AddTransient(); diff --git a/PARR.API/Services/Implementations/MqGeneratorTemplateService.cs b/PARR.API/Services/Implementations/MqGeneratorTemplateService.cs deleted file mode 100644 index 245efa0d..00000000 --- a/PARR.API/Services/Implementations/MqGeneratorTemplateService.cs +++ /dev/null @@ -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 logger; - - public MqGeneratorTemplateService(MqSettings mqSettings, ILogger 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 { { "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; - } - } - } -} diff --git a/PARR.API/Services/Interfaces/IMqGeneratorTemplateService.cs b/PARR.API/Services/Interfaces/IMqGeneratorTemplateService.cs deleted file mode 100644 index 51d3361c..00000000 --- a/PARR.API/Services/Interfaces/IMqGeneratorTemplateService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace PARR.API.Services.Interfaces -{ - public interface IMqGeneratorTemplateService - { - bool SendMsg(string msg); - } -} diff --git a/PARR.API/Settings/MqSettings.cs b/PARR.API/Settings/MqSettings.cs index db4d8e7e..4efd1fff 100644 --- a/PARR.API/Settings/MqSettings.cs +++ b/PARR.API/Settings/MqSettings.cs @@ -1,4 +1,6 @@ -namespace PARR.API.Settings +using PARR.BLL.Contracts.Interfaces; + +namespace PARR.API.Settings { public class MqSettings { @@ -12,12 +14,4 @@ public string User { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; } - - public interface IMqSettings - { - string HostName { get; set; } - string QueueName { get; set; } - string User { get; set; } - string Password { get; set; } - } } diff --git a/PARR.API/appsettings.json b/PARR.API/appsettings.json index 513c496c..f15eb375 100644 --- a/PARR.API/appsettings.json +++ b/PARR.API/appsettings.json @@ -39,7 +39,7 @@ }, "MqSettings": { "GenerateTemplates": { - "HostName": "parr-rabbitmq", + "HostName": "10.99.253.216", "QueueName": "parr-generate-templates", "User": "generate_templates_api", "Password": "DHhjgdsf*&%95" diff --git a/PARR.BLL/Contracts/Interfaces/IMqSettings.cs b/PARR.BLL/Contracts/Interfaces/IMqSettings.cs new file mode 100644 index 00000000..ef0536c9 --- /dev/null +++ b/PARR.BLL/Contracts/Interfaces/IMqSettings.cs @@ -0,0 +1,13 @@ +namespace PARR.BLL.Contracts.Interfaces +{ + /// + /// Поля настроек MQ, используется для appsettings.json + /// + public interface IMqSettings + { + string HostName { get; set; } + string QueueName { get; set; } + string User { get; set; } + string Password { get; set; } + } +} diff --git a/PARR.BLL/Domain/Mq/MqSendResult.cs b/PARR.BLL/Domain/Mq/MqSendResult.cs new file mode 100644 index 00000000..b719b484 --- /dev/null +++ b/PARR.BLL/Domain/Mq/MqSendResult.cs @@ -0,0 +1,18 @@ +namespace PARR.BLL.Domain.Mq +{ + /// + /// Результат отправки очереди в MQ + /// + public class MqSendResult + { + /// + /// Результат отправки + /// + public bool IsSuccess { get; set; } + + /// + /// Неотправленные сообщения + /// + public string[]? NotSendMessages { get; set; } + } +} diff --git a/PARR.BLL/PARR.BLL.csproj b/PARR.BLL/PARR.BLL.csproj index 1c2edf9c..fa036eaa 100644 --- a/PARR.BLL/PARR.BLL.csproj +++ b/PARR.BLL/PARR.BLL.csproj @@ -11,6 +11,7 @@ + diff --git a/PARR.BLL/ParrBllInstaller.cs b/PARR.BLL/ParrBllInstaller.cs index 782a45e0..0c34489d 100644 --- a/PARR.BLL/ParrBllInstaller.cs +++ b/PARR.BLL/ParrBllInstaller.cs @@ -16,6 +16,7 @@ namespace PARR.BLL services.AddTransient(); + services.AddTransient(); } } } diff --git a/PARR.BLL/Services/Implementations/MqService.cs b/PARR.BLL/Services/Implementations/MqService.cs new file mode 100644 index 00000000..12720100 --- /dev/null +++ b/PARR.BLL/Services/Implementations/MqService.cs @@ -0,0 +1,159 @@ +using Microsoft.Extensions.Logging; +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 +{ + internal class MqService : IMqService + { + private readonly ILogger logger; + + private IConnection? connection; + private IModel? channel; + + public event ReceivedHandler? Received; + + public MqService(ILogger logger) + { + this.logger = logger; + } + + public MqSendResult Send(IMqSettings mqSettings, string[] msgList) + { + var factory = new ConnectionFactory + { + HostName = mqSettings.HostName, + UserName = mqSettings.User, + Password = mqSettings.Password + }; + + // индекс текущей отправки в очередь из массива msgList + // нужен для формирования списка неотправленных сообщений + var sendIdx = 0; + + try + { + using (var connection = factory.CreateConnection()) + using (var channel = connection.CreateModel()) + { + //https://www.rabbitmq.com/lazy-queues.html + //Рекомендовано при работе порциями использовать ленивые очереди. сообщения не используют память + //Формируем соответствующий аргумент + var args = new Dictionary { { "x-queue-mode", "lazy" } }; + + //Объявляем очередь с которой будем работать. + //Если такой очереди ещё нет, то создатся. + //Если нет, нужно параметры типа durable должны совпадать иначе будет ошибка. + //В целом эти параметры можно посмотреть в админке RabbitMQ + + channel.QueueDeclare( + queue: mqSettings.QueueName, + durable: true, + exclusive: false, + autoDelete: false, + arguments: args + ); + + var props = channel.CreateBasicProperties(); + //храним на диске + props.DeliveryMode = 2; + //время жизни, мс + //props.Expiration = "60000"; + + 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.HostName} {mqSettings.QueueName}, {msg}"); + } + } + + return new MqSendResult { IsSuccess = true }; + } + catch (Exception ex) + { + var notSendMsg = new List(); + + // формируем список неотправленных элементов + for (int i = sendIdx; i < msgList.Length; i++) + notSendMsg.Add(msgList[i]); + + logger.LogError(ex, $"Ошибка при отправке сообщения в очередь {mqSettings.HostName} {mqSettings.QueueName}, {string.Join(',', notSendMsg)}"); + + return new MqSendResult { IsSuccess = false, NotSendMessages = notSendMsg.ToArray() }; + } + } + + + 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 + { + //using var connection = facory.CreateConnection(); + //using var channel = connection.CreateModel(); + + connection = facory.CreateConnection(); + 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}"); + } + } + + public void Dispose() + { + channel?.Close(); + connection?.Close(); + + channel?.Dispose(); + connection?.Dispose(); + } + } +} diff --git a/PARR.BLL/Services/Interfaces/IMqService.cs b/PARR.BLL/Services/Interfaces/IMqService.cs new file mode 100644 index 00000000..3db517c6 --- /dev/null +++ b/PARR.BLL/Services/Interfaces/IMqService.cs @@ -0,0 +1,15 @@ +using PARR.BLL.Contracts.Interfaces; +using PARR.BLL.Domain.Mq; + +namespace PARR.BLL.Services.Interfaces +{ + public delegate void ReceivedHandler(string msg); + + public interface IMqService : IDisposable + { + 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..00b33020 --- /dev/null +++ b/PARR.GeneratorTemplates/GeneratorTemplate.cs @@ -0,0 +1,53 @@ +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.Received += (msg) => + { + logger.LogInformation($"Привет откуда надо!!! ${msg}"); + //todo: идем дальеш! + + // создать отдельный метод + // преобразовать json в модель + // попробовать поиск по регулярке + + // проверить как ту дела с асинком + // логгер работает в разных потоках все таки или нет? + + // поиск в существующих шаблонах, есть ли такие и есть ли у них эти работы + // поиск в хостах эк которых еще нет в шаблонах и у них такие работы + // генерим список и вставляем в шаблоны + }; + + mqService.InitConsumer(mqSettings); + + + } + + public void Stop() + { + mqService.Dispose(); + } + } +} diff --git a/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs b/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs new file mode 100644 index 00000000..4b8d1acf --- /dev/null +++ b/PARR.GeneratorTemplates/GeneratorTemplateInstaller.cs @@ -0,0 +1,41 @@ +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 + { + //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 new file mode 100644 index 00000000..21190861 --- /dev/null +++ b/PARR.GeneratorTemplates/PARR.GeneratorTemplates.csproj @@ -0,0 +1,14 @@ + + + + net7.0 + enable + 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 new file mode 100644 index 00000000..bb916995 --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/PARR.GeneratorTemplatesWorker.csproj @@ -0,0 +1,22 @@ + + + + net7.0 + enable + enable + dotnet-PARR.GeneratorTemplatesWorker-deb14157-f207-44e0-8d4b-963c6066a75e + + + + + + + + + + + + + + + diff --git a/PARR.GeneratorTemplatesWorker/Program.cs b/PARR.GeneratorTemplatesWorker/Program.cs new file mode 100644 index 00000000..b9739fda --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/Program.cs @@ -0,0 +1,34 @@ +using PARR.GeneratorTemplates; +using PARR.GeneratorTemplatesWorker; +using Serilog; + + +IHost host = Host.CreateDefaultBuilder(args) + .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) => + { + config + .WriteTo.Console() + .ReadFrom.Configuration(hostContext.Configuration); + }) + .Build(); + +host.Run(); diff --git a/PARR.GeneratorTemplatesWorker/Properties/launchSettings.json b/PARR.GeneratorTemplatesWorker/Properties/launchSettings.json new file mode 100644 index 00000000..06416c77 --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/Properties/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "PARR.GeneratorTemplatesWorker": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/PARR.GeneratorTemplatesWorker/Worker.cs b/PARR.GeneratorTemplatesWorker/Worker.cs new file mode 100644 index 00000000..7f1ec28a --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/Worker.cs @@ -0,0 +1,34 @@ +using PARR.GeneratorTemplates; + +namespace PARR.GeneratorTemplatesWorker +{ + public class Worker : BackgroundService + { + private readonly ILogger _logger; + private readonly IGeneratorTemplate generatorTemplate; + + public Worker(ILogger logger, IGeneratorTemplate generatorTemplate) + { + _logger = logger; + this.generatorTemplate = generatorTemplate; + } + + protected override async Task ExecuteAsync(CancellationToken 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 new file mode 100644 index 00000000..992d2719 --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/appsettings.Development.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "MqSettings": { + "HostName": "10.99.253.216" + } +} diff --git a/PARR.GeneratorTemplatesWorker/appsettings.json b/PARR.GeneratorTemplatesWorker/appsettings.json new file mode 100644 index 00000000..c4246afa --- /dev/null +++ b/PARR.GeneratorTemplatesWorker/appsettings.json @@ -0,0 +1,32 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Debug" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log/log-.txt", + "rollingInterval": "Day" + } + } + ] + }, + "MqSettings": { + "HostName": "10.99.253.216", + "QueueName": "parr-generate-templates", + "User": "generate_templates_worker", + "Password": "Dhgdf%d&609sssa" + } +} diff --git a/PARR.Worker/PARR.Worker/Program.cs b/PARR.Worker/PARR.Worker/Program.cs index 7432b953..bdaf9c9b 100644 --- a/PARR.Worker/PARR.Worker/Program.cs +++ b/PARR.Worker/PARR.Worker/Program.cs @@ -1,7 +1,4 @@ -using Microsoft.EntityFrameworkCore; using PARR.AIHIT; -using PARR.DAL; -using PARR.Mail; using PARR.Worker; using PARR.Worker.Settings; using Serilog;