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.Entities;
using Dlhis.Middle.Response;
using Dlhis.Middle.Services;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.UnifyResultAndValidation;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Middle.BackgroundServices;
@@ -102,23 +98,30 @@ public class BalanceRecordUploadBackgroundService : BackgroundService
}
if (now is { Hour: 4, Minute: 1 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
try
{
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 }
&& !_healthCheckService.GetIsOffline()
&& !await db.Queryable<LocalBalanceRecordEntity>().Where(x => !x.IsSync).AnyAsync())
catch (Exception e)
{
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);
_logger.LogError(e, "定时同步离线数据失败");
}
}

View File

@@ -5,17 +5,31 @@ namespace Dlhis.Middle.BackgroundServices;
public class HealthCheckBackgroundService : BackgroundService
{
private readonly IHealthCheckService _healthCheckService;
private readonly IServiceProvider _serviceProvider;
private bool _init;
public HealthCheckBackgroundService(IHealthCheckService healthCheckService)
public HealthCheckBackgroundService(IHealthCheckService healthCheckService, IServiceProvider serviceProvider)
{
_healthCheckService = healthCheckService;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
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.Entity.Canteen.Entities;
using Dlhis.Entity.Device.Entities;
using Dlhis.Middle.Entities;
using Dlhis.Middle.Entities;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes;

View File

@@ -1,5 +1,4 @@
using Dlhis.Middle.BackgroundServices;
using Dlhis.Middle.Services;
using Kurisu.AspNetCore;
using Kurisu.AspNetCore.Startup;
using Microsoft.IdentityModel.Logging;
@@ -31,18 +30,4 @@ public class Startup : DefaultStartup
services.AddRemoteCall(App.ActiveTypes);
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();
Task DoHealthCheckAsync();
Task<bool> DoHealthCheckAsync();
}

View File

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

View File

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

Binary file not shown.