68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
using EgwCoreLib.Lux.Data.Services;
|
|
|
|
namespace Lux.API.Services
|
|
{
|
|
public class RedisSubscriberService : BackgroundService
|
|
{
|
|
|
|
#if false
|
|
public RedisSubscriberService(IConfiguration config, RedisSubscriptionManager subManager, ExternalMessageProcessor processor)
|
|
{
|
|
_subManager = subManager;
|
|
_processor = processor;
|
|
_config = config;
|
|
chSub = _config.GetValue<string>("ServerConf:ChannelSub") ?? "";
|
|
}
|
|
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_subManager.Subscribe(chSub, async (ch, msg) =>
|
|
{
|
|
await _processor.HandleResultMessageAsync($"{ch}", $"{msg}");
|
|
});
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
|
|
private readonly IConfiguration _config;
|
|
private readonly ExternalMessageProcessor _processor;
|
|
private readonly RedisSubscriptionManager _subManager;
|
|
private readonly string chSub = "";
|
|
#endif
|
|
|
|
|
|
private readonly IConfiguration _config;
|
|
private readonly RedisSubscriptionManager _subManager;
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
private readonly string _channel;
|
|
|
|
public RedisSubscriberService(
|
|
IConfiguration config,
|
|
RedisSubscriptionManager subManager,
|
|
IServiceScopeFactory scopeFactory)
|
|
{
|
|
_config = config;
|
|
_subManager = subManager;
|
|
_scopeFactory = scopeFactory;
|
|
|
|
_channel = _config.GetValue<string>("ServerConf:ChannelSub") ?? "";
|
|
}
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_subManager.Subscribe(_channel, async (ch, msg) =>
|
|
{
|
|
// Ogni messaggio → nuovo scope
|
|
using var scope = _scopeFactory.CreateScope();
|
|
|
|
var processor = scope.ServiceProvider.GetRequiredService<ExternalMessageProcessor>();
|
|
|
|
await processor.HandleResultMessageAsync($"{ch}", $"{msg}");
|
|
});
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
} |