SPMS_API/SPMS.API/Controllers/PublicController.cs
seonkyu.kim 3fc3bb8144 fix: RabbitMQ 상태 모니터링 및 백그라운드 재시도 추가 (#124)
- RabbitMQInitializer를 BackgroundService로 변경 (30초 간격 재시도)
- RabbitMQConnection에 IsConnected 속성 추가
- Health check에 RabbitMQ 연결/초기화 상태 반영
- DI 등록 변경 (Singleton + HostedService 패턴)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 19:15:42 +09:00

81 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using SPMS.Domain.Common;
using SPMS.Infrastructure;
using SPMS.Infrastructure.Messaging;
namespace SPMS.API.Controllers;
[ApiController]
[Route("v1/out")]
[AllowAnonymous]
[ApiExplorerSettings(GroupName = "public")]
public class PublicController : ControllerBase
{
private readonly AppDbContext _dbContext;
private readonly RabbitMQConnection _rabbitConnection;
private readonly RabbitMQInitializer _rabbitInitializer;
public PublicController(
AppDbContext dbContext,
RabbitMQConnection rabbitConnection,
RabbitMQInitializer rabbitInitializer)
{
_dbContext = dbContext;
_rabbitConnection = rabbitConnection;
_rabbitInitializer = rabbitInitializer;
}
[HttpPost("health")]
[SwaggerOperation(Summary = "서버 상태 확인", Description = "MariaDB, Redis, RabbitMQ 연결 상태를 확인합니다.")]
public async Task<IActionResult> HealthCheckAsync()
{
var checks = new Dictionary<string, object>();
var allHealthy = true;
// 1. MariaDB 연결 확인
try
{
await _dbContext.Database.ExecuteSqlRawAsync("SELECT 1");
checks["database"] = new { status = "healthy" };
}
catch (Exception ex)
{
checks["database"] = new { status = "unhealthy", error = ex.Message };
allHealthy = false;
}
// 2. Redis 연결 확인 (Phase 3-2에서 구현 예정)
checks["redis"] = new { status = "not_configured" };
// 3. RabbitMQ 연결 확인
var rabbitConnected = _rabbitConnection.IsConnected;
var rabbitInitialized = _rabbitInitializer.IsInitialized;
if (rabbitConnected && rabbitInitialized)
{
checks["rabbitmq"] = new { status = "healthy" };
}
else
{
checks["rabbitmq"] = new
{
status = "unhealthy",
connected = rabbitConnected,
initialized = rabbitInitialized
};
allHealthy = false;
}
if (allHealthy)
{
return Ok(ApiResponse<object>.Success(checks));
}
return StatusCode(503, ApiResponse<object>.Fail(
ErrorCodes.InternalError,
"하나 이상의 서비스에 문제가 있습니다."));
}
}