Skip to content

Commit

Permalink
fix: duplicate request on UpdateEpisodeJob
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Sep 4, 2024
1 parent 523576a commit 45873cc
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/main/kotlin/fr/shikkanime/jobs/UpdateEpisodeJob.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.shikkanime.jobs

import com.google.inject.Inject
import fr.shikkanime.caches.CountryCodeIdKeyCache
import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.EpisodeMapping
import fr.shikkanime.entities.EpisodeVariant
Expand Down Expand Up @@ -48,6 +49,30 @@ class UpdateEpisodeJob : AbstractJob {
@Inject
private lateinit var traceActionService: TraceActionService

private val adnCache = MapCache<CountryCodeIdKeyCache, List<Episode>> {
return@MapCache runBlocking {
val video = try {
AnimationDigitalNetworkWrapper.getShowVideo(it.id)
} catch (e: Exception) {
logger.severe("Impossible to get ADN video ${it.id} : ${e.message} (Maybe the video is not available anymore)")
return@runBlocking emptyList()
}

try {
animationDigitalNetworkPlatform.convertEpisode(
it.countryCode,
video,
ZonedDateTime.now(),
needSimulcast = false,
checkAnimation = false
)
} catch (e: Exception) {
logger.warning("Error while getting ADN episode ${it.id} : ${e.message}")
emptyList()
}
}
}

override fun run() {
// Take 15 episodes of a platform, and if the lastUpdate is older than 30 days, or if the episode mapping is valid
val lastDateTime = ZonedDateTime.now().minusDays(30)
Expand Down Expand Up @@ -127,17 +152,19 @@ class UpdateEpisodeJob : AbstractJob {

if (hasChanged) {
traceActionService.createTraceAction(mapping, TraceAction.Action.UPDATE)
logger.info("Episode $mappingIdentifier updated")
}

logger.info("Episode $mappingIdentifier updated")
}

if (needRecalculate) {
logger.info("Recalculating simulcasts...")
animeService.recalculateSimulcasts()
}

logger.info("Episodes updated")

if (needRefreshCache) {
logger.info("Episodes updated")
MapCache.invalidate(Anime::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java)
}
}
Expand All @@ -158,7 +185,7 @@ class UpdateEpisodeJob : AbstractJob {
return emptyList()
}

episodes.addAll(getADNEpisodeAndVariants(countryCode, adnId))
episodes.addAll(adnCache[CountryCodeIdKeyCache(countryCode, adnId)]!!)
}

if (episodeVariant.platform == Platform.CRUN) {
Expand Down Expand Up @@ -218,29 +245,4 @@ class UpdateEpisodeJob : AbstractJob {
}
}
}

private suspend fun getADNEpisodeAndVariants(
countryCode: CountryCode,
adnId: String,
): List<Episode> {
val video = try {
AnimationDigitalNetworkWrapper.getShowVideo(adnId)
} catch (e: Exception) {
logger.severe("Impossible to get ADN video $adnId : ${e.message} (Maybe the video is not available anymore)")
return emptyList()
}

return try {
animationDigitalNetworkPlatform.convertEpisode(
countryCode,
video,
ZonedDateTime.now(),
needSimulcast = false,
checkAnimation = false
)
} catch (e: Exception) {
logger.warning("Error while getting ADN episode $adnId : ${e.message}")
emptyList()
}
}
}

0 comments on commit 45873cc

Please sign in to comment.