Skip to content

Commit

Permalink
Added retry exausted handling to post transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
wcdkj committed Jan 24, 2025
1 parent 92f0c92 commit d49cb57
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PostTransactionForPersonTest(
nomisApiMockServer.stubNomisApiResponseForPost(
prisonId,
nomisNumber,
asJsonString(exampleTransaction),
asJsonString(exampleTransaction.toApiConformingMap()),
"""
{
"id": "6179604-1",
Expand Down

0 comments on commit d49cb57

Please sign in to comment.