diff --git a/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java b/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java index 646b0f30..0b32dca2 100644 --- a/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java +++ b/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java @@ -6,6 +6,8 @@ import lombok.RequiredArgsConstructor; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -15,6 +17,7 @@ @Component public class FcfsScheduler { + private static final Logger log = LoggerFactory.getLogger(FcfsScheduler.class); // 분산 환경에서 메서드가 여러 번 실행되는 것을 방지하기 위해 분산 락 도입 private final RedissonClient redissonClient; private final FcfsManageService fcfsManageService; @@ -22,6 +25,7 @@ public class FcfsScheduler { // 매일 자정 1분마다 실행되며, 오늘의 선착순 이벤트에 대한 정보를 DB에서 Redis로 이동시킨다. @Scheduled(cron = "0 1 0 * * *") public void registerFcfsEvents() { + log.info("Move the information of FCFS Events from DB to Redis"); RLock lock = redissonClient.getLock(ConstantUtil.DB_TO_REDIS_LOCK); try { // 5분동안 락 점유 @@ -40,6 +44,7 @@ public void registerFcfsEvents() { // 매일 자정마다 실행되며, 선착순 이벤트 당첨자들을 Redis에서 DB로 이동시킨다. @Scheduled(cron = "0 0 0 * * *") public void registerWinners() { + log.info("Move the result of FCFS Events from Redis to DB"); RLock lock = redissonClient.getLock(ConstantUtil.REDIS_TO_DB_LOCK); try { // 5분동안 락 점유 diff --git a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java index 101a7e3f..9051cdc5 100644 --- a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java +++ b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java @@ -49,38 +49,44 @@ public void registerFcfsEvents() { // redis에 저장된 모든 선착순 이벤트의 당첨자 정보를 DB로 이관 @Transactional public void registerWinners() { - Set fcfsIds = stringRedisTemplate.keys("*:count"); - if (fcfsIds == null || fcfsIds.isEmpty()) { + Set fcfsKeys = stringRedisTemplate.keys("*:count"); + if (fcfsKeys == null || fcfsKeys.isEmpty()) { + log.info("There are no FCFS events in yesterday"); return; } - for(String fcfsId : fcfsIds) { - String eventId = fcfsId.replace(":count", "").replace("fcfs:", ""); - Set userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(eventId), 0, -1); + // 당첨자 관련 정보 조합하여 Entity 생성 + log.info("keys for FCFS Events: {}", fcfsKeys); + for(String key : fcfsKeys) { + String fcfsEventId = key.replace(":count", "").replace("fcfs:", ""); + Set userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(fcfsEventId), 0, -1); if(userIds == null || userIds.isEmpty()) { - return; + log.info("No winners in FCFS Event {}", fcfsEventId); + continue; } - FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(eventId)) + FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(fcfsEventId)) .orElseThrow(() -> new FcfsEventException(ErrorCode.FCFS_EVENT_NOT_FOUND)); List users = eventUserRepository.findAllByUserId(userIds.stream().toList()); List winningInfos = users .stream() - .map(user -> FcfsEventWinningInfo.of(event, user, getTimeFromScore(stringRedisTemplate.opsForZSet().score(FcfsUtil.winnerFormatting(eventId), user.getUserId())))) + .map(user -> FcfsEventWinningInfo.of(event, user, getTimeFromScore(stringRedisTemplate.opsForZSet().score(FcfsUtil.winnerFormatting(fcfsEventId), user.getUserId())))) .toList(); + log.info("Winners of FCFS event {} were registered in DB", fcfsEventId); fcfsEventWinningInfoRepository.saveAll(winningInfos); - deleteEventInfo(eventId); + deleteEventInfo(fcfsEventId); } + // PK를 간접적으로 보관하던 eventId 제거 Set eventIds = stringRedisTemplate.keys("*:eventId"); if(eventIds != null && !eventIds.isEmpty()) { for(String eventId : eventIds) { stringRedisTemplate.delete(eventId); } } - log.info("Winners of all FCFS events were registered in DB"); + log.info("Registering winners of FCFS events in DB is completed"); } // 특정 선착순 이벤트의 정보 조회