Knocks/BackEnd/Knoks.Api/Logic/Managers/RefreshExchangesHostedServi...

62 lines
2.0 KiB
C#

using Knoks.Core.Data.Interfaces;
using Knoks.Core.Entities;
using Knoks.Core.Logic.Interfaces;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;
using System.Collections.ObjectModel;
using Knoks.Api.Logic.Interfaces;
using Knoks.Api.Data.Interfaces;
using Knoks.Api.Entities;
using Microsoft.Extensions.Hosting;
using System.Threading;
using Knoks.Framework.Services;
namespace Knoks.Api.Logic.Managers
{
public class RefreshExchangesHostedService : IHostedService, IDisposable
{
private readonly ILogger _logger;
private Timer _timer;
private readonly IExchangeManager _exchangeManager;
private readonly int _exchangesInfoUpdateHours;
public RefreshExchangesHostedService(CommonSettings settings, ILogger<RefreshExchangesHostedService> logger, IExchangeManager exchangeManager)
{
_logger = logger;
_exchangeManager = exchangeManager;
_exchangesInfoUpdateHours = settings.ExchangesInfoUpdateHours;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"{nameof(RefreshExchangesHostedService)} is starting.");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromHours(_exchangesInfoUpdateHours));
return Task.CompletedTask;
}
private void DoWork(object state)
{
_exchangeManager.UpdateExchanges();
_logger.LogInformation($"{nameof(RefreshExchangesHostedService)} is working.");
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"{nameof(RefreshExchangesHostedService)} is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}