11package org.gitanimals.gotcha.infra
22
3+ import org.gitanimals.core.IdGenerator
4+ import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
35import org.gitanimals.gotcha.app.RenderApi
46import org.gitanimals.gotcha.domain.DropRateClient
7+ import org.slf4j.LoggerFactory
8+ import org.slf4j.MDC
59import org.springframework.boot.context.event.ApplicationStartedEvent
610import org.springframework.context.event.EventListener
711import org.springframework.scheduling.annotation.Scheduled
@@ -12,6 +16,7 @@ class DropRateCache(
1216 private val renderApi : RenderApi ,
1317) : DropRateClient {
1418
19+ private val logger = LoggerFactory .getLogger(this ::class .simpleName)
1520 private var dropRates = mutableMapOf<String , Double >()
1621
1722 override fun getDropRate (name : String ): Double = dropRates[name] ? : DEFAULT_DROP_RATE
@@ -27,7 +32,15 @@ class DropRateCache(
2732 }
2833
2934 fun updateDropRate () {
30- val personas = renderApi.getAllPersonas().personas
35+ val personas = runCatching {
36+ MDC .put(TRACE_ID , IdGenerator .generate().toString())
37+ renderApi.getAllPersonas().personas
38+ }.getOrElse {
39+ logger.error(" Fail to cache drop rate retry after 10 minutes" , it)
40+ emptyList()
41+ }.also {
42+ MDC .remove(TRACE_ID )
43+ }
3144
3245 personas.forEach { persona ->
3346 dropRates[persona.type] = persona.dropRate.replace(" %" , " " ).toDouble()
0 commit comments