PalacePal/Pal.Client/Floors/Tasks/DbTask.cs

42 lines
1.2 KiB
C#
Raw Normal View History

2023-02-24 10:18:03 +00:00
using System;
using System.Threading.Tasks;
2023-02-18 20:12:36 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2023-02-18 20:12:36 +00:00
using Pal.Client.Database;
namespace Pal.Client.Floors.Tasks
{
internal abstract class DbTask<T>
where T : DbTask<T>
2023-02-18 20:12:36 +00:00
{
private readonly IServiceScopeFactory _serviceScopeFactory;
protected DbTask(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
public void Start()
{
Task.Run(() =>
{
2023-02-24 10:18:03 +00:00
try
{
using var scope = _serviceScopeFactory.CreateScope();
ILogger<T> logger = scope.ServiceProvider.GetRequiredService<ILogger<T>>();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
2023-02-18 20:12:36 +00:00
2023-02-24 10:18:03 +00:00
Run(dbContext, logger);
}
catch (Exception e)
{
DependencyInjectionContext.LoggerProvider.CreateLogger<DbTask<T>>()
.LogError(e, "Failed to run DbTask");
}
2023-02-18 20:12:36 +00:00
});
}
protected abstract void Run(PalClientContext dbContext, ILogger<T> logger);
2023-02-18 20:12:36 +00:00
}
}