|
| 1 | +package com.nowait.applicationadmin.statistic.scheduler; |
| 2 | + |
| 3 | +import java.time.LocalDateTime; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.data.redis.core.RedisCallback; |
| 8 | +import org.springframework.data.redis.core.RedisTemplate; |
| 9 | +import org.springframework.scheduling.annotation.EnableScheduling; |
| 10 | +import org.springframework.scheduling.annotation.Scheduled; |
| 11 | +import org.springframework.stereotype.Component; |
| 12 | + |
| 13 | +import com.nowait.domainadminrdb.statistic.dto.StoreSales; |
| 14 | +import com.nowait.domainadminrdb.statistic.repository.StatisticCustomRepository; |
| 15 | +import com.nowait.domaincoreredis.common.util.RedisKeyUtils; |
| 16 | +import com.nowait.domaincoreredis.rank.repository.RankingQueryRepository; |
| 17 | + |
| 18 | +import jakarta.annotation.PostConstruct; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | + |
| 22 | + |
| 23 | +@Component |
| 24 | +@RequiredArgsConstructor |
| 25 | +@Slf4j |
| 26 | +public class RankingRefreshScheduler { |
| 27 | + |
| 28 | + private final StatisticCustomRepository statisticCustomRepository; |
| 29 | + private final RankingQueryRepository rankingQueryRepository; |
| 30 | + private final RedisTemplate<String, String> redis; |
| 31 | + |
| 32 | + @PostConstruct |
| 33 | + public void init() { |
| 34 | + // 초기화 작업 |
| 35 | + refresh(); |
| 36 | + } |
| 37 | + |
| 38 | + @Scheduled(cron = "0 */5 * * * *") // 매 5분마다 실행 |
| 39 | + public void refresh() { |
| 40 | + log.info("RankingRefreshScheduler.refresh() called at {}", LocalDateTime.now()); |
| 41 | + |
| 42 | + try { |
| 43 | + doRefresh(); |
| 44 | + } catch (Exception e) { |
| 45 | + log.error("랭킹 데이터 갱신 중 오류 발생", e); |
| 46 | + // 예외 발생 시 알림 또는 로깅 처리 |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void doRefresh() { |
| 51 | + String nextKey = RedisKeyUtils.buildNextKey(); |
| 52 | + String currentKey = RedisKeyUtils.buildCurrentKey(); |
| 53 | + String previousKey = RedisKeyUtils.buildPreviousKey(); |
| 54 | + |
| 55 | + // 1) 다음 스냅샷 키 초기화 |
| 56 | + redis.delete(nextKey); |
| 57 | + |
| 58 | + // 2) DB에서 매출 합계 가져와 ZADD |
| 59 | + List<StoreSales> salesList = statisticCustomRepository.findTotalSales(); |
| 60 | + |
| 61 | + if (salesList.isEmpty()) { |
| 62 | + log.warn("매출 데이터가 없습니다. 다음 스냅샷 키를 초기화합니다."); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + salesList.forEach(s -> |
| 67 | + rankingQueryRepository.addToRanking(nextKey, s.getStoreId(), s.getTotalSales()) |
| 68 | + ); |
| 69 | + |
| 70 | + // 3) 현재 스냅샷 키를 이전 스냅샷 키로 이동 |
| 71 | + rotateKeys(currentKey, previousKey, nextKey); |
| 72 | + } |
| 73 | + |
| 74 | + private void rotateKeys(String currentKey, String previousKey, String nextKey) { |
| 75 | + try { |
| 76 | + redis.execute((RedisCallback<Object>)connection -> { |
| 77 | + // 현재 스냅샷 키를 이전 스냅샷 키로 이동하고, 다음 스냅샷 키를 현재 스냅샷 키로 이동 |
| 78 | + if (redis.hasKey(previousKey)) { |
| 79 | + redis.delete(previousKey); |
| 80 | + } |
| 81 | + // 현재 스냅샷 키를 다음 스냅샷 키로 이동 |
| 82 | + if (redis.hasKey(currentKey)) { |
| 83 | + redis.rename(currentKey, previousKey); |
| 84 | + } |
| 85 | + // 다음 스냅샷 키가 존재하면 현재 스냅샷 키로 이동 |
| 86 | + if (redis.hasKey(nextKey)) { |
| 87 | + redis.rename(nextKey, currentKey); |
| 88 | + } |
| 89 | + return null; |
| 90 | + }); |
| 91 | + log.info("Keys rotated: current -> {}, previous -> {}, next -> {}", currentKey, previousKey, nextKey); |
| 92 | + } catch (Exception e) { |
| 93 | + log.error("Redis 키 교체 중 오류 발생", e); |
| 94 | + throw new RuntimeException("랭킹 데이터 갱신 실패", e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments