using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Diagnostics.HealthChecks; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; namespace Bootstrap.Admin.HealthChecks { /// /// Gitee 接口检查器 /// public class GiteeHttpHealthCheck : IHealthCheck { private readonly HttpClient _client; /// /// 构造函数 /// /// /// 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}"); } /// /// 异步检查方法 /// /// /// /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { var urls = new string[] { "Issues", "Pulls", "Releases", "Builds" }; var data = new Dictionary(); Task.WaitAll(urls.Select(url => Task.Run(async () => { var sw = Stopwatch.StartNew(); Exception error = null; var result = await _client.GetAsJsonAsync($"/api/Gitee/{url}", ex => error = ex, cancellationToken); sw.Stop(); data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}"); })).ToArray()); return Task.FromResult(HealthCheckResult.Healthy("Ok", data)); } } }