From aebb305b806856987326daad35132450dcd9c65d Mon Sep 17 00:00:00 2001 From: stevomcallister Date: Thu, 21 Sep 2023 16:38:23 +0100 Subject: [PATCH] PI-1478 changes to retry exceptions --- .../uk/gov/justice/digital/hmpps/retry/Retry.kt | 14 +++++++++++--- .../hmpps/listener/AwsNotificationListener.kt | 4 ++-- .../digital/hmpps/config/feign/FeignConfig.kt | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/retry/Retry.kt b/libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/retry/Retry.kt index 03e74eb34f..b2dab5ec19 100644 --- a/libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/retry/Retry.kt +++ b/libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/retry/Retry.kt @@ -1,8 +1,15 @@ package uk.gov.justice.digital.hmpps.retry +import java.time.Duration +import java.util.concurrent.TimeUnit import kotlin.reflect.KClass -fun retry(maxRetries: Int, exceptions: List> = listOf(Exception::class), code: () -> T): T { +fun retry( + maxRetries: Int, + delay: Duration = Duration.ofMillis(100), + exceptions: List> = listOf(Exception::class), + code: () -> T +): T { var throwable: Throwable? (1..maxRetries).forEach { count -> try { @@ -14,8 +21,9 @@ fun retry(maxRetries: Int, exceptions: List> = listOf( } else { e } - - if (throwable != null) { + if (throwable == null) { + TimeUnit.MILLISECONDS.sleep(delay.toMillis() * count * count) + }else{ throw throwable!! } } diff --git a/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/listener/AwsNotificationListener.kt b/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/listener/AwsNotificationListener.kt index bc932082c5..65db7b458f 100644 --- a/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/listener/AwsNotificationListener.kt +++ b/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/listener/AwsNotificationListener.kt @@ -1,7 +1,6 @@ package uk.gov.justice.digital.hmpps.listener import feign.FeignException -import feign.RetryableException import io.awspring.cloud.sqs.annotation.SqsListener import io.awspring.cloud.sqs.listener.AsyncAdapterBlockingExecutionFailedException import io.awspring.cloud.sqs.listener.ListenerExecutionFailedException @@ -11,6 +10,7 @@ import io.sentry.Sentry import io.sentry.spring.jakarta.tracing.SentryTransaction import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression import org.springframework.context.annotation.Conditional +import org.springframework.dao.CannotAcquireLockException import org.springframework.jdbc.CannotGetJdbcConnectionException import org.springframework.orm.ObjectOptimisticLockingFailureException import org.springframework.stereotype.Component @@ -35,7 +35,7 @@ class AwsNotificationListener( 3, listOf( FeignException.NotFound::class, - RetryableException::class, + CannotAcquireLockException::class, ObjectOptimisticLockingFailureException::class, CannotCreateTransactionException::class, CannotGetJdbcConnectionException::class diff --git a/libs/oauth-client/src/main/kotlin/uk/gov/justice/digital/hmpps/config/feign/FeignConfig.kt b/libs/oauth-client/src/main/kotlin/uk/gov/justice/digital/hmpps/config/feign/FeignConfig.kt index 640cc2548c..62ea960dd1 100644 --- a/libs/oauth-client/src/main/kotlin/uk/gov/justice/digital/hmpps/config/feign/FeignConfig.kt +++ b/libs/oauth-client/src/main/kotlin/uk/gov/justice/digital/hmpps/config/feign/FeignConfig.kt @@ -1,6 +1,7 @@ package uk.gov.justice.digital.hmpps.config.feign import feign.RequestInterceptor +import feign.Retryer import org.springframework.context.annotation.Bean import org.springframework.http.HttpHeaders import org.springframework.security.authentication.AnonymousAuthenticationToken @@ -16,6 +17,9 @@ abstract class FeignConfig( abstract fun registrationId(): String + @Bean + open fun retryer() = Retryer.Default() + @Bean open fun requestInterceptor() = RequestInterceptor { template -> template.header(HttpHeaders.AUTHORIZATION, "Bearer ${getAccessToken()}")