Aggiunto invio su PipeChannel x timbrature e richieste

This commit is contained in:
Samuele Locatelli
2024-03-05 10:30:54 +01:00
parent 63e92f0c83
commit de2a8cb6a5
8 changed files with 232 additions and 9 deletions
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPW.CORE.Data
{
public class Const
{
#region Public Fields
public const string rPipeChRich = $"Channel_Rich";
public const string rPipeChTimb = $"Channel_Timb";
#endregion Public Fields
}
}
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EgwCoreLib.Utils" Version="1.5.2402.2411" />
<PackageReference Include="MailKit" Version="3.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.27" />
</ItemGroup>
</Project>
+2 -1
View File
@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EgwCoreLib.Utils" Version="1.4.2402.2316" />
<PackageReference Include="EgwCoreLib.Utils" Version="1.5.2402.2411" />
<PackageReference Include="MailKit" Version="3.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.14" />
@@ -19,6 +19,7 @@
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.27" />
</ItemGroup>
</Project>
+158
View File
@@ -0,0 +1,158 @@
using Microsoft.EntityFrameworkCore.Storage;
using NLog;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPW.CORE.Data
{
public class MessagePipe
{
#region Public Constructors
public MessagePipe(IConnectionMultiplexer redisConn, string channelName, bool enableLog = false)
{
_channel = channelName;
redis = redisConn;
redisDb = redis.GetDatabase();
this.enableLog = enableLog;
// aggiungo sottoscrittore
setupSubscriber();
}
#endregion Public Constructors
#region Public Events
public event EventHandler EA_NewMessage = delegate { };
#endregion Public Events
#region Public Methods
/// <summary>
/// Invio messaggio sul canale + salvataggio in cache REDIS
/// </summary>
/// <param name="memKey">Chiave REDIS x salvare valore</param>
/// <param name="message">Messaggio serializzato da inviare</param>
public bool saveAndSendMessage(string memKey, string message)
{
bool answ = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// invio notifica tramite il canale richiesto
answ = sendMessage(message);
if (redisDb != null)
{
redisDb.StringSetAsync(memKey, message);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
if (numSent.ContainsKey(memKey))
{
numSent[memKey]++;
}
else
{
numSent.Add(memKey, 1);
}
if (enableLog || numSent[memKey] > 30)
{
Log.Info($"saveAndSendMessage| mKey {memKey} x {numSent[memKey]} | {message.Length} size | {ts.TotalMilliseconds} ms");
numSent[memKey] = 0;
}
return answ;
}
/// <summary>
/// Invio messaggio sul canale
/// </summary>
/// <param name="newMess"></param>
/// <returns></returns>
public bool sendMessage(string newMess)
{
bool answ = false;
ISubscriber sub = redis.GetSubscriber();
sub.Publish(_channel, newMess);
return answ;
}
#endregion Public Methods
#region Protected Fields
protected static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Protected Fields
#region Private Fields
private bool enableLog = false;
private Dictionary<string, int> numSent = new Dictionary<string, int>();
private IConnectionMultiplexer redis;
private StackExchange.Redis.IDatabase redisDb;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Canale associato al gestore pipeline messaggi
/// </summary>
private string _channel { get; set; } = "";
#endregion Private Properties
#region Private Methods
private void setupSubscriber()
{
ISubscriber sub = redis.GetSubscriber();
//Subscribe to the channel named messages
sub.Subscribe(_channel, (channel, message) =>
{
if (enableLog)
{
Log.Trace($"req setup ch {channel} | {message}");
}
// messaggio
PubSubEventArgs mea = new PubSubEventArgs(message);
// se qualcuno ascolta sollevo evento nuovo valore...
if (EA_NewMessage != null)
{
EA_NewMessage(this, mea);
}
});
if (enableLog)
{
Log.Info($"Subscribed {_channel}");
}
}
#endregion Private Methods
}
public class PubSubEventArgs : EventArgs
{
#region Public Constructors
public PubSubEventArgs(string messaggio)
{
this.newMessage = messaggio;
}
#endregion Public Constructors
#region Public Properties
public string newMessage { get; set; } = "";
#endregion Public Properties
}
}
@@ -98,7 +98,7 @@ namespace GPW.CORE.Smart.Components
IdxDipendente = idxDip,
Ipv4 = $"{ipv4}"
};
done= await CDService.TimbratureUpdate(currRecord);
done = await CDService.TimbratureUpdate(currRecord);
}
}
}
+22 -1
View File
@@ -1,8 +1,10 @@
using EgwCoreLib.Razor.Data;
using EgwCoreLib.Utils;
using GPW.CORE.Data;
using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using NLog;
using StackExchange.Redis;
@@ -51,6 +53,11 @@ namespace GPW.CORE.Smart.Data
{
dbController = new CORE.Data.Controllers.GPWController(configuration);
}
// init datapipe...
mPipeTimb = new MessagePipe(redisConn, Const.rPipeChTimb);
mPipeRich = new MessagePipe(redisConn, Const.rPipeChRich);
_logger.LogInformation("Avviata classe CoreSmartDataService");
}
@@ -60,6 +67,16 @@ namespace GPW.CORE.Smart.Data
public string CodApp { get; set; } = "";
/// <summary>
/// Pipe messaggi richieste
/// </summary>
public MessagePipe mPipeRich { get; set; } = null!;
/// <summary>
/// Pipe messaggi timbrature
/// </summary>
public MessagePipe mPipeTimb { get; set; } = null!;
#endregion Public Properties
#region Public Methods
@@ -1301,7 +1318,7 @@ namespace GPW.CORE.Smart.Data
}
/// <summary>
/// Inserimento richeista mancata timbratura
/// Inserimento richiesta mancata timbratura
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
@@ -1315,6 +1332,8 @@ namespace GPW.CORE.Smart.Data
currItem.Approv = false;
// upsert!
answ = dbController.TimbratureUpdate(currItem);
// invio in pipe timbratura
mPipeRich.sendMessage(JsonConvert.SerializeObject(currItem));
Log.Info($"Registrata richiesta Mancata Timbratura | idxDip {currItem.IdxDipendente} | data-ora: {currItem.DataOra} | isEntrata: {currItem.Entrata}");
// invalido la cache...
await FlushRedisCache();
@@ -1347,6 +1366,8 @@ namespace GPW.CORE.Smart.Data
try
{
answ = dbController.TimbratureUpdate(currItem);
// invio in pipe timbratura
mPipeTimb.sendMessage(JsonConvert.SerializeObject(currItem));
// invalido la cache...
await ExecFlushRedisPattern($"{rKeyDailyData}:*");
}
+4 -4
View File
@@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Version>3.0.2403.0508</Version>
<Version>3.0.2403.0510</Version>
<ImplicitUsings>enable</ImplicitUsings>
<PackageProjectUrl>www.egalware.com</PackageProjectUrl>
<Description>GPW Smart UI</Description>
@@ -28,10 +28,10 @@
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
<PackageReference Include="Blazored.SessionStorage" Version="2.3.0" />
<PackageReference Include="EgwCoreLib.Razor" Version="1.4.2402.2316" />
<PackageReference Include="EgwCoreLib.Utils" Version="1.4.2402.2316" />
<PackageReference Include="EgwCoreLib.Razor" Version="1.5.2402.2411" />
<PackageReference Include="EgwCoreLib.Utils" Version="1.5.2402.2411" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.27" />
</ItemGroup>
<ItemGroup>
+2 -2
View File
@@ -42,7 +42,7 @@
<script>
Blazor.start({
reconnectionOptions: {
maxRetries: 600,
maxRetries: 3600,
retryIntervalMilliseconds: 1000
},
}).then(() => {
@@ -50,7 +50,7 @@
console.log("Client reconnected, waiting 5 sec and reload!");
setTimeout(function () {
document.location.reload();
}, 3000);
}, 2000);
}
});
</script>