diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/WebClientWrapper.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/WebClientWrapper.kt index 61a8d6d9e..a9b378034 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/WebClientWrapper.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/WebClientWrapper.kt @@ -12,12 +12,15 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.ResponseException import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError +import java.time.Duration +@Suppress("ktlint:standard:property-naming") class WebClientWrapper( val baseUrl: String, ) { - @Suppress("ktlint:standard:property-naming") - val CREATE_TRANSACTION_RETRY_HTTP_CODES = listOf(500, 502, 503, 504, 522, 599, 404, 499, 408, 301) + val CREATE_TRANSACTION_RETRY_HTTP_CODES = listOf(500, 502, 503, 504, 522, 599, 499, 408, 301) + val MAX_RETRY_ATTEMPTS = 3L + val MIN_BACKOFF_DURATION = Duration.ofSeconds(3) val client: WebClient = WebClient @@ -79,8 +82,9 @@ class WebClientWrapper( .bodyToMono(T::class.java) .retryWhen( Retry - .backoff(3, java.time.Duration.ofSeconds(3)) - .filter { throwable -> throwable is ResponseException }, + .backoff(MAX_RETRY_ATTEMPTS, MIN_BACKOFF_DURATION) + .filter { throwable -> throwable is ResponseException } + .onRetryExhaustedThrow { _, retrySignal -> throw ResponseException("External Service failed to process after max retries", HttpStatus.SERVICE_UNAVAILABLE.value()) }, ).block()!! WebClientWrapperResponse.Success(responseData) diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/PostTransactionForPersonTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/PostTransactionForPersonTest.kt index 0df8a6aa6..bc7c7a303 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/PostTransactionForPersonTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/nomis/PostTransactionForPersonTest.kt @@ -47,7 +47,7 @@ class PostTransactionForPersonTest( nomisApiMockServer.stubNomisApiResponseForPost( prisonId, nomisNumber, - asJsonString(exampleTransaction), + asJsonString(exampleTransaction.toApiConformingMap()), """ { "id": "6179604-1",