58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using AS1024.GeoFeed.Core.Interfaces;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AS1024.GeoFeed.Core.CacheService
|
|
{
|
|
public class GeoFeedCacheService : IHostedService
|
|
{
|
|
private readonly ILogger<GeoFeedCacheService> logger;
|
|
private readonly IGeoFeedProvider feedProvider;
|
|
private readonly IHost host;
|
|
|
|
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
|
IGeoFeedProvider feedProvider,
|
|
IHost host)
|
|
{
|
|
this.logger = logger;
|
|
this.feedProvider = feedProvider;
|
|
this.host = host;
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_ = StartPerioidicSync(cancellationToken);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task<bool> StartPerioidicSync(CancellationToken Token)
|
|
{
|
|
while (!Token.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
var scope = host.Services.CreateScope();
|
|
|
|
var persistentCacheProvider =
|
|
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
|
|
|
var results = await feedProvider.GetGeoFeedData();
|
|
await persistentCacheProvider.CacheGeoFeed(results);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry...");
|
|
}
|
|
await Task.Delay(TimeSpan.FromMinutes(30));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |