using System; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Pal.Client.Database; namespace Pal.Client.Floors.Tasks; internal abstract class DbTask where T : DbTask { private readonly IServiceScopeFactory _serviceScopeFactory; protected DbTask(IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; } public void Start() { Task.Run(() => { try { using var scope = _serviceScopeFactory.CreateScope(); ILogger logger = scope.ServiceProvider.GetRequiredService>(); try { using var dbContext = scope.ServiceProvider.GetRequiredService(); Run(dbContext, logger); } catch (Exception e) { logger.LogError(e, "Failed to run DbTask"); } } catch (Exception) { // nothing we can do here but catch it, if we don't we crash the game } }); } protected abstract void Run(PalClientContext dbContext, ILogger logger); }