feat: 增加文件检查类

This commit is contained in:
Argo Zhang 2019-08-13 14:39:35 +08:00
parent a191a9e383
commit dd8aaa633c
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
3 changed files with 46 additions and 1 deletions

View File

@ -9,7 +9,7 @@ using Task = System.Threading.Tasks.Task;
namespace Bootstrap.Admin.HealthChecks namespace Bootstrap.Admin.HealthChecks
{ {
/// <summary> /// <summary>
/// 数据库检查 /// 数据库检查
/// </summary> /// </summary>
public class DBHealthCheck : IHealthCheck public class DBHealthCheck : IHealthCheck
{ {

View File

@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Bootstrap.Admin.HealthChecks
{
/// <summary>
/// 文件健康检查类
/// </summary>
public class FileHealCheck : IHealthCheck
{
private readonly IHostingEnvironment _env;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="env"></param>
public FileHealCheck(IHostingEnvironment env)
{
_env = env;
}
/// <summary>
/// 异步检查方法
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var file = _env.IsDevelopment() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Longbow.lic") : Path.Combine(_env.ContentRootPath, "Longbow.lic");
var data = new Dictionary<string, object>();
data.Add("ContentRootPath", _env.ContentRootPath);
data.Add("WebRootPath", _env.WebRootPath);
data.Add("ApplicationName", _env.ApplicationName);
data.Add("EnvironmentName", _env.EnvironmentName);
data.Add("CheckFile", file);
return Task.FromResult(File.Exists(file) ? HealthCheckResult.Healthy("Ok", data) : HealthCheckResult.Unhealthy($"Missing file {file}", null, data));
}
}
}

View File

@ -15,6 +15,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder)
{ {
builder.AddCheck<DBHealthCheck>("db"); builder.AddCheck<DBHealthCheck>("db");
builder.AddCheck<FileHealCheck>("file");
return builder; return builder;
} }
} }