Update a stored e metodi x evitare race conditions su scrittura remoteRebootLog

This commit is contained in:
Samuele Locatelli
2026-04-28 08:23:27 +02:00
parent d9ede3aae3
commit d2ec5b15d4
6 changed files with 34 additions and 18 deletions
+30 -13
View File
@@ -1060,24 +1060,40 @@ namespace MP.Data.Controllers
{
bool fatto = false;
using var dbCtx = new MoonProContext(_configuration);
using var transaction = await dbCtx.Database.BeginTransactionAsync();
// 1. Transazione minima: SOLO INSERT + COMMIT
await using var tx = await dbCtx.Database.BeginTransactionAsync();
try
{
dbCtx.DbSetRemRebLog.Add(newRec);
fatto = await dbCtx.SaveChangesAsync() > 0;
var param = new SqlParameter("@num2keep", num2keep);
await dbCtx.Database.ExecuteSqlRawAsync("EXEC dbo.stp_RRL_KeepLatest @num2keep", param);
await transaction.CommitAsync();
return true;
await tx.CommitAsync(); // 🔑 Commit prima della pulizia
}
catch
{
await transaction.RollbackAsync();
await tx.RollbackAsync();
throw;
}
// 2. Cleanup "smart" fuori transazione
if (fatto)
{
try
{
var param = new SqlParameter("@num2keep", num2keep);
await dbCtx.Database.ExecuteSqlRawAsync(
"EXEC dbo.stp_RRL_KeepLatest @num2keep, @multiplier", param,
new SqlParameter("@multiplier", 1.3f)); // Fisso o leggo da config
}
catch (Exception ex)
{
// Log dell'errore, ma NON rilanciare: il record è già salvo
Log.Error(ex, "Failed to execute RRL cleanup for {Machine}", newRec.IdxMacchina);
}
}
return fatto;
}
/// <summary>
@@ -1119,10 +1135,11 @@ namespace MP.Data.Controllers
{
using var dbCtx = new MoonProContext(_configuration);
var Num2keep = new SqlParameter("@num2keep", num2keep);
var dbResult = await dbCtx
.Database
.ExecuteSqlRawAsync("exec dbo.stp_RRL_KeepLatest @num2keep", Num2keep);
var pNum2Keep = new SqlParameter("@num2keep", num2keep);
// La SP gestisce già la logica di soglia (1.5x), ma la specifico x sicurezza
var pThresh = new SqlParameter("@threshMult", 1.5);
var dbResult = await dbCtx.Database.ExecuteSqlRawAsync(
"EXEC dbo.stp_RRL_KeepLatest @num2keep, @threshMult", pNum2Keep, pThresh);
return dbResult != 0;
}
-1
View File
@@ -1156,7 +1156,6 @@ namespace MP.IOC.Controllers
// recupero IP del client remoto, se vuoto IOC
string agent = Request.Headers["User-Agent"].ToString() ?? "IOC";
agent = string.IsNullOrWhiteSpace(agent) ? "IOC" : agent;
//var ipv4 = HttpContext.Connection.RemoteIpAddress?.ToString();
var ipv4 = HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
// chiamo registrazione reboot
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>8.16.2604.2718</Version>
<Version>8.16.2604.2808</Version>
</PropertyGroup>
<ItemGroup>
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo MP-IOC </i>
<h4>Versione: 8.16.2604.2718</h4>
<h4>Versione: 8.16.2604.2808</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
8.16.2604.2718
8.16.2604.2808
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>8.16.2604.2718</version>
<version>8.16.2604.2808</version>
<url>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>