adding cache for spills
This commit is contained in:
parent
b9d76ca2f4
commit
6ce1935dea
@ -21,23 +21,7 @@
|
|||||||
<SpillsTable Job=@job />
|
<SpillsTable Job=@job />
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private SouthernWaterApiJob? job;
|
private SouthernWaterApiJob? job => cache.CurrentSouthernWaterApiJob;
|
||||||
[Inject] private IMongoDatabase database { get; set; }
|
[Inject] private SpillCache cache { get; set; }
|
||||||
// private bool showIds;
|
// private bool showIds;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
|
||||||
{
|
|
||||||
job = database.GetCollection<SouthernWaterApiJob>(Static.JobCollectionName)
|
|
||||||
.AsQueryable()
|
|
||||||
.OrderByDescending(j => j.EndTime)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (job is not null)
|
|
||||||
{
|
|
||||||
job.Spills = database.GetCollection<Spill>(Static.SpillCollectionName)
|
|
||||||
.AsQueryable()
|
|
||||||
.Where(s => s.JobId == job._id)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -25,24 +25,8 @@
|
|||||||
<SpillsCalendar Job="@job" GenuineOnly="@genuineOnly" />
|
<SpillsCalendar Job="@job" GenuineOnly="@genuineOnly" />
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private SouthernWaterApiJob? job;
|
private SouthernWaterApiJob? job => cache.CurrentSouthernWaterApiJob;
|
||||||
[Inject] private IMongoDatabase database { get; set; }
|
[Inject] private SpillCache cache { get; set; }
|
||||||
// private bool showIds;
|
// private bool showIds;
|
||||||
private bool genuineOnly = true;
|
private bool genuineOnly = true;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
|
||||||
{
|
|
||||||
job = database.GetCollection<SouthernWaterApiJob>(Static.JobCollectionName)
|
|
||||||
.AsQueryable()
|
|
||||||
.OrderByDescending(j => j.EndTime)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (job is not null)
|
|
||||||
{
|
|
||||||
job.Spills = database.GetCollection<Spill>(Static.SpillCollectionName)
|
|
||||||
.AsQueryable()
|
|
||||||
.Where(s => s.JobId == job._id)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -60,6 +60,22 @@ builder.Services.AddQuartz(q =>
|
|||||||
.WithCronSchedule(builder.Configuration.GetSection("SouthernWater").GetValue<string>("Cron") ?? "0 0 8 * * ?")
|
.WithCronSchedule(builder.Configuration.GetSection("SouthernWater").GetValue<string>("Cron") ?? "0 0 8 * * ?")
|
||||||
.WithDescription("Periodic trigger for Southern Water API pulling")
|
.WithDescription("Periodic trigger for Southern Water API pulling")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var cacheKey = new JobKey("cache-refresh", "cache");
|
||||||
|
|
||||||
|
q.AddJob<CacheReloadJob>(j => j
|
||||||
|
.WithDescription("Refresh caches")
|
||||||
|
.WithIdentity(cacheKey)
|
||||||
|
.UsingJobData("IsFull", false)
|
||||||
|
);
|
||||||
|
|
||||||
|
q.AddTrigger(t => t
|
||||||
|
.WithIdentity("cache-refresh-trigger")
|
||||||
|
.ForJob(cacheKey)
|
||||||
|
.StartNow()
|
||||||
|
.WithCronSchedule(builder.Configuration.GetSection("Cache").GetValue<string>("Cron") ?? "0 0 8 * * ?")
|
||||||
|
.WithDescription("Periodic trigger for cache refreshing")
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// ASP.NET Core hosting
|
// ASP.NET Core hosting
|
||||||
@ -74,6 +90,10 @@ builder.Services.AddSingleton<SouthernWaterApi>();
|
|||||||
builder.Services.AddScoped<SouthernWaterApiJobRunner, SouthernWaterApiJobRunnerPersisting>();
|
builder.Services.AddScoped<SouthernWaterApiJobRunner, SouthernWaterApiJobRunnerPersisting>();
|
||||||
builder.Services.AddTransient<SouthernWaterJob>();
|
builder.Services.AddTransient<SouthernWaterJob>();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<SpillCache>();
|
||||||
|
builder.Services.AddSingleton<SouthernWaterSpillCache>();
|
||||||
|
builder.Services.AddHostedService<LoadCacheOnStart>();
|
||||||
|
|
||||||
builder.Services.AddRadzenComponents();
|
builder.Services.AddRadzenComponents();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
@ -11,5 +11,8 @@
|
|||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"SouthernWater": {
|
"SouthernWater": {
|
||||||
"Cron": "0 26 20 * * ?"
|
"Cron": "0 26 20 * * ?"
|
||||||
|
},
|
||||||
|
"Cache": {
|
||||||
|
"Cron": "0 */3 * * * ?"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
Overflow/CacheReloadJob.cs
Normal file
15
Overflow/CacheReloadJob.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace Overflow;
|
||||||
|
|
||||||
|
[DisallowConcurrentExecution]
|
||||||
|
public class CacheReloadJob(SpillCache cache, ILogger<CacheReloadJob> logger): IJob
|
||||||
|
{
|
||||||
|
public Task Execute(IJobExecutionContext context)
|
||||||
|
{
|
||||||
|
logger.LogDebug("Refreshing caches");
|
||||||
|
cache.Refresh();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,8 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
|
||||||
<PackageReference Include="Quartz" Version="3.9.0" />
|
<PackageReference Include="Quartz" Version="3.9.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
44
Overflow/SouthernWater/SouthernWaterSpillCache.cs
Normal file
44
Overflow/SouthernWater/SouthernWaterSpillCache.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace Overflow.SouthernWater;
|
||||||
|
|
||||||
|
public class SouthernWaterSpillCache(IServiceProvider serviceProvider, ILogger<SouthernWaterSpillCache> logger)
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
|
||||||
|
private SouthernWaterApiJob _currentJob;
|
||||||
|
|
||||||
|
public SouthernWaterApiJob CurrentJob => _currentJob;
|
||||||
|
|
||||||
|
public void ReloadJob()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Refreshing Southern Water Spills");
|
||||||
|
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
|
||||||
|
var database = scope.ServiceProvider.GetRequiredService<IMongoDatabase>();
|
||||||
|
|
||||||
|
var job = database.GetCollection<SouthernWaterApiJob>(Static.JobCollectionName)
|
||||||
|
.AsQueryable()
|
||||||
|
.OrderByDescending(j => j.EndTime)
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
if (job is not null)
|
||||||
|
{
|
||||||
|
job.Spills = database.GetCollection<Spill>(Static.SpillCollectionName)
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(s => s.JobId == job._id)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
_currentJob = job;
|
||||||
|
|
||||||
|
_logger.LogInformation("Southern Water Spills cache refreshed");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No Southern Water Spills returned");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
Overflow/SpillCache.cs
Normal file
35
Overflow/SpillCache.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Overflow.SouthernWater;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace Overflow;
|
||||||
|
|
||||||
|
public class SpillCache(SouthernWaterSpillCache southernWaterSpillCache, ILogger<SpillCache> logger)
|
||||||
|
{
|
||||||
|
public SouthernWaterApiJob CurrentSouthernWaterApiJob => southernWaterSpillCache.CurrentJob;
|
||||||
|
|
||||||
|
public void Refresh()
|
||||||
|
{
|
||||||
|
logger.LogDebug("Refreshing caches");
|
||||||
|
|
||||||
|
southernWaterSpillCache.ReloadJob();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoadCacheOnStart(ISchedulerFactory scheduler, SpillCache cache, ILogger<LoadCacheOnStart> logger) : IHostedService
|
||||||
|
{
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Loading caches for startup");
|
||||||
|
|
||||||
|
await (await scheduler.GetScheduler()).TriggerJob(new JobKey("cache-refresh", "cache"));
|
||||||
|
// cache.Refresh();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user