fix 启动拉取数据,避免dblock

This commit is contained in:
2024-11-14 17:53:20 +08:00
parent 2c6ac44782
commit 5033d895ea
2 changed files with 28 additions and 25 deletions

View File

@@ -7,39 +7,17 @@ namespace Dlhis.Middle.BackgroundServices;
public class HealthCheckBackgroundService : BackgroundService
{
private readonly IHealthCheckService _healthCheckService;
private readonly IServiceProvider _serviceProvider;
private bool _init;
public HealthCheckBackgroundService(IHealthCheckService healthCheckService, IServiceProvider serviceProvider)
public HealthCheckBackgroundService(IHealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (await _healthCheckService.DoHealthCheckAsync())
{
if (!_init)
{
using var scope = _serviceProvider.CreateScope();
var db = scope.ServiceProvider.GetService<IDbContext>();
db.Client.DbMaintenance.CreateDatabase();
db.Client.CodeFirst.InitTables<LocalBalanceRecordEntity, LocalGainfoEntity, LocalGainfoMealEntity, LocalBusinessPlaceEntity>();
db.Client.CodeFirst.InitTables<LocalBusinessShiftEntity, LocalDeviceRegistryEntity, LocalDeviceEntity, LocalBusinessPlaceShiftRelationshipEntity, LocalBusinessShiftMealRelationshipEntity>();
db.Client.CodeFirst.InitTables<LocalCanteenSettingEntity, LocalDepartmentEntity>();
var factories = scope.ServiceProvider.GetService<ISettingService>().GetFactories();
var ds = scope.ServiceProvider.GetService<IDataDownloadService>();
await ds.InitAsync(factories);
}
}
_init = true;
await _healthCheckService.DoHealthCheckAsync();
}
}
}