Fix TimeOut Telegramas 1425
This commit is contained in:
@@ -249,14 +249,11 @@ public class CriticalDataWorker : BackgroundService
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
||||
|
||||
var partidos = await dbContext.AmbitosGeograficos
|
||||
.AsNoTracking()
|
||||
// La obtención de partidos y categorías no cambia
|
||||
var partidos = await dbContext.AmbitosGeograficos.AsNoTracking()
|
||||
.Where(a => a.NivelId == 30 && a.DistritoId != null && a.SeccionId != null)
|
||||
.ToListAsync(stoppingToken);
|
||||
|
||||
var categorias = await dbContext.CategoriasElectorales
|
||||
.AsNoTracking()
|
||||
.ToListAsync(stoppingToken);
|
||||
var categorias = await dbContext.CategoriasElectorales.AsNoTracking().ToListAsync(stoppingToken);
|
||||
|
||||
if (!partidos.Any() || !categorias.Any()) return;
|
||||
|
||||
@@ -270,13 +267,13 @@ public class CriticalDataWorker : BackgroundService
|
||||
|
||||
if (listaTelegramasApi is { Count: > 0 })
|
||||
{
|
||||
// Creamos el DbContext para la operación de guardado
|
||||
using var innerScope = _serviceProvider.CreateScope();
|
||||
var innerDbContext = innerScope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
||||
|
||||
var idsYaEnDb = await innerDbContext.Telegramas
|
||||
.Where(t => listaTelegramasApi.Contains(t.Id))
|
||||
.Select(t => t.Id)
|
||||
.ToListAsync(stoppingToken);
|
||||
.Select(t => t.Id).ToListAsync(stoppingToken);
|
||||
|
||||
var nuevosTelegramasIds = listaTelegramasApi.Except(idsYaEnDb).ToList();
|
||||
|
||||
@@ -284,8 +281,14 @@ public class CriticalDataWorker : BackgroundService
|
||||
{
|
||||
_logger.LogInformation("Se encontraron {count} telegramas nuevos en '{partido}' para '{cat}'. Descargando...", nuevosTelegramasIds.Count, partido.Nombre, categoria.Nombre);
|
||||
|
||||
var originalTimeout = innerDbContext.Database.GetCommandTimeout();
|
||||
try
|
||||
{
|
||||
innerDbContext.Database.SetCommandTimeout(180);
|
||||
_logger.LogDebug("Timeout de BD aumentado a 180s para descarga de telegramas.");
|
||||
|
||||
int contadorLote = 0;
|
||||
const int tamanoLote = 5; // Guardaremos de 100 en 100
|
||||
const int tamanoLote = 100;
|
||||
|
||||
foreach (var mesaId in nuevosTelegramasIds)
|
||||
{
|
||||
@@ -302,40 +305,45 @@ public class CriticalDataWorker : BackgroundService
|
||||
var nuevoTelegrama = new Telegrama
|
||||
{
|
||||
Id = telegramaFile.NombreArchivo,
|
||||
// 3. Usamos el ID del ÁMBITO DE LA MESA, no el del municipio.
|
||||
AmbitoGeograficoId = ambitoMesa.Id,
|
||||
ContenidoBase64 = telegramaFile.Imagen,
|
||||
FechaEscaneo = DateTime.Parse(telegramaFile.FechaEscaneo).ToUniversalTime(),
|
||||
FechaTotalizacion = DateTime.Parse(telegramaFile.FechaTotalizacion).ToUniversalTime()
|
||||
};
|
||||
await innerDbContext.Telegramas.AddAsync(nuevoTelegrama, stoppingToken);
|
||||
contadorLote++; // Incrementamos el contador
|
||||
contadorLote++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No se encontró un ámbito geográfico para la mesa con MesaId {MesaId}. El telegrama no será guardado.", mesaId);
|
||||
}
|
||||
}
|
||||
await Task.Delay(250, stoppingToken); // Mantenemos el delay para no saturar la API
|
||||
await Task.Delay(250, stoppingToken);
|
||||
|
||||
// Si hemos alcanzado el tamaño del lote, guardamos y reseteamos.
|
||||
if (contadorLote >= tamanoLote)
|
||||
{
|
||||
await innerDbContext.SaveChangesAsync(stoppingToken);
|
||||
_logger.LogInformation("Guardado un lote de {count} telegramas.", contadorLote);
|
||||
contadorLote = 0; // Reseteamos el contador para el siguiente lote
|
||||
contadorLote = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Guardamos cualquier registro restante que no haya completado un lote completo.
|
||||
if (contadorLote > 0)
|
||||
{
|
||||
await innerDbContext.SaveChangesAsync(stoppingToken);
|
||||
_logger.LogInformation("Guardado el último lote de {count} telegramas.", contadorLote);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
innerDbContext.Database.SetCommandTimeout(originalTimeout);
|
||||
_logger.LogDebug("Timeout de BD restaurado a su valor original ({timeout}s).", originalTimeout);
|
||||
}
|
||||
} // Fin del if (nuevosTelegramasIds.Any())
|
||||
|
||||
// Movemos el delay aquí para que solo se ejecute si hubo telegramas en la respuesta de la API
|
||||
await Task.Delay(100, stoppingToken);
|
||||
} // Fin del if (listaTelegramasApi is not null)
|
||||
}
|
||||
}
|
||||
_logger.LogInformation("Sondeo de Telegramas completado.");
|
||||
|
||||
Reference in New Issue
Block a user