Fix Paralelizmo en Procesos de baja prioridad
This commit is contained in:
		| @@ -12,6 +12,9 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|   private readonly IServiceProvider _serviceProvider; | ||||
|   private readonly IElectoralApiService _apiService; | ||||
|  | ||||
|   // Una variable para rastrear la tarea de telegramas, si está en ejecución. | ||||
|   private Task? _telegramasTask; | ||||
|  | ||||
|   public LowPriorityDataWorker( | ||||
|   ILogger<LowPriorityDataWorker> logger, | ||||
|   SharedTokenService tokenService, | ||||
| @@ -28,6 +31,7 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|   { | ||||
|     _logger.LogInformation("Worker de Baja Prioridad iniciado."); | ||||
|  | ||||
|     // La sincronización inicial sigue siendo un paso de bloqueo, es necesario. | ||||
|     await SincronizarCatalogosMaestrosAsync(stoppingToken); | ||||
|  | ||||
|     while (!stoppingToken.IsCancellationRequested) | ||||
| @@ -42,8 +46,28 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|         continue; | ||||
|       } | ||||
|  | ||||
|       // --- LÓGICA DE EJECUCIÓN INDEPENDIENTE --- | ||||
|  | ||||
|       // 1. TAREA DE BANCAS: Siempre se ejecuta y se espera. Es rápida. | ||||
|       _logger.LogInformation("Iniciando sondeo de Bancas..."); | ||||
|       await SondearProyeccionBancasAsync(authToken, stoppingToken); | ||||
|       await SondearNuevosTelegramasAsync(authToken, stoppingToken); | ||||
|       _logger.LogInformation("Sondeo de Bancas completado."); | ||||
|  | ||||
|       // 2. TAREA DE TELEGRAMAS: "Dispara y Olvida" de forma segura. | ||||
|       // Comprobamos si la tarea anterior de telegramas ya ha terminado. | ||||
|       if (_telegramasTask == null || _telegramasTask.IsCompleted) | ||||
|       { | ||||
|         _logger.LogInformation("Iniciando sondeo de Telegramas en segundo plano..."); | ||||
|         // Lanzamos la tarea de telegramas pero NO la esperamos con 'await'. | ||||
|         // Guardamos una referencia a la tarea en nuestra variable de estado. | ||||
|         _telegramasTask = SondearNuevosTelegramasAsync(authToken, stoppingToken); | ||||
|       } | ||||
|       else | ||||
|       { | ||||
|         // Si la descarga anterior todavía está en curso, nos saltamos este sondeo | ||||
|         // para no acumular tareas y sobrecargar el sistema. | ||||
|         _logger.LogInformation("El sondeo de telegramas anterior sigue en ejecución. Se omitirá en este ciclo."); | ||||
|       } | ||||
|  | ||||
|       _logger.LogInformation("--- Ciclo de Datos de Baja Prioridad completado. Esperando 5 minutos. ---"); | ||||
|       try | ||||
|   | ||||
		Reference in New Issue
	
	Block a user