1234567891011121314151617181920212223242526272829303132333435 |
- namespace BankOperationsUpdate.WorkerService;
- public class Worker : BackgroundService
- {
- private readonly ILogger<Worker> _logger;
- private TimeSpan refreshInterval;
- private readonly UpdateOperations _updateOperations;
- public Worker(ILogger<Worker> logger, UpdateOperations updateOperations, IConfiguration config)
- {
- _logger = logger;
- _updateOperations = updateOperations;
- refreshInterval = Int32.TryParse(config.GetSection("RefreshInterval").Value, out int minutes) ? TimeSpan.FromMinutes(5) : TimeSpan.FromMinutes(minutes);
- }
-
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- _updateOperations.WhenStart();
- while (!stoppingToken.IsCancellationRequested) {
- try {
- _updateOperations.Execute();
- }
- catch (Exception ex) {
- _logger.LogError("Message: {message} \n Source : {source} \n StackTrace: {stackTrace}", ex.Message, ex.Source, ex.StackTrace);
-
- }
-
- _logger.LogInformation("end of period");
- await Task.Delay(refreshInterval, stoppingToken);
- }
- }
- }
|