Skip to content

Commit

Permalink
Retry kall til innsending-api
Browse files Browse the repository at this point in the history
  • Loading branch information
akgagnat committed Sep 16, 2024
1 parent e553b0c commit 2ec3328
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import no.nav.soknad.arkivering.innsending.api.*
import no.nav.soknad.arkivering.innsending.model.*
import no.nav.soknad.arkivering.innsending.infrastructure.Serializer.jacksonObjectMapper
import no.nav.soknad.arkivering.tokensupport.createOkHttpAuthorizationClient
import no.nav.soknad.arkivering.utils.retry
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -50,19 +51,19 @@ class InnsendingApi(config: Config, useOauth: Boolean? = false) {
)
}
)
val soknad = ettersending.opprettEttersending(dto)
val soknad = retry(3) { ettersending.opprettEttersending(dto) }
logger.info("Opprettet ettersending: $soknad")
return SoknadTestdata(soknad, sendInnFil)
}

fun sendInn(soknad: SoknadTestdata) = runCatching {
fun sendInn(soknad: SoknadTestdata) {
logger.info("Sender inn søknad: ${soknad.innsendingsId}")
sendInnSoknad.sendInnSoknad(soknad.innsendingsId)
retry(3) { sendInnSoknad.sendInnSoknad(soknad.innsendingsId) }
}

fun sendInn(innsendingsId: String) = runCatching {
fun sendInn(innsendingsId: String) {
logger.info("Sender inn søknad: ${innsendingsId}")
sendInnSoknad.sendInnSoknad(innsendingsId)
retry(3) { sendInnSoknad.sendInnSoknad(innsendingsId) }
}

fun getArkiveringsstatus(innsendingsId: String): ArkiveringsStatusDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.soknad.arkivering.innsending

import no.nav.soknad.arkivering.innsending.api.SendinnFilApi
import no.nav.soknad.arkivering.innsending.model.DokumentSoknadDto
import no.nav.soknad.arkivering.utils.retry
import java.io.File

class SoknadTestdata(
Expand Down Expand Up @@ -38,7 +39,7 @@ class VedleggslisteTestdata(
}

fun lastOppFil(index: Int, file: File): VedleggslisteTestdata {
sendinnFilApi.lagreFil(innsendingsId, vedleggIdListe[index].id, file)
retry(3) { sendinnFilApi.lagreFil(innsendingsId, vedleggIdListe[index].id, file) }
return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package no.nav.soknad.arkivering.utils

fun <R> retry(
maxAttempts: Int,
action: () -> R
): R {
require(maxAttempts > 0) { "maxAttempts must be greater than 0" }
return runCatching(action).getOrElse {
val leftAttempts = maxAttempts.dec()
if (leftAttempts == 0) throw it
retry(leftAttempts, action)
}
}

0 comments on commit 2ec3328

Please sign in to comment.