forked from xiongyuxing/tiku-backend.net
- Added tags to BrowserAuthController for browser authentication endpoints. - Added tags to CatalogController for public catalog access. - Added tags to CommerceController for student transaction operations. - Added tags to CommissionController for tenant commission management. - Added tags to CrmController for tenant CRM functionalities. - Added tags to HealthController for system health checks. - Added tags to LearningController for student learning resources. - Added tags to MeController for current user information. - Added tags to PlatformAdminController for platform management. - Introduced PlatformBackofficeController for backend permissions management. - Added tags to PlatformBillingCallbackController for billing callbacks. - Added tags to PlatformPaymentSettingsController for payment settings management. - Added tags to PlatformSaasController for SaaS package management. - Added tags to PlatformTenantCapabilitiesController for tenant capabilities. - Added tags to PointsController for student points management. - Added tags to ProfileController for student profile management. - Added tags to QuestionVideosController for question video resources. - Added tags to ReferralController for referral growth management. - Added tags to RuntimeController for runtime configurations. - Added tags to ScorelineController for scoreline management. - Added tags to TaxonomyController for category management. - Added tags to TenantAdminDirectController for tenant operations management. - Introduced TenantBackofficeController for tenant backend permissions. - Added tags to TenantBillingController for tenant billing operations. - Added tags to TenantCommerceController for tenant commerce operations. - Added tags to TenantContentController for tenant content management. - Added tags to TenantContentDirectController for direct content management. - Added tags to TenantFrontendConfigController for frontend configurations. - Added tags to TenantOnboardingController for onboarding guidance. - Added tags to TenantPublicController for public tenant configurations. - Added tags to TenantsController for current tenant information. - Added tags to VideosController for student video resources.
79 lines
3.3 KiB
C#
79 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Tiku.Api.Contracts;
|
|
using Tiku.Application.Security;
|
|
using Tiku.Infrastructure.Persistence;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MassTransit.EntityFrameworkCoreIntegration;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using Tiku.Infrastructure.Messaging;
|
|
|
|
namespace Tiku.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Tags("平台端-系统健康")]
|
|
[AllowAnonymous]
|
|
[Produces("application/json")]
|
|
[Route("api/health")]
|
|
public sealed class HealthController(
|
|
TikuDbContext dbContext,
|
|
IRedisSecurityStore redisSecurityStore,
|
|
MessagingOptions messagingOptions,
|
|
HealthCheckService healthCheckService) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[EndpointSummary("健康检查")]
|
|
[EndpointDescription("用于本地、CI 和部署环境 smoke test 的轻量健康检查。")]
|
|
[ProducesResponseType<HealthResponseDto>(StatusCodes.Status200OK)]
|
|
public ActionResult<HealthResponseDto> Get()
|
|
{
|
|
return Ok(new HealthResponseDto(
|
|
"ok",
|
|
"tiku-api",
|
|
DateTimeOffset.UtcNow));
|
|
}
|
|
|
|
[HttpGet("ready")]
|
|
[EndpointSummary("依赖就绪检查")]
|
|
public async Task<ActionResult<object>> Ready(CancellationToken cancellationToken)
|
|
{
|
|
var database = await dbContext.Database.CanConnectAsync(cancellationToken);
|
|
var redis = !redisSecurityStore.IsConfigured || await redisSecurityStore.PingAsync(cancellationToken);
|
|
var rabbitHealth = await healthCheckService.CheckHealthAsync(
|
|
registration => registration.Tags.Contains("ready"),
|
|
cancellationToken);
|
|
var rabbitMq = !messagingOptions.IsConfigured || rabbitHealth.Status == HealthStatus.Healthy;
|
|
var outboxPending = database
|
|
? await dbContext.Set<OutboxMessage>().CountAsync(cancellationToken)
|
|
: -1;
|
|
var outboxOldestSentTime = database
|
|
? await dbContext.Set<OutboxMessage>()
|
|
.Select(message => (DateTime?)message.SentTime)
|
|
.MinAsync(cancellationToken)
|
|
: null;
|
|
var outboxOldestAgeSeconds = outboxOldestSentTime is null
|
|
? 0
|
|
: Math.Max(0, (DateTimeOffset.UtcNow - new DateTimeOffset(outboxOldestSentTime.Value)).TotalSeconds);
|
|
var outboxAlert = outboxPending >= messagingOptions.OutboxBacklogAlertCount ||
|
|
outboxOldestAgeSeconds >= messagingOptions.OutboxOldestMessageAlertSeconds;
|
|
var ready = database && redis && rabbitMq;
|
|
var response = new
|
|
{
|
|
status = ready ? "ready" : "not_ready",
|
|
database,
|
|
redis = new { configured = redisSecurityStore.IsConfigured, ready = redis },
|
|
rabbitMq = new { configured = messagingOptions.IsConfigured, ready = rabbitMq },
|
|
outbox = new
|
|
{
|
|
pending = outboxPending,
|
|
oldestAgeSeconds = Math.Round(outboxOldestAgeSeconds, 1),
|
|
alert = outboxAlert,
|
|
backlogAlertCount = messagingOptions.OutboxBacklogAlertCount,
|
|
oldestMessageAlertSeconds = messagingOptions.OutboxOldestMessageAlertSeconds
|
|
},
|
|
checkedAt = DateTimeOffset.UtcNow
|
|
};
|
|
return ready ? Ok(response) : StatusCode(StatusCodes.Status503ServiceUnavailable, response);
|
|
}
|
|
}
|