fix 启动拉取数据,避免dblock
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using Dlhis.Middle.BackgroundServices;
|
||||
using Dlhis.Middle.Entities;
|
||||
using Dlhis.Middle.Services;
|
||||
using Kurisu.AspNetCore;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Startup;
|
||||
using Microsoft.IdentityModel.Logging;
|
||||
|
||||
@@ -30,4 +33,26 @@ public class Startup : DefaultStartup
|
||||
services.AddRemoteCall(App.ActiveTypes);
|
||||
services.AddMemoryCache();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
base.Configure(app, env);
|
||||
|
||||
var healthCheckService = app.ApplicationServices.GetService<IHealthCheckService>();
|
||||
if (healthCheckService.DoHealthCheckAsync().GetAwaiter().GetResult())
|
||||
{
|
||||
using var scope = app.ApplicationServices.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>();
|
||||
ds.InitAsync(factories).Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user