QueueWorker.cs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.Hosting;
  5. using Microsoft.Extensions.Logging;
  6. using WebApplication3.Clases;
  7. using System.IO;
  8. using WebApplication3.Controllers;
  9. using WebApplication3.Models;
  10. namespace WebApplication3.Clases
  11. {
  12. public class QueueWorker : BackgroundService
  13. {
  14. private readonly ILogger<QueueWorker> _logger;
  15. private ServicioController servicioController;
  16. private SimulacionController simulacionController;
  17. private ServiceQueueController serviceQueueController;
  18. public QueueWorker(ILogger<QueueWorker> logger)
  19. {
  20. _logger = logger;
  21. var aquiferContext = new AquiferContext();
  22. servicioController = new ServicioController(aquiferContext);
  23. simulacionController = new SimulacionController(aquiferContext);
  24. serviceQueueController = new ServiceQueueController(aquiferContext);
  25. }
  26. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  27. {
  28. while (!stoppingToken.IsCancellationRequested)
  29. {
  30. _logger.LogInformation("HEY QUEUE WORKER HERE", DateTimeOffset.Now);
  31. var serviceRunning = await serviceQueueController.ServiceRunning();
  32. _logger.LogInformation(serviceRunning.ToString(), DateTimeOffset.Now);
  33. if (serviceRunning == false)
  34. {
  35. _logger.LogInformation("NOTHING RUNNING", DateTimeOffset.Now);
  36. var nextInQueue = serviceQueueController.GetNextInQueue();
  37. if (nextInQueue != null)
  38. {
  39. _logger.LogInformation("RUNNING A SIM", DateTimeOffset.Now);
  40. _logger.LogInformation(nextInQueue.Id.ToString(), DateTimeOffset.Now);
  41. //Actualizamos el estado del item del servicio/simulacion a ejecutar
  42. //await serviceQueueController.SetCorriendo(nextInQueue.Id);
  43. //Se ejecuta la simulacion
  44. await simulacionController.SimulaAsync(nextInQueue);
  45. ////Se actualiza el estado del item ya que ha terminado su ejecucion
  46. //await serviceQueueController.SetTerminado(nextInQueue.Id);
  47. }
  48. }
  49. await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
  50. }
  51. }
  52. public void LogInfo(string msg)
  53. {
  54. _logger.LogInformation(msg);
  55. }
  56. }
  57. }