fix 优化健康检查,数据同步

This commit is contained in:
2024-11-14 15:32:52 +08:00
parent ecf98fc1d3
commit c6593f93bc
8 changed files with 47 additions and 45 deletions

View File

@@ -1,12 +1,8 @@
using System.Net.Http.Headers;
using System.Text;
using Dlhis.Middle.Apis; using Dlhis.Middle.Apis;
using Dlhis.Middle.Entities; using Dlhis.Middle.Entities;
using Dlhis.Middle.Response;
using Dlhis.Middle.Services; using Dlhis.Middle.Services;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.UnifyResultAndValidation; using Kurisu.AspNetCore.UnifyResultAndValidation;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Middle.BackgroundServices; namespace Dlhis.Middle.BackgroundServices;
@@ -102,23 +98,30 @@ public class BalanceRecordUploadBackgroundService : BackgroundService
} }
if (now is { Hour: 4, Minute: 1 } try
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{ {
await ds.InitAsync(factories); if (now is { Hour: 4, Minute: 1 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{
await ds.InitAsync(factories);
}
else if (now is { Hour: 15, Minute: 1 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{
await ds.InitAsync(factories);
}
else if (now is { Hour: 20, Minute: 1 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{
await ds.InitAsync(factories);
}
} }
else if (now is { Hour: 15, Minute: 1 } catch (Exception e)
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{ {
await ds.InitAsync(factories); _logger.LogError(e, "定时同步离线数据失败");
}
else if (now is { Hour: 20, Minute: 1 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
{
await ds.InitAsync(factories);
} }
} }

View File

@@ -5,17 +5,31 @@ namespace Dlhis.Middle.BackgroundServices;
public class HealthCheckBackgroundService : BackgroundService public class HealthCheckBackgroundService : BackgroundService
{ {
private readonly IHealthCheckService _healthCheckService; private readonly IHealthCheckService _healthCheckService;
private readonly IServiceProvider _serviceProvider;
private bool _init;
public HealthCheckBackgroundService(IHealthCheckService healthCheckService) public HealthCheckBackgroundService(IHealthCheckService healthCheckService, IServiceProvider serviceProvider)
{ {
_healthCheckService = healthCheckService; _healthCheckService = healthCheckService;
_serviceProvider = serviceProvider;
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
await _healthCheckService.DoHealthCheckAsync(); if (await _healthCheckService.DoHealthCheckAsync())
{
if (!_init)
{
using var scope = _serviceProvider.CreateScope();
var factories = scope.ServiceProvider.GetService<ISettingService>().GetFactories();
var ds = scope.ServiceProvider.GetService<IDataDownloadService>();
await ds.InitAsync(factories);
}
}
_init = true;
} }
} }
} }

View File

@@ -1,7 +1,4 @@
using Dlhis.Entity.Basic.Entity; using Dlhis.Middle.Entities;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Device.Entities;
using Dlhis.Middle.Entities;
using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Entity; using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes; using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes;
@@ -141,7 +138,7 @@ public class SqlSugarPack : BaseAppPack
{ {
using var scope = app.ApplicationServices.CreateScope(); using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetService<IDbContext>(); var db = scope.ServiceProvider.GetService<IDbContext>();
db.Client.DbMaintenance.CreateDatabase(); db.Client.DbMaintenance.CreateDatabase();
db.Client.CodeFirst.InitTables<LocalBalanceRecordEntity, LocalGainfoEntity, LocalGainfoMealEntity, LocalBusinessPlaceEntity>(); db.Client.CodeFirst.InitTables<LocalBalanceRecordEntity, LocalGainfoEntity, LocalGainfoMealEntity, LocalBusinessPlaceEntity>();
db.Client.CodeFirst.InitTables<LocalBusinessShiftEntity, LocalDeviceRegistryEntity, LocalDeviceEntity, LocalBusinessPlaceShiftRelationshipEntity, LocalBusinessShiftMealRelationshipEntity>(); db.Client.CodeFirst.InitTables<LocalBusinessShiftEntity, LocalDeviceRegistryEntity, LocalDeviceEntity, LocalBusinessPlaceShiftRelationshipEntity, LocalBusinessShiftMealRelationshipEntity>();

View File

@@ -1,5 +1,4 @@
using Dlhis.Middle.BackgroundServices; using Dlhis.Middle.BackgroundServices;
using Dlhis.Middle.Services;
using Kurisu.AspNetCore; using Kurisu.AspNetCore;
using Kurisu.AspNetCore.Startup; using Kurisu.AspNetCore.Startup;
using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Logging;
@@ -31,18 +30,4 @@ public class Startup : DefaultStartup
services.AddRemoteCall(App.ActiveTypes); services.AddRemoteCall(App.ActiveTypes);
services.AddMemoryCache(); services.AddMemoryCache();
} }
/// <inheritdoc />
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
base.Configure(app, env);
if (Configuration.GetSection("Download").Value == "1")
{
using var scope = app.ApplicationServices.CreateScope();
var factories = scope.ServiceProvider.GetService<ISettingService>().GetFactories();
var ds = scope.ServiceProvider.GetService<IDataDownloadService>();
ds.InitAsync(factories).Wait();
}
}
} }

View File

@@ -4,5 +4,5 @@ public interface IHealthCheckService : ISingletonDependency
{ {
bool GetIsOffline(); bool GetIsOffline();
Task DoHealthCheckAsync(); Task<bool> DoHealthCheckAsync();
} }

View File

@@ -28,7 +28,7 @@ public class HealthCheckService : IHealthCheckService
return _isOffline; return _isOffline;
} }
public async Task DoHealthCheckAsync() public async Task<bool> DoHealthCheckAsync()
{ {
try try
{ {
@@ -50,12 +50,13 @@ public class HealthCheckService : IHealthCheckService
_isBegin = true; _isBegin = true;
} }
await Task.Delay(_attempted * 1000);
if (_isOffline == false) if (_isOffline == false)
{ {
_logger.LogInformation("Health"); _logger.LogInformation("Health");
return true;
} }
await Task.Delay(_attempted * 1000);
} }
else else
{ {
@@ -69,5 +70,7 @@ public class HealthCheckService : IHealthCheckService
_isOffline = true; _isOffline = true;
await Task.Delay(5000); await Task.Delay(5000);
} }
return false;
} }
} }

View File

@@ -7,5 +7,5 @@
}, },
"BaseUrl": "https://yikatong.isawesome.cn", "BaseUrl": "https://yikatong.isawesome.cn",
"Factory": "1234", "Factory": "1234",
"Download":1 "InternalIP":""
} }

Binary file not shown.