fix: RabbitMQ 연결 실패 시 앱 크래시 방지 (#124)

- StartAsync에서 throw 제거, LogWarning으로 변경
- InitializeAsync 메서드 분리 (재시도 가능)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
seonkyu.kim 2026-02-10 19:05:13 +09:00
parent 355d3269c0
commit efd6615809

View File

@ -25,10 +25,19 @@ public class RabbitMQInitializer : IHostedService
public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
await InitializeAsync(cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "RabbitMQ 초기화 실패 — 서버는 계속 실행됩니다. 메시지 발송 시 재연결을 시도합니다.");
}
}
public async Task InitializeAsync(CancellationToken cancellationToken)
{
await using var channel = await _connection.CreateChannelAsync(cancellationToken);
// Exchange 선언: Direct, Durable
await channel.ExchangeDeclareAsync(
exchange: _settings.Exchange,
type: ExchangeType.Direct,
@ -44,7 +53,6 @@ public class RabbitMQInitializer : IHostedService
{ "x-message-ttl", _settings.MessageTtl }
};
// Push Queue 선언
await channel.QueueDeclareAsync(
queue: _settings.PushQueue,
durable: true,
@ -62,7 +70,6 @@ public class RabbitMQInitializer : IHostedService
_logger.LogInformation("Queue 선언 및 바인딩 완료: {Queue} → {Exchange} (routing_key: push)",
_settings.PushQueue, _settings.Exchange);
// Schedule Queue 선언
await channel.QueueDeclareAsync(
queue: _settings.ScheduleQueue,
durable: true,
@ -80,12 +87,6 @@ public class RabbitMQInitializer : IHostedService
_logger.LogInformation("Queue 선언 및 바인딩 완료: {Queue} → {Exchange} (routing_key: schedule)",
_settings.ScheduleQueue, _settings.Exchange);
}
catch (Exception ex)
{
_logger.LogError(ex, "RabbitMQ Exchange/Queue 초기화 실패");
throw;
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}