From 8f2001ad3292d3a68e9aed0a395d506334f49419 Mon Sep 17 00:00:00 2001 From: OleksandrChmyrNAV Date: Wed, 9 Oct 2024 11:47:08 +0200 Subject: [PATCH] Ny instans av timeout i cpa-sync ved kall til cpa-repo timestamps endepunkt (#1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Håndtering av forespørsel timeout i løpet av CPA sync * Fikset navn på rotprosjekt * Forenklet feilhåndtering --- .../main/kotlin/no/nav/emottak/cpa/HttpClient.kt | 4 ++++ .../src/main/kotlin/no/nav/emottak/cpa/Routes.kt | 13 +++++++++++-- settings.gradle.kts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/HttpClient.kt b/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/HttpClient.kt index be2e219..d013209 100644 --- a/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/HttpClient.kt +++ b/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/HttpClient.kt @@ -4,6 +4,7 @@ import com.nimbusds.jwt.SignedJWT import io.ktor.client.HttpClient import io.ktor.client.HttpClientConfig import io.ktor.client.engine.cio.CIO +import io.ktor.client.plugins.HttpTimeout import io.ktor.client.plugins.auth.Auth import io.ktor.client.plugins.auth.providers.BearerTokens import io.ktor.client.plugins.auth.providers.bearer @@ -78,6 +79,9 @@ suspend fun getCpaRepoToken(): BearerTokens { fun getCpaRepoAuthenticatedClient(): HttpClient { return HttpClient(CIO) { + install(HttpTimeout) { + this.requestTimeoutMillis = 60000 + } install(ContentNegotiation) { json() } diff --git a/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/Routes.kt b/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/Routes.kt index cdd3014..38ad6c7 100644 --- a/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/Routes.kt +++ b/cpa-sync/src/main/kotlin/no/nav/emottak/cpa/Routes.kt @@ -1,5 +1,7 @@ package no.nav.emottak.cpa +import io.ktor.client.network.sockets.ConnectTimeoutException +import io.ktor.client.plugins.HttpRequestTimeoutException import io.ktor.client.request.get import io.ktor.client.statement.bodyAsText import io.ktor.http.HttpStatusCode @@ -27,8 +29,15 @@ fun Route.cpaSync(): Route = get("/cpa-sync") { } result.onSuccess { log.info("CPA sync completed in $duration") + TIMEOUT_EXCEPTION_COUNTER = 0 call.respond(HttpStatusCode.OK, "CPA sync complete") }.onFailure { + when (it) { + // flere feilhåndteringer pga. vi ikke er sikre på hvilken timeout er det. + is HttpRequestTimeoutException, is ConnectTimeoutException -> log.error("Timeout exception", it) + .also { TIMEOUT_EXCEPTION_COUNTER++ } + else -> log.error(it.message, it) + } call.respond(HttpStatusCode.InternalServerError, "Something went wrong") } } @@ -43,10 +52,10 @@ fun Route.testAzureAuthToCpaRepo(): Route = get("/testCpaRepoConnection") { ) } -var BUG_ENCOUNTERED_CPA_REPO_TIMEOUT = false +var TIMEOUT_EXCEPTION_COUNTER = 0 fun Routing.registerHealthEndpoints() { get("/internal/health/liveness") { - if (BUG_ENCOUNTERED_CPA_REPO_TIMEOUT) { // TODO : årsak ukjent, cpa-repo/timestamps endepunkt timer ut etter en stund + if (TIMEOUT_EXCEPTION_COUNTER > 5) { // TODO : årsak ukjent, cpa-repo/timestamps endepunkt timer ut etter en stund call.respond(HttpStatusCode.ServiceUnavailable, "Restart me X_X") } else { call.respondText("I'm alive! :)") diff --git a/settings.gradle.kts b/settings.gradle.kts index 2e90b79..43de047 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -147,5 +147,5 @@ dependencyResolutionManagement { } } -rootProject.name = "ebxml-processor" +rootProject.name = "send-in-send-out" include("felles", "smtp-transport", "ebms-send-in", "cpa-sync")