diff --git a/AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs b/AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs index 60127da..ac5a52b 100644 --- a/AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs +++ b/AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs @@ -6,7 +6,6 @@ namespace AS1024.GeoFeed.GeoFeedLocalCache { public class GeoFeedCacheService : IHostedService { - CancellationToken _cancellationToken; private readonly ILogger logger; private readonly IGeoFeedProvider feedProvider; private readonly IHost host; @@ -22,7 +21,7 @@ namespace AS1024.GeoFeed.GeoFeedLocalCache public Task StartAsync(CancellationToken cancellationToken) { - _ = StartPerioidicSync(); + _ = StartPerioidicSync(cancellationToken); return Task.CompletedTask; } @@ -31,14 +30,13 @@ namespace AS1024.GeoFeed.GeoFeedLocalCache return Task.CompletedTask; } - public async Task StartPerioidicSync() + public async Task StartPerioidicSync(CancellationToken Token) { await DBContextMigrate(); List geoFeedCacheEntry = []; - while (!_cancellationToken.IsCancellationRequested) + while (!Token.IsCancellationRequested) { - await Task.Delay(TimeSpan.FromMinutes(30)); logger.LogInformation("Running on disk fallback cache process..."); try { @@ -63,13 +61,14 @@ namespace AS1024.GeoFeed.GeoFeedLocalCache { dbContext.GeoFeedCacheEntries.RemoveRange(dbContext.GeoFeedCacheEntries.ToArray()); } - await dbContext.AddRangeAsync(geoFeedCacheEntry); - await dbContext.SaveChangesAsync(); + await dbContext.AddRangeAsync(geoFeedCacheEntry, Token); + await dbContext.SaveChangesAsync(Token); } catch (Exception ex) { logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry..."); } + await Task.Delay(TimeSpan.FromMinutes(30)); } return false; @@ -79,11 +78,13 @@ namespace AS1024.GeoFeed.GeoFeedLocalCache { using IServiceScope scope = host.Services.CreateScope(); using GeoFeedCacheDbContext? dbContext = - host.Services.GetService(); - if (dbContext.Database.GetPendingMigrations().Any()) - { + scope.ServiceProvider.GetService(); +#pragma warning disable CS8602 // Dereference of a possibly null reference. + if (dbContext.Database.GetPendingMigrations().Any()) { await dbContext.Database.MigrateAsync(); } +#pragma warning restore CS8602 // Dereference of a possibly null reference. + } } } \ No newline at end of file