Skip to content

Commit 9189400

Browse files
committed
core: stdcm: log timetable downloads
This should help tracking that it's not blocked and it's slowing down. No more log than once in 5s. Signed-off-by: Pierre-Etienne Bougué <bougue.pe@proton.me>
1 parent e557ce2 commit 9189400

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

core/src/main/java/fr/sncf/osrd/api/TimetableProvider.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import fr.sncf.osrd.utils.json.UnitAdapterFactory
1515
import java.time.Duration
1616
import java.time.Instant
1717
import java.time.ZonedDateTime
18+
import java.util.concurrent.atomic.AtomicLong
1819
import kotlin.math.pow
1920
import kotlinx.coroutines.Dispatchers
2021
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -59,7 +60,17 @@ class TimetableDownloader(
5960
infraId: String,
6061
timetableId: TimetableId,
6162
): Flow<TrainRequirementsById> = flow {
62-
val firstPageTrainRequirements = getTrainPaginatedRequirements(infraId, timetableId, 1)
63+
logger.info(
64+
"Fetching train requirements for infra $infraId and timetable $timetableId: Start."
65+
)
66+
67+
val lastLogSecond = AtomicLong(Long.MIN_VALUE)
68+
val firstPageTrainRequirements =
69+
getTrainPaginatedRequirements(infraId, timetableId, 1, lastLogSecond)
70+
71+
logger.info(
72+
"Fetching train requirements for infra $infraId and timetable $timetableId: ${firstPageTrainRequirements.pageCount} total pages to get."
73+
)
6374

6475
emitAll(firstPageTrainRequirements.results.asFlow())
6576
emitAll(
@@ -69,23 +80,38 @@ class TimetableDownloader(
6980
.flatMapMerge { page ->
7081
flow {
7182
val paginatedTrainRequirements =
72-
getTrainPaginatedRequirements(infraId, timetableId, page)
83+
getTrainPaginatedRequirements(infraId, timetableId, page, lastLogSecond)
7384
emitAll(paginatedTrainRequirements.results.asFlow())
7485
}
7586
}
7687
.flowOn(httpDispatcher)
7788
)
89+
logger.info(
90+
"Fetching train requirements for infra $infraId and timetable $timetableId: End."
91+
)
7892
}
7993

8094
private fun getTrainPaginatedRequirements(
8195
infraId: String,
8296
timetableId: TimetableId,
8397
page: Int,
98+
lastLogSecond: AtomicLong,
8499
): PaginatedRequirements {
85100
val endpointPath = "timetable/$timetableId/requirements/"
86101
val request =
87102
buildRequest(endpointPath, "infra_id=$infraId&page=$page&page_size=$PAGE_SIZE")
88103
val response = getWithRetries(request)
104+
105+
// Do not log more than once in 5s
106+
val muteDuration = 5
107+
val now = Instant.now().epochSecond
108+
val lastLog = lastLogSecond.getAndUpdate { if (it + muteDuration < now) now else it }
109+
if (lastLog + muteDuration < now) {
110+
logger.info(
111+
"Fetching train requirements for infra $infraId and timetable $timetableId: obtained page $page, HTTP status ${response.code} [muting this log for the next $muteDuration s]."
112+
)
113+
}
114+
89115
return paginatedRequirementsAdapter.fromJson(response.body.source())!!
90116
}
91117

0 commit comments

Comments
 (0)