Skip to content

Commit

Permalink
[RAPPORT NAV]: Ajout d'un timeout lors d'un appel à l'API RapportNav (#…
Browse files Browse the repository at this point in the history
…1504)

Ajout d'un timeout et de logs  lors d'un appel à l'API RapportNav
  • Loading branch information
maximeperraultdev authored Jun 27, 2024
2 parents f55b21f + 4fdba7d commit f8dc70b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.gouv.cacem.monitorenv.config

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component

@Component
@ConfigurationProperties(prefix = "rapportnav")
class RapportnavProperties {
var url: String = ""
var timeout: Long = 3000
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import fr.gouv.cacem.monitorenv.domain.entities.mission.rapportnav.RapportNavMis
import fr.gouv.cacem.monitorenv.domain.repositories.IRapportNavMissionActionsRepository
import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.net.http.HttpTimeoutException

@Repository
class APIRapportNavMissionActionsRepository(
Expand All @@ -23,24 +26,31 @@ class APIRapportNavMissionActionsRepository(
"${rapportnavProperties.url}/api/v1/missions/$missionId"

return runBlocking {
try {
val rapportNavMissionActions =
apiClient
.httpClient
.get(missionActionsUrl)
.body<RapportNavMissionActionEntity>()
logger.info(
"Fetched is mission has actions and the result is : $rapportNavMissionActions",
)
withTimeout(rapportnavProperties.timeout) {
try {
val rapportNavMissionActions =
apiClient
.httpClient
.get(missionActionsUrl)
.body<RapportNavMissionActionEntity>()
logger.info(
"Fetched is mission has actions and the result is : $rapportNavMissionActions",
)

return@runBlocking rapportNavMissionActions
} catch (e: Exception) {
logger.error(
"Could not fetch mission actions from rapportNav at $missionActionsUrl",
e,
)
return@withTimeout rapportNavMissionActions
} catch (e: CancellationException) {
logger.error("Timeout while fetching rapportNav $missionActionsUrl", e)
throw HttpTimeoutException(e.message)
} catch (e: Exception) {
logger.error(
"Could not fetch mission actions from rapportNav at $missionActionsUrl",
e,
)

throw NoSuchElementException()
throw NoSuchElementException(
"Could not fetch mission actions from rapportNav at $missionActionsUrl",
)
}
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@

server.port=${monitorenv.server.port}
server.use-forward-headers=true
host.ip=${host.ip}
monitorenv.ajp.port=${monitorenv.ajp.port}
spring.jmx.enabled=true

spring.mvc.static-path-pattern=/**
spring.web.resources.static-locations=file:${STATIC_FILES_PATH}

spring.flyway.locations=${monitorenv.flyway.locations}

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect
spring.jpa.properties.hibernate.jdbc.time_zone = UTC

spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.datasource.url=${env.db.url}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.maxLifetime=60000

# Whether response compression is enabled.
server.compression.enabled=true
# List of user-agents to exclude from compression.
Expand All @@ -27,15 +21,14 @@ server.compression.excluded-user-agents=
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript
# Minimum "Content-Length" value that is required for compression to be performed.
server.compression.min-response-size=2048

management.endpoint.health.show-details=always
management.endpoint.metrics.enable=false
management.endpoints.web.exposure.include=*

monitorenv.sentry.enabled=${monitorenv.sentry.enabled}
monitorenv.sentry.environment=${monitorenv.sentry.environment}
monitorenv.sentry.dsn=${SENTRY_DSN}
monitorenv.version=${VERSION}
monitorfish.url=${monitorfish.url}
monitorfish.xApiKey=${monitorfish.api.key}
rapportnav.url=${rapportnav.url}
rapportnav.timeout=3000
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import io.ktor.http.*
import io.ktor.utils.io.*
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.net.http.HttpTimeoutException

class APIRapportNavActionsRepositoryITests {

Expand Down Expand Up @@ -66,4 +69,37 @@ class APIRapportNavActionsRepositoryITests {
Assertions.assertThat(missionActions.containsActionsAddedByUnit).isFalse()
}
}

@Test
fun `findRapportNavMissionActionsById should throw HttpTimeoutException after X ms when rapportNav doesnt answer`() {
runBlocking {
val mockEngine = MockEngine { _ ->
Thread.sleep(5000)
respond(
content = ByteReadChannel(
"""
{
"id": 1,
"containsActionsAddedByUnit": false
}
""",
),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json"),
)
}
val apiClient = ApiClient(mockEngine)
val rapportnavProperties = RapportnavProperties()
rapportnavProperties.url = "http://test"
rapportnavProperties.timeout = 3000

// When
val httpTimeoutException = assertThrows<HttpTimeoutException> {
APIRapportNavMissionActionsRepository(apiClient, rapportnavProperties).findRapportNavMissionActionsById(
1,
)
}
assertThat(httpTimeoutException.message).isEqualTo("Timed out waiting for 3000 ms")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ monitorenv.server.port=8880
monitorenv.flyway.locations=classpath:/db/migration
monitorenv.ajp.port=8000
host.ip=${HOST_IP}

monitorenv.sentry.environment=integration
monitorenv.sentry.enabled=true

monitorfish.url=http://monitorfish-test.csam.e2.rie.gouv.fr
rapportnav.url=http://int-rapportnav-appli01.dsi.damgm.i2
2 changes: 0 additions & 2 deletions infra/configurations/backend/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ monitorenv.server.port=8880
monitorenv.flyway.locations=classpath:/db/migration
monitorenv.ajp.port=8000
host.ip=${HOST_IP}

monitorenv.sentry.environment=production
monitorenv.sentry.enabled=true
sentry.dsn=https://[email protected]/8

monitorfish.url=https://monitorfish.din.developpement-durable.gouv.fr
rapportnav.url=https://rapport-nav.din.developpement-durable.gouv.fr

0 comments on commit f8dc70b

Please sign in to comment.