Break it out to individual files and initial cache service
This commit is contained in:
parent
2fa8f32f44
commit
e974635cfb
|
|
@ -1,63 +0,0 @@
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using AS1024.GeoFeed.GeoFeedBuilder;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using AS1024.GeoFeed.Interfaces;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.GeoFeedCacheService
|
|
||||||
{
|
|
||||||
public class GeoFeedCacheService : IHostedService
|
|
||||||
{
|
|
||||||
CancellationToken _cancellationToken;
|
|
||||||
private readonly ILogger<GeoFeedCacheService> logger;
|
|
||||||
private readonly IGeoFeedProvider feedProvider;
|
|
||||||
|
|
||||||
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
|
||||||
IGeoFeedProvider feedProvider)
|
|
||||||
{
|
|
||||||
this.logger = logger;
|
|
||||||
this.feedProvider = feedProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
_ = StartPerioidicSync();
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> StartPerioidicSync(){
|
|
||||||
List<GeoFeedCacheEntry> geoFeedCacheEntry = [];
|
|
||||||
while (!_cancellationToken.IsCancellationRequested) {
|
|
||||||
await Task.Delay(TimeSpan.FromMinutes(30));
|
|
||||||
logger.LogInformation("Running on disk fallback cache process...");
|
|
||||||
var results = await feedProvider.GetGeoFeedData();
|
|
||||||
|
|
||||||
results.ForEach(x => {
|
|
||||||
geoFeedCacheEntry.Add(new (){
|
|
||||||
Prefix = x.Prefix,
|
|
||||||
GeolocCity = x.GeolocCity,
|
|
||||||
GeolocCountry = x.GeolocCountry,
|
|
||||||
GeolocHasLocation = x.GeolocHasLocation,
|
|
||||||
GeolocPostalCode = x.GeolocPostalCode,
|
|
||||||
GeolocRegion = x.GeolocRegion
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class GeoFeedCacheEntry : IPGeoFeed {
|
|
||||||
[Key]
|
|
||||||
public int Id { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class GeoFeedCacheDbContext : DbContext {
|
|
||||||
public virtual DbSet<GeoFeedCacheEntry> GeoFeedCacheEntries {get;set;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||||
|
{
|
||||||
|
public class GeoFeedCacheDbContext : DbContext
|
||||||
|
{
|
||||||
|
public GeoFeedCacheDbContext(DbContextOptions options)
|
||||||
|
: base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual DbSet<GeoFeedCacheEntry> GeoFeedCacheEntries { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
using AS1024.GeoFeed.Models;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||||
|
{
|
||||||
|
public class GeoFeedCacheEntry : IPGeoFeed
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
using AS1024.GeoFeed.Interfaces;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||||
|
{
|
||||||
|
public class GeoFeedCacheService : IHostedService
|
||||||
|
{
|
||||||
|
CancellationToken _cancellationToken;
|
||||||
|
private readonly ILogger<GeoFeedCacheService> logger;
|
||||||
|
private readonly IGeoFeedProvider feedProvider;
|
||||||
|
private readonly GeoFeedCacheDbContext dbContext;
|
||||||
|
|
||||||
|
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
||||||
|
IGeoFeedProvider feedProvider,
|
||||||
|
GeoFeedCacheDbContext dbContext)
|
||||||
|
{
|
||||||
|
this.logger = logger;
|
||||||
|
this.feedProvider = feedProvider;
|
||||||
|
this.dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_ = StartPerioidicSync();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> StartPerioidicSync()
|
||||||
|
{
|
||||||
|
if (dbContext.Database.GetPendingMigrations().Any())
|
||||||
|
await dbContext.Database.MigrateAsync();
|
||||||
|
|
||||||
|
List<GeoFeedCacheEntry> geoFeedCacheEntry = [];
|
||||||
|
while (!_cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
await Task.Delay(TimeSpan.FromMinutes(30));
|
||||||
|
logger.LogInformation("Running on disk fallback cache process...");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var results = await feedProvider.GetGeoFeedData();
|
||||||
|
|
||||||
|
results.ForEach(x =>
|
||||||
|
{
|
||||||
|
geoFeedCacheEntry.Add(new()
|
||||||
|
{
|
||||||
|
Prefix = x.Prefix,
|
||||||
|
GeolocCity = x.GeolocCity,
|
||||||
|
GeolocCountry = x.GeolocCountry,
|
||||||
|
GeolocHasLocation = x.GeolocHasLocation,
|
||||||
|
GeolocPostalCode = x.GeolocPostalCode,
|
||||||
|
GeolocRegion = x.GeolocRegion
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (dbContext.GeoFeedCacheEntries.Any())
|
||||||
|
{
|
||||||
|
dbContext.GeoFeedCacheEntries.RemoveRange(dbContext.GeoFeedCacheEntries.ToArray());
|
||||||
|
}
|
||||||
|
await dbContext.AddRangeAsync(geoFeedCacheEntry);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Design;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||||
|
{
|
||||||
|
public class GeoFeedDesignTimeMigration : IDesignTimeDbContextFactory<GeoFeedCacheDbContext>
|
||||||
|
{
|
||||||
|
public GeoFeedCacheDbContext CreateDbContext(string[] args)
|
||||||
|
{
|
||||||
|
var builder = new DbContextOptionsBuilder<GeoFeedCacheDbContext>();
|
||||||
|
builder.UseSqlite("Data Source=migratedb.db");
|
||||||
|
return new GeoFeedCacheDbContext(builder.Options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue