2019-08-13 14:19:27 +08:00
|
|
|
|
using Bootstrap.DataAccess;
|
|
|
|
|
using Bootstrap.Security;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2019-08-13 20:23:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
2019-08-13 14:19:27 +08:00
|
|
|
|
using System.Linq;
|
2019-08-13 13:39:18 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-08-13 14:19:27 +08:00
|
|
|
|
using Task = System.Threading.Tasks.Task;
|
2019-08-13 13:39:18 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.Admin.HealthChecks
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-08-13 14:39:35 +08:00
|
|
|
|
/// 数据库检查类
|
2019-08-13 13:39:18 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class DBHealthCheck : IHealthCheck
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步检查方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
2019-08-13 14:19:27 +08:00
|
|
|
|
using (var db = DbManager.Create())
|
|
|
|
|
{
|
|
|
|
|
var connStr = db.ConnectionString;
|
|
|
|
|
var dicts = db.Fetch<BootstrapDict>("Select * from Dicts");
|
2019-08-13 20:23:28 +08:00
|
|
|
|
var data = new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{ "ConnectionString", connStr },
|
|
|
|
|
{ "DbType", db.Provider.GetType().Name },
|
|
|
|
|
{ "Dicts", dicts.Count }
|
|
|
|
|
};
|
|
|
|
|
return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok", data)) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB"));
|
2019-08-13 14:19:27 +08:00
|
|
|
|
}
|
2019-08-13 13:39:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|