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;
|
2023-02-22 16:21:48 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-18 20:12:36 +00:00
|
|
|
|
using Pal.Client.Database;
|
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
namespace Pal.Client.Floors.Tasks;
|
|
|
|
|
|
|
|
|
|
internal abstract class DbTask<T>
|
|
|
|
|
where T : DbTask<T>
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
2023-02-18 20:12:36 +00:00
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
protected DbTask(IServiceScopeFactory serviceScopeFactory)
|
|
|
|
|
{
|
|
|
|
|
_serviceScopeFactory = serviceScopeFactory;
|
|
|
|
|
}
|
2023-02-18 20:12:36 +00:00
|
|
|
|
|
2023-03-30 20:01:43 +00:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
try
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-03-30 20:01:43 +00:00
|
|
|
|
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-03-30 20:01:43 +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
|
|
|
|
}
|
2023-03-30 20:01:43 +00:00
|
|
|
|
|
|
|
|
|
protected abstract void Run(PalClientContext dbContext, ILogger<T> logger);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
}
|