Update a stored e metodi x evitare race conditions su scrittura remoteRebootLog
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user