fix(content): 오디오 스냅샷 일 배치 lock을 보강한다

This commit is contained in:
2026-07-12 22:45:20 +09:00
parent 91f0d20a79
commit 18abdee249
2 changed files with 68 additions and 0 deletions

View File

@@ -1,6 +1,9 @@
package kr.co.vividnext.sodalive.v2.content.recommendation.adapter.out.scheduler
import kr.co.vividnext.sodalive.v2.content.recommendation.application.AudioRecommendationSnapshotFallbackService
import kr.co.vividnext.sodalive.v2.content.recommendation.application.AudioRecommendationSnapshotRefreshService
import kr.co.vividnext.sodalive.v2.recommendation.domain.RecommendedSectionType
import org.redisson.api.RLock
import org.redisson.api.RedissonClient
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
@@ -14,19 +17,43 @@ class AudioRecommendationSnapshotScheduler(
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
fun refreshDailySnapshots() {
val lock = redissonClient.getLock(LOCK_KEY)
val sectionLocks = mutableListOf<RLock>()
try {
if (lock.tryLock(0, -1, TimeUnit.SECONDS)) {
if (!tryLockSections(sectionLocks)) return
refreshService.refreshDailySnapshots()
}
} finally {
sectionLocks.asReversed().forEach { sectionLock ->
if (sectionLock.isHeldByCurrentThread) {
sectionLock.unlock()
}
}
if (lock.isHeldByCurrentThread) {
lock.unlock()
}
}
}
private fun tryLockSections(acquiredLocks: MutableList<RLock>): Boolean {
return AUDIO_SECTION_TYPES.all { sectionType ->
val lock = redissonClient.getLock(AudioRecommendationSnapshotFallbackService.lockKey(sectionType))
if (!lock.tryLock(0, -1, TimeUnit.MILLISECONDS)) return false
acquiredLocks.add(lock)
true
}
}
companion object {
const val LOCK_KEY = "lock:audio-recommendation-snapshot-refresh"
private val AUDIO_SECTION_TYPES = listOf(
RecommendedSectionType.NEW_AND_HOT_AUDIO_SAFE,
RecommendedSectionType.NEW_AND_HOT_AUDIO_ALL,
RecommendedSectionType.MOST_COMMENTED_AUDIO_SAFE,
RecommendedSectionType.MOST_COMMENTED_AUDIO_ALL,
RecommendedSectionType.RECOMMENDED_AUDIO_SAFE,
RecommendedSectionType.RECOMMENDED_AUDIO_ALL
)
}
}