feat(recommend): 추천 스냅샷 lock을 적용한다
This commit is contained in:
@@ -1,15 +1,29 @@
|
||||
package kr.co.vividnext.sodalive.v2.recommend.adapter.out.scheduler
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.recommend.application.RecommendationSnapshotRefreshService
|
||||
import org.redisson.api.RedissonClient
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Component
|
||||
class RecommendationSnapshotScheduler(
|
||||
private val refreshService: RecommendationSnapshotRefreshService
|
||||
private val refreshService: RecommendationSnapshotRefreshService,
|
||||
private val redissonClient: RedissonClient
|
||||
) {
|
||||
@Scheduled(cron = "0 0 6 * * *", zone = "Asia/Seoul")
|
||||
fun refreshDailySnapshots() {
|
||||
refreshService.refreshDailySnapshots()
|
||||
val lockName = "lock:recommendation-snapshot-refresh"
|
||||
val lock = redissonClient.getLock(lockName)
|
||||
|
||||
try {
|
||||
if (lock.tryLock(0, -1, TimeUnit.SECONDS)) {
|
||||
refreshService.refreshDailySnapshots()
|
||||
}
|
||||
} finally {
|
||||
if (lock.isHeldByCurrentThread) {
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user