fix 多钱包调整
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
using System.Net.Sockets;
|
||||
using Dlhis.Middle.Apis;
|
||||
using Dlhis.Middle.Request;
|
||||
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
||||
|
||||
namespace Dlhis.Middle.Services.Implements;
|
||||
|
||||
public class HealthCheckService : IHealthCheckService
|
||||
{
|
||||
private bool _isOffline;
|
||||
private readonly string _hostName;
|
||||
private readonly ILogger<HealthCheckService> _logger;
|
||||
private int _successRetry;
|
||||
private int _retry;
|
||||
private readonly ISettingService _settingService;
|
||||
private readonly ICommonApi _healthCheckApi;
|
||||
private int _attempted;
|
||||
|
||||
public HealthCheckService(ILogger<HealthCheckService> logger, IConfiguration configuration)
|
||||
private bool _isBegin;
|
||||
|
||||
public HealthCheckService(ILogger<HealthCheckService> logger,
|
||||
ISettingService settingService,
|
||||
ICommonApi healthCheckApi)
|
||||
{
|
||||
_logger = logger;
|
||||
_hostName = configuration.GetSection("HostName").Value;
|
||||
_settingService = settingService;
|
||||
_healthCheckApi = healthCheckApi;
|
||||
}
|
||||
|
||||
public bool GetIsOffline()
|
||||
@@ -23,44 +30,44 @@ public class HealthCheckService : IHealthCheckService
|
||||
|
||||
public async Task DoHealthCheckAsync()
|
||||
{
|
||||
var cancellationTokenSource = new CancellationTokenSource(5000);
|
||||
var tcpClient = new TcpClient();
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("tcp connecting {host} {port}", _hostName, 443);
|
||||
await tcpClient.ConnectAsync(_hostName, 443, cancellationTokenSource.Token);
|
||||
_retry++;
|
||||
_logger.LogDebug("retry {retry}", _retry);
|
||||
if (tcpClient.Connected)
|
||||
var factories = _settingService.GetFactories();
|
||||
|
||||
var response = await _healthCheckApi.CheckHealthAsync(new HealthCheckRequest { Factories = factories.ToList() });
|
||||
if (response.Code == ApiStateCode.Success)
|
||||
{
|
||||
_successRetry++;
|
||||
_logger.LogDebug("success connected {count}", _successRetry);
|
||||
if (_successRetry == 4)
|
||||
_attempted++;
|
||||
if (_attempted == 5)
|
||||
{
|
||||
_isOffline = false;
|
||||
_successRetry = 0;
|
||||
_retry = 0;
|
||||
_logger.LogInformation("current is {status}", "healthy");
|
||||
_attempted = 0;
|
||||
}
|
||||
else
|
||||
|
||||
if (!_isBegin)
|
||||
{
|
||||
_logger.LogDebug("wait next check in {waitTime}", _successRetry * 1000);
|
||||
await Task.Delay(_successRetry * 1000, cancellationTokenSource.Token);
|
||||
_isOffline = false;
|
||||
_isBegin = true;
|
||||
}
|
||||
|
||||
if (_isOffline == false)
|
||||
{
|
||||
_logger.LogInformation("Health");
|
||||
}
|
||||
|
||||
await Task.Delay(_attempted * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
_successRetry = 0;
|
||||
_isOffline = true;
|
||||
_logger.LogDebug("current is {status}", "unavailable");
|
||||
await Task.Delay(500);
|
||||
_logger.LogInformation("Error {message}", response.Msg);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("fail cause :{reason}", ex.Message);
|
||||
_logger.LogError("Failed cause :{reason}", ex.Message);
|
||||
_isOffline = true;
|
||||
await Task.Delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user