Fix RabbitMQ запись в БД. Репо перенастроен на harbor
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.DAL.Context;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.EsppTemplateSync.Services;
|
||||
using PARR.EsppTemplateSync.Settings;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System.Runtime.Serialization.DataContracts;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.EsppTemplateSync
|
||||
@@ -15,92 +12,93 @@ namespace PARR.EsppTemplateSync
|
||||
private readonly ILogger<TemplateMQSyncer> logger;
|
||||
private readonly IManager manager;
|
||||
private readonly GlobalSettings globalSettings;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
private IConnection connection;
|
||||
private RabbitMQ.Client.IModel channel;
|
||||
|
||||
private IConnection? connection;
|
||||
private IModel? channel;
|
||||
|
||||
public TemplateMQSyncer(
|
||||
ILogger<TemplateMQSyncer> logger,
|
||||
IManager manager,
|
||||
GlobalSettings globalSettings,
|
||||
IServiceProvider serviceProvider
|
||||
GlobalSettings globalSettings
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.manager = manager;
|
||||
this.globalSettings = globalSettings;
|
||||
this.serviceProvider = serviceProvider;
|
||||
|
||||
if (globalSettings.MqSettings == null)
|
||||
{
|
||||
logger.LogError("Нет секции настроек хранилища. MqSettings, EsppTemplates");
|
||||
throw new Exception("Нет секции настроек хранилища. MqSettings, EsppTemplates");
|
||||
}
|
||||
var factory = new ConnectionFactory { HostName = globalSettings.MqSettings.HostName };
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
InitConnection();
|
||||
|
||||
logger.LogInformation($"Запущена проверка очереди {globalSettings.MqSettings!.QueueName}.");
|
||||
|
||||
GetMessages();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
channel?.Close();
|
||||
connection?.Close();
|
||||
|
||||
logger.LogInformation($"=== === === Соединение с очередью {globalSettings.MqSettings!.QueueName} закрыто === === ===");
|
||||
}
|
||||
|
||||
private void InitConnection()
|
||||
{
|
||||
logger.LogInformation($"Устанавливаю соединение с RabbitMQ: {globalSettings.MqSettings!.HostName}");
|
||||
|
||||
var factory = new ConnectionFactory { HostName = globalSettings.MqSettings!.HostName };
|
||||
factory.UserName = globalSettings.MqSettings.UserName;
|
||||
factory.Password = globalSettings.MqSettings.Password;
|
||||
factory.AutomaticRecoveryEnabled = true;
|
||||
factory.DispatchConsumersAsync = true;
|
||||
|
||||
connection = factory.CreateConnection();
|
||||
channel = connection.CreateModel();
|
||||
Dictionary<String, Object> args = new Dictionary<String, Object>();
|
||||
args.Add("x-queue-mode", "lazy");
|
||||
|
||||
var args = new Dictionary<string, object>
|
||||
{
|
||||
{ "x-queue-mode", "lazy" }
|
||||
};
|
||||
|
||||
channel.QueueDeclare(queue: globalSettings.MqSettings.QueueName,
|
||||
durable: true,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: args);
|
||||
}
|
||||
public async Task StartAsync()
|
||||
{
|
||||
logger.LogInformation($"Запущена проверка очереди {globalSettings.MqSettings!.QueueName}. Интервал: {globalSettings.MqSettings!.CheckIntervalSeconds} секунд.");
|
||||
|
||||
// CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
|
||||
// CancellationToken token = cancelTokenSource.Token;
|
||||
// Task task = new Task(() => { GetMessages(); }, token);
|
||||
// task.Start();
|
||||
// await task.WaitAsync(token);
|
||||
GetMessages();
|
||||
logger.LogInformation("Соединение с RabbitMQ установлено.");
|
||||
}
|
||||
|
||||
private void GetMessages()
|
||||
{
|
||||
if (channel == null || connection == null)
|
||||
{
|
||||
logger.LogError("Отсутствует соедниение с RabbitMQ.");
|
||||
return;
|
||||
}
|
||||
|
||||
var consumer = new AsyncEventingBasicConsumer(channel);
|
||||
|
||||
consumer.Received += async (ch, ea) =>
|
||||
{
|
||||
var content = Encoding.UTF8.GetString(ea.Body.ToArray());
|
||||
logger.LogDebug($"Получено сообщение: {content}");
|
||||
|
||||
// Обрабатываем полученное сообщение
|
||||
await manager.ManageStringAsync(content);
|
||||
|
||||
// ------------
|
||||
|
||||
//using var scope = serviceProvider.CreateScope();
|
||||
//var services = scope.ServiceProvider;
|
||||
//var manager = services.GetRequiredService<IManager>();
|
||||
|
||||
//// ------------
|
||||
|
||||
|
||||
//if (manager != null) await manager.ManageStringAsync(content);
|
||||
|
||||
channel.BasicAck(ea.DeliveryTag, false);
|
||||
await Task.Yield();
|
||||
};
|
||||
|
||||
channel.BasicConsume(globalSettings.MqSettings!.QueueName, false, consumer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
channel.Close();
|
||||
connection.Close();
|
||||
logger.LogInformation($"=== === === Соединение с очередью {globalSettings.MqSettings!.QueueName} закрыто === === ===");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user