using System; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using WebApplication3.Clases; using System.IO; using WebApplication3.Controllers; using WebApplication3.Models; namespace WebApplication3.Clases { public class QueueWorker : BackgroundService { private readonly ILogger _logger; private ServicioController servicioController; private SimulacionController simulacionController; private ServiceQueueController serviceQueueController; public QueueWorker(ILogger logger) { _logger = logger; var aquiferContext = new AquiferContext(); simulacionController = new SimulacionController(aquiferContext); serviceQueueController = new ServiceQueueController(aquiferContext); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("HEY QUEUE WORKER HERE", DateTimeOffset.Now); var serviceRunning = await serviceQueueController.ServiceRunning(); _logger.LogInformation(serviceRunning.ToString(), DateTimeOffset.Now); if (serviceRunning == false) { _logger.LogInformation("NOTHING RUNNING", DateTimeOffset.Now); var nextInQueue = serviceQueueController.GetNextInQueue(); if (nextInQueue != null) { _logger.LogInformation("RUNNING A SIM", DateTimeOffset.Now); _logger.LogInformation(nextInQueue.Id.ToString(), DateTimeOffset.Now); //Actualizamos el estado del item del servicio/simulacion a ejecutar //await serviceQueueController.SetCorriendo(nextInQueue.Id); //Se ejecuta la simulacion await simulacionController.SimulaAsync(nextInQueue); ////Se actualiza el estado del item ya que ha terminado su ejecucion //await serviceQueueController.SetTerminado(nextInQueue.Id); } } await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken); } } public void LogInfo(string msg) { _logger.LogInformation(msg); } } }