Fix Paralelizmo en Procesos de baja prioridad
This commit is contained in:
		| @@ -12,11 +12,14 @@ public class LowPriorityDataWorker : BackgroundService | |||||||
|   private readonly IServiceProvider _serviceProvider; |   private readonly IServiceProvider _serviceProvider; | ||||||
|   private readonly IElectoralApiService _apiService; |   private readonly IElectoralApiService _apiService; | ||||||
|  |  | ||||||
|  |   // Una variable para rastrear la tarea de telegramas, si está en ejecución. | ||||||
|  |   private Task? _telegramasTask; | ||||||
|  |  | ||||||
|   public LowPriorityDataWorker( |   public LowPriorityDataWorker( | ||||||
|       ILogger<LowPriorityDataWorker> logger, |   ILogger<LowPriorityDataWorker> logger, | ||||||
|       SharedTokenService tokenService, |   SharedTokenService tokenService, | ||||||
|       IServiceProvider serviceProvider, |   IServiceProvider serviceProvider, | ||||||
|       IElectoralApiService apiService) |   IElectoralApiService apiService) | ||||||
|   { |   { | ||||||
|     _logger = logger; |     _logger = logger; | ||||||
|     _tokenService = tokenService; |     _tokenService = tokenService; | ||||||
| @@ -28,6 +31,7 @@ public class LowPriorityDataWorker : BackgroundService | |||||||
|   { |   { | ||||||
|     _logger.LogInformation("Worker de Baja Prioridad iniciado."); |     _logger.LogInformation("Worker de Baja Prioridad iniciado."); | ||||||
|  |  | ||||||
|  |     // La sincronización inicial sigue siendo un paso de bloqueo, es necesario. | ||||||
|     await SincronizarCatalogosMaestrosAsync(stoppingToken); |     await SincronizarCatalogosMaestrosAsync(stoppingToken); | ||||||
|  |  | ||||||
|     while (!stoppingToken.IsCancellationRequested) |     while (!stoppingToken.IsCancellationRequested) | ||||||
| @@ -42,8 +46,28 @@ public class LowPriorityDataWorker : BackgroundService | |||||||
|         continue; |         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 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. ---"); |       _logger.LogInformation("--- Ciclo de Datos de Baja Prioridad completado. Esperando 5 minutos. ---"); | ||||||
|       try |       try | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user