package com.krylov.refound.ai.client; import com.krylov.refound.ai.dto.ModerationResponse; import com.krylov.refound.ai.dto.TextModerationRequest; import com.krylov.refound.ai.exception.AiServerException; import com.krylov.refound.ai.exception.AiTimeoutException; import com.krylov.refound.ai.exception.AiUnavailableException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor @Slf4j public class TextModerationClient { private final AiHttpClient client; @Retryable( retryFor = {AiUnavailableException.class, AiServerException.class, AiTimeoutException.class}, // Параметры из ai.retry.* (AiRetryProperties); раньше они были захардкожены maxAttemptsExpression = "${ai.retry.max-attempts:3}", backoff = @Backoff(delayExpression = "${ai.retry.delay:500}", multiplierExpression = "${ai.retry.multiplier:2}") ) public ModerationResponse moderate(String text) { log.info("Calling AI TextModeration service"); return client.post("/api/v1/moderation/text", new TextModerationRequest(text), ModerationResponse.class); } // После исчерпания попыток возвращаем null: пост остаётся на модерации и // проверяется снова при следующем запуске планировщика. @Recover public ModerationResponse recover(AiUnavailableException e, String text) { log.error("AI TextModeration unavailable after retries: {}", e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); return null; } @Recover public ModerationResponse recover(AiTimeoutException e, String text) { log.error("AI TextModeration timed out after retries"); return null; } @Recover public ModerationResponse recover(AiServerException e, String text) { log.error("AI TextModeration server error after retries: {}", e.getMessage()); return null; } }