450 lines
15 KiB
C#
450 lines
15 KiB
C#
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
using WebDoorCreator.Core;
|
|
using WebDoorCreator.Data;
|
|
|
|
namespace WebDoorCreator.Data.Services
|
|
{
|
|
public class QueueDataService : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="emailSender"></param>
|
|
/// <param name="redisCacheClient"></param>
|
|
public QueueDataService(IConfiguration configuration, IEmailSender emailSender, IConnectionMultiplexer redisConn)
|
|
{
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
// setup componenti REDIS
|
|
var redConnString = _configuration.GetConnectionString("Redis");
|
|
if (redConnString == null)
|
|
{
|
|
Log.Error("REDIS ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
this.redisConn = ConnectionMultiplexer.Connect(redConnString);
|
|
redisDb = this.redisConn.GetDatabase();
|
|
}
|
|
// setup canali pub/sub
|
|
calcReqPipe = new MessagePipe(redisConn, Constants.CALC_REQ_QUEUE);
|
|
calcDonePipe = new MessagePipe(redisConn, Constants.CALC_DONE_QUEUE);
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Message pipe esecuzione elaborazione CAM --> UI
|
|
/// </summary>
|
|
public MessagePipe calcDonePipe { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Message pipe richieste elaborazione UI --> CAM
|
|
/// </summary>
|
|
public MessagePipe calcReqPipe { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
redisConn.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="DoorId"></param>
|
|
/// <param name="FullDDF">Contenuto completo del DDF</param>
|
|
/// <returns>indice/versione del calcolo DDF</returns>
|
|
public async Task<int> sendCalcReq(int DoorId, string FullDDF)
|
|
{
|
|
int currVers = 1;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
// accumulo richieste di calcolo in HashTable redis
|
|
|
|
|
|
// per prima cosa controllo se ho GIA' in coda qualcosa come richeiste
|
|
var numPending = await NumRequestPending();
|
|
// se coda vuota --> aggiungo e stop
|
|
if (numPending == 0)
|
|
{
|
|
}
|
|
// se coda non vuota: cerco la DoorId corrente, se c'è sovrascrivo versione altrimenti
|
|
// inserisco in coda
|
|
else
|
|
{
|
|
}
|
|
// salvo nell'archivio REDIS delle porte il DDF corrente (door+ vers numb), potrebbe
|
|
// venire buono anche x eventuale UNDO...
|
|
|
|
// invio sul canale dei messaggi il numero di items in coda attuali x chiedere esecuzione...
|
|
|
|
#if false
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND);
|
|
currVers = redisDb.HashLength(currKey);
|
|
if (currVers > 0)
|
|
{
|
|
var rawData = await redisDb.HashGetAllAsync(currKey);
|
|
foreach (var item in rawData)
|
|
{
|
|
dictResult.Add($"{item.Name}", $"{item.Value}");
|
|
}
|
|
}
|
|
#endif
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"EnqueueDoorDDF | DoorId: {DoorId} enqueued in: {ts.TotalMilliseconds} ms");
|
|
|
|
|
|
|
|
// to do
|
|
// calcolo quanti emssaggi ho in coda
|
|
string message = "999";
|
|
// invio sul channel redis la richiesta, salvando il valore anche in cache
|
|
bool answ = calcReqPipe.saveAndSendMessage(Constants.LAST_CALC_REQ_KEY, message);
|
|
|
|
// simulazione ritorno dati...
|
|
await Task.Delay(200);
|
|
|
|
// invio il FINTO messaggio di ritorno...
|
|
string retMess = "";
|
|
string fileName = Path.Combine("temp", $"Logo{idxSim:00}.svg");
|
|
if (File.Exists(fileName))
|
|
{
|
|
retMess = File.ReadAllText(fileName);
|
|
idxSim++;
|
|
idxSim = idxSim % 4;
|
|
}
|
|
calcDonePipe.saveAndSendMessage(Constants.LAST_CALC_DONE_KEY, retMess);
|
|
|
|
return currVers;
|
|
}
|
|
|
|
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{Constants.BASE_HASH}:Cache*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get # of calculation request pending
|
|
/// </summary>
|
|
public async Task<long> NumRequestPending()
|
|
{
|
|
long numReq = 0;
|
|
string source = "REDIS";
|
|
Dictionary<string, string> dictResult = new Dictionary<string, string>();
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND);
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
numReq = await redisDb.HashLengthAsync(currKey);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"NumRequestPending | {source} in: {ts.TotalMilliseconds} ms");
|
|
return numReq;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get # of calculation request processing
|
|
/// </summary>
|
|
public async Task<long> NumRequestProcessing()
|
|
{
|
|
long numReq = 0;
|
|
string source = "REDIS";
|
|
Dictionary<string, string> dictResult = new Dictionary<string, string>();
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PROC);
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
numReq = await redisDb.HashLengthAsync(currKey);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"NumRequestProcessing | {source} in: {ts.TotalMilliseconds} ms");
|
|
return numReq;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Queue request pending
|
|
/// </summary>
|
|
/// <returns>Dictionary of DoorId, saveVersNumb</returns>
|
|
public async Task<Dictionary<string, string>> RequestPending()
|
|
{
|
|
string source = "REDIS";
|
|
long numReq = 0;
|
|
Dictionary<string, string> dictResult = new Dictionary<string, string>();
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND);
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
numReq = redisDb.HashLength(currKey);
|
|
if (numReq > 0)
|
|
{
|
|
var rawData = await redisDb.HashGetAllAsync(currKey);
|
|
foreach (var item in rawData)
|
|
{
|
|
dictResult.Add($"{item.Name}", $"{item.Value}");
|
|
}
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RequestPending | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dictResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Queue request processing
|
|
/// </summary>
|
|
/// <returns>Dictionary of DoorId, saveVersNumb</returns>
|
|
public async Task<Dictionary<string, string>> RequestProcessing()
|
|
{
|
|
string source = "REDIS";
|
|
long numReq = 0;
|
|
Dictionary<string, string> dictResult = new Dictionary<string, string>();
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PROC);
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
numReq = redisDb.HashLength(currKey);
|
|
if (numReq > 0)
|
|
{
|
|
var rawData = await redisDb.HashGetAllAsync(currKey);
|
|
foreach (var item in rawData)
|
|
{
|
|
dictResult.Add($"{item.Name}", $"{item.Value}");
|
|
}
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"RequestProcessing | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dictResult;
|
|
}
|
|
/// <summary>
|
|
/// Get Queue request pending, removing from queue and putting on processing queue
|
|
/// </summary>
|
|
/// <returns>Dictionary of DoorId, saveVersNumb</returns>
|
|
public async Task<Dictionary<string, string>> TakeProcessingItems(int numItems)
|
|
{
|
|
string source = "REDIS";
|
|
long numReq = 0;
|
|
Dictionary<string, string> dictResult = new Dictionary<string, string>();
|
|
// cerco da cache
|
|
RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND);
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
numReq = redisDb.HashLength(currKey);
|
|
if (numReq > 0)
|
|
{
|
|
var rawData = await redisDb.HashGetAllAsync(currKey);
|
|
foreach (var item in rawData)
|
|
{
|
|
dictResult.Add($"{item.Name}", $"{item.Value}");
|
|
}
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"TakeProcessingItems | {source} in: {ts.TotalMilliseconds} ms");
|
|
return dictResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected const string rKeyCalcOreFase = "Check:OreFasi";
|
|
|
|
protected const string rKeyFasiAct = "Check:FasiAct";
|
|
|
|
protected const string rKeyProjAct = "Check:ProjAct";
|
|
|
|
/// <summary>
|
|
/// TTL da 1 min x cache Redis
|
|
/// </summary>
|
|
protected const int shortTTL = 60 * 5;
|
|
|
|
protected int idxSim = 0;
|
|
|
|
protected Random rnd = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Recupero chiave da redis
|
|
/// </summary>
|
|
/// <param name="rKey"></param>
|
|
/// <returns></returns>
|
|
protected async Task<string> getRSV(string rKey)
|
|
{
|
|
string answ = "";
|
|
var rawData = await redisDb.StringGetAsync(rKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
answ = $"{rawData}";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salvataggio chiave in redis
|
|
/// </summary>
|
|
/// <param name="rKey"></param>
|
|
/// <param name="rVal"></param>
|
|
/// <param name="ttlSec"></param>
|
|
/// <returns></returns>
|
|
protected async Task<bool> setRSV(string rKey, string rVal, int ttlSec)
|
|
{
|
|
bool fatto = false;
|
|
await redisDb.StringSetAsync(rKey, rVal, TimeSpan.FromSeconds(ttlSec));
|
|
fatto = true;
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salvataggio chiave in redis
|
|
/// </summary>
|
|
/// <param name="rKey"></param>
|
|
/// <param name="rValInt"></param>
|
|
/// <param name="ttlSec"></param>
|
|
/// <returns></returns>
|
|
protected async Task<bool> setRSV(string rKey, int rValInt, int ttlSec)
|
|
{
|
|
bool fatto = false;
|
|
await redisDb.StringSetAsync(rKey, rValInt, TimeSpan.FromSeconds(ttlSec));
|
|
fatto = true;
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registra in cache chiave se non fosse già in elenco
|
|
/// </summary>
|
|
/// <param name="newKey"></param>
|
|
protected void trackCache(string newKey)
|
|
{
|
|
if (!cachedDataList.Contains(newKey))
|
|
{
|
|
cachedDataList.Add(newKey);
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
/// <summary>
|
|
/// Elenco obj in cache
|
|
/// </summary>
|
|
private List<string> cachedDataList = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlLong = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata cache breve IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlShort = 60 * 1;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan FastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan LongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraLongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
// brutalmente rimuovo intero contenuto DB... DANGER
|
|
//await server.FlushDatabaseAsync();
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |