Worker.cs 747 B

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