Worker.cs 899 B

1234567891011121314151617181920212223242526272829303132
  1. namespace BankOperationsUpdate.WorkerService;
  2. public class Worker : BackgroundService
  3. {
  4. private readonly ILogger<Worker> _logger;
  5. private TimeSpan refreshInterval;
  6. private readonly UpdateOperations _updateOperations;
  7. public Worker(ILogger<Worker> logger, UpdateOperations updateOperations, IConfiguration config)
  8. {
  9. _logger = logger;
  10. _updateOperations = updateOperations;
  11. refreshInterval = Int32.TryParse(config.GetSection("RefreshInterval").Value, out int minutes) ? TimeSpan.FromMinutes(5) : TimeSpan.FromMinutes(minutes);
  12. }
  13. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  14. {
  15. while (!stoppingToken.IsCancellationRequested) {
  16. try {
  17. _updateOperations.Execute();
  18. }
  19. catch (Exception ex) {
  20. _logger.LogError(ex.Message);
  21. }
  22. _logger.LogInformation("end of period");
  23. await Task.Delay(refreshInterval, stoppingToken);
  24. }
  25. }
  26. }