using System.Net.Sockets; using JiShe.CollectBus.Cassandra; using JiShe.CollectBus.IoTDB.Interface; using JiShe.CollectBus.IoTDB.Options; using JiShe.CollectBus.IoTDB.Provider; using Microsoft.Extensions.Diagnostics.HealthChecks; namespace JiShe.CollectBus.Host.HealthChecks { /// /// IoTDBHealthCheck /// /// public class IoTdbHealthCheck : IHealthCheck { private readonly IConfiguration _configuration; /// /// Initializes a new instance of the class. /// /// The configuration. public IoTdbHealthCheck(IConfiguration configuration) { _configuration = configuration; } /// /// Runs the health check, returning the status of the component being checked. /// /// A context object associated with the current execution. /// A that can be used to cancel the health check. /// /// A that completes when the health check has finished, yielding the status of the component being checked. /// public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { var ioTDbOptions = new IoTDbOptions(); _configuration.GetSection("IoTDBOptions").Bind(ioTDbOptions); var pool = new SessionPoolAdapter(ioTDbOptions); await pool.OpenAsync(); return HealthCheckResult.Healthy($"IoTDB is healthy."); } catch (Exception ex) { return new HealthCheckResult(context.Registration.FailureStatus, $"IoTDB不健康: {ex.Message}", ex); } } } }