feat: 增加 Gitee 接口检查

This commit is contained in:
Argo Zhang 2019-08-13 15:55:55 +08:00
parent 9e82a5d272
commit c4aee7cc19
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Bootstrap.Admin.HealthChecks
{
/// <summary>
/// Gitee 接口检查器
/// </summary>
public class GiteeHttpHealthCheck : IHealthCheck
{
private readonly HttpClient _client;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="factory"></param>
/// <param name="accessor"></param>
public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor)
{
_client = factory.CreateClient();
_client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}/{accessor.HttpContext.Request.PathBase}");
}
/// <summary>
/// 异步检查方法
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var urls = new string[] { "Issues", "Pulls", "Releases", "Builds" };
var data = new Dictionary<string, object>();
var sw = Stopwatch.StartNew();
foreach (var url in urls)
{
sw.Restart();
await _client.GetStringAsync($"api/Gitee/{url}").ConfigureAwait(false);
sw.Stop();
data.Add(url, sw.Elapsed);
}
return HealthCheckResult.Healthy("Ok", data);
}
}
}

View File

@ -18,6 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection
builder.AddCheck<FileHealCheck>("file"); builder.AddCheck<FileHealCheck>("file");
builder.AddCheck<GCHealthCheck>("gc"); builder.AddCheck<GCHealthCheck>("gc");
builder.AddCheck<MemoryHealthCheck>("mem"); builder.AddCheck<MemoryHealthCheck>("mem");
builder.AddCheck<GiteeHttpHealthCheck>("Gitee");
return builder; return builder;
} }
} }