rdt-client/server/RdtClient.Service/BackgroundServices/Startup.cs
omgbeez 9ea2219df7 fix(service): Clear SQLite connection pools on service stop
Should fix manual delete of Regression tests which create a temp SQLite DB for their tests.
2026-04-28 14:31:29 -04:00

48 lines
1.7 KiB
C#

using System.Reflection;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RdtClient.Data.Data;
using RdtClient.Service.Services;
namespace RdtClient.Service.BackgroundServices;
public class Startup(IServiceProvider serviceProvider) : IHostedService
{
public static Boolean Ready { get; private set; }
public async Task StartAsync(CancellationToken cancellationToken)
{
var version = Assembly.GetEntryAssembly()?.GetName().Version;
using var scope = serviceProvider.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Startup>>();
logger.LogWarning("Starting host on version {version}", version);
var dbContext = scope.ServiceProvider.GetRequiredService<DataContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
// Configure SQLite for better concurrency and performance
await dbContext.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL;", cancellationToken);
await dbContext.Database.ExecuteSqlRawAsync("PRAGMA synchronous=NORMAL;", cancellationToken);
await dbContext.Database.ExecuteSqlRawAsync("PRAGMA busy_timeout=5000;", cancellationToken);
await dbContext.Database.ExecuteSqlRawAsync("PRAGMA cache_size=-64000;", cancellationToken);
var settings = scope.ServiceProvider.GetRequiredService<Settings>();
await settings.Seed();
await settings.ResetCache();
Ready = true;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Ready = false;
SqliteConnection.ClearAllPools();
return Task.CompletedTask;
}
}