67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EgwCoreLib.Lux.Data.Services
|
|
{
|
|
public class RedisSubscriberServiceOld : BackgroundService
|
|
{
|
|
private readonly IRedisService _redis;
|
|
private readonly ILogger<RedisSubscriberServiceOld> _logger;
|
|
|
|
public RedisSubscriberServiceOld(IRedisService redis, ILogger<RedisSubscriberServiceOld> logger)
|
|
{
|
|
_redis = redis;
|
|
_logger = logger;
|
|
}
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_redis.Subscribe("user:notifications", HandleUserNotification);
|
|
_redis.Subscribe("system:alerts", HandleSystemAlert);
|
|
_redis.Subscribe("data:sync", HandleDataSync);
|
|
|
|
_redis.Subscribe("yourChannel", (channel, message) =>
|
|
{
|
|
_logger.LogInformation($"Message received: {message}");
|
|
// Call your message handler here
|
|
});
|
|
|
|
// versione async task...
|
|
#if false
|
|
_redis.Subscribe("channel", async (ch, msg) =>
|
|
{
|
|
await DoSomethingAsync(msg);
|
|
});
|
|
#endif
|
|
|
|
|
|
// Per mantenere servizio attivo/alive: non è necessario lasciare il loop
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void HandleUserNotification(RedisChannel channel, RedisValue message)
|
|
{
|
|
_logger.LogInformation($"🔔 Notification received: {message}");
|
|
// Route to user service or in-memory cache etc.
|
|
}
|
|
|
|
private void HandleSystemAlert(RedisChannel channel, RedisValue message)
|
|
{
|
|
_logger.LogWarning($"⚠️ System alert received: {message}");
|
|
// Trigger alert logging or email notifications
|
|
}
|
|
|
|
private void HandleDataSync(RedisChannel channel, RedisValue message)
|
|
{
|
|
_logger.LogInformation($"🔄 Sync event received: {message}");
|
|
// Refresh cache, rehydrate objects, etc.
|
|
}
|
|
}
|
|
}
|