Fix Telegramas
This commit is contained in:
@@ -578,6 +578,9 @@ public class Worker : BackgroundService
|
|||||||
|
|
||||||
_logger.LogInformation("Iniciando sondeo de Telegramas para {count} combinaciones... con paralelismo de {degree}", combinaciones.Count(), GRADO_DE_PARALELISMO);
|
_logger.LogInformation("Iniciando sondeo de Telegramas para {count} combinaciones... con paralelismo de {degree}", combinaciones.Count(), GRADO_DE_PARALELISMO);
|
||||||
|
|
||||||
|
// Usaremos un ConcurrentBag para recolectar de forma segura los telegramas nuevos desde múltiples hilos.
|
||||||
|
var telegramasNuevosParaGuardar = new ConcurrentBag<Telegrama>();
|
||||||
|
|
||||||
var tareas = combinaciones.Select(async item =>
|
var tareas = combinaciones.Select(async item =>
|
||||||
{
|
{
|
||||||
await semaforo.WaitAsync(stoppingToken);
|
await semaforo.WaitAsync(stoppingToken);
|
||||||
@@ -590,8 +593,6 @@ public class Worker : BackgroundService
|
|||||||
using var innerScope = _serviceProvider.CreateScope();
|
using var innerScope = _serviceProvider.CreateScope();
|
||||||
var innerDbContext = innerScope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
var innerDbContext = innerScope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
||||||
|
|
||||||
// --- CORRECCIÓN CLAVE ---
|
|
||||||
// 'idsDeApi' ya es una List<string>, no necesitamos hacer .Select(t => t[0])
|
|
||||||
var idsYaEnDb = await innerDbContext.Telegramas
|
var idsYaEnDb = await innerDbContext.Telegramas
|
||||||
.Where(t => idsDeApi.Contains(t.Id))
|
.Where(t => idsDeApi.Contains(t.Id))
|
||||||
.Select(t => t.Id)
|
.Select(t => t.Id)
|
||||||
@@ -599,17 +600,13 @@ public class Worker : BackgroundService
|
|||||||
|
|
||||||
var nuevosTelegramasIds = idsDeApi.Except(idsYaEnDb).ToList();
|
var nuevosTelegramasIds = idsDeApi.Except(idsYaEnDb).ToList();
|
||||||
|
|
||||||
if (!nuevosTelegramasIds.Any())
|
if (!nuevosTelegramasIds.Any()) return;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("Se encontraron {count} telegramas nuevos en '{partido}' para '{cat}'. Descargando...", nuevosTelegramasIds.Count, item.partido.Nombre, item.categoria.Nombre);
|
_logger.LogInformation("Se encontraron {count} telegramas nuevos en '{partido}' para '{cat}'. Descargando en paralelo...", nuevosTelegramasIds.Count, item.partido.Nombre, item.categoria.Nombre);
|
||||||
|
|
||||||
// Iteramos y descargamos cada nuevo telegrama.
|
// --- NUEVA OPTIMIZACIÓN: Paralelizar la descarga de los archivos ---
|
||||||
foreach (var mesaId in nuevosTelegramasIds)
|
await Task.WhenAll(nuevosTelegramasIds.Select(async mesaId =>
|
||||||
{
|
{
|
||||||
if (stoppingToken.IsCancellationRequested) break;
|
|
||||||
var telegramaFile = await _apiService.GetTelegramaFileAsync(authToken, mesaId);
|
var telegramaFile = await _apiService.GetTelegramaFileAsync(authToken, mesaId);
|
||||||
if (telegramaFile != null)
|
if (telegramaFile != null)
|
||||||
{
|
{
|
||||||
@@ -621,23 +618,39 @@ public class Worker : BackgroundService
|
|||||||
FechaEscaneo = DateTime.Parse(telegramaFile.FechaEscaneo).ToUniversalTime(),
|
FechaEscaneo = DateTime.Parse(telegramaFile.FechaEscaneo).ToUniversalTime(),
|
||||||
FechaTotalizacion = DateTime.Parse(telegramaFile.FechaTotalizacion).ToUniversalTime()
|
FechaTotalizacion = DateTime.Parse(telegramaFile.FechaTotalizacion).ToUniversalTime()
|
||||||
};
|
};
|
||||||
await innerDbContext.Telegramas.AddAsync(nuevoTelegrama, stoppingToken);
|
telegramasNuevosParaGuardar.Add(nuevoTelegrama);
|
||||||
|
}
|
||||||
|
// Si telegramaFile es null (por 403, 200 vacío, etc.), simplemente no hacemos nada.
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Guardamos los cambios de ESTA tarea específica en la BD.
|
// --- NUEVA ROBUSTEZ: Capturar errores por tarea ---
|
||||||
await innerDbContext.SaveChangesAsync(stoppingToken);
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
|
// Si una combinación entera falla (ej. getTelegramasTotalizados da 500), lo logueamos
|
||||||
|
// pero NO relanzamos la excepción, para no cancelar el Task.WhenAll principal.
|
||||||
|
_logger.LogError(ex, "Falló el sondeo de telegramas para el partido '{partido}' y categoría '{cat}'", item.partido.Nombre, item.categoria.Nombre);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// Liberamos el semáforo para que otra tarea pueda comenzar.
|
|
||||||
semaforo.Release();
|
semaforo.Release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ejecutamos todas las tareas en paralelo y esperamos a que finalicen.
|
// Esperamos a que todas las tareas de sondeo y descarga terminen.
|
||||||
await Task.WhenAll(tareas);
|
await Task.WhenAll(tareas);
|
||||||
|
|
||||||
|
// --- Guardado Masivo Final ---
|
||||||
|
// Después de que todo el paralelismo ha terminado, hacemos una única operación de escritura en la BD.
|
||||||
|
if (!telegramasNuevosParaGuardar.IsEmpty)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Guardando un total de {count} telegramas nuevos en la base de datos...", telegramasNuevosParaGuardar.Count);
|
||||||
|
using var finalScope = _serviceProvider.CreateScope();
|
||||||
|
var finalDbContext = finalScope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
||||||
|
await finalDbContext.Telegramas.AddRangeAsync(telegramasNuevosParaGuardar, stoppingToken);
|
||||||
|
await finalDbContext.SaveChangesAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Sondeo de Telegramas completado.");
|
_logger.LogInformation("Sondeo de Telegramas completado.");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user