|
| 1 | +package com.threestar.trainus.domain.lesson.issue; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 9 | +import org.springframework.context.annotation.Profile; |
| 10 | +import org.springframework.data.redis.core.StringRedisTemplate; |
| 11 | +import org.springframework.scheduling.annotation.Scheduled; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | + |
| 17 | +@Profile("consumer & legacy") |
| 18 | +@Slf4j |
| 19 | +@Component |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class LegacyLessonAdmissionScheduler { |
| 22 | + |
| 23 | + @Qualifier("coreRedisTemplate") |
| 24 | + private final StringRedisTemplate coreRedisTemplate; |
| 25 | + |
| 26 | + @Qualifier("mqRedisTemplate") |
| 27 | + private final StringRedisTemplate mqRedisTemplate; |
| 28 | + |
| 29 | + private final LessonWaitingRoomService waitingRoomService; |
| 30 | + |
| 31 | + private static final long ADMIT_BATCH_SIZE = 1000L; |
| 32 | + |
| 33 | + @Scheduled(fixedDelay = 500) |
| 34 | + public void admitUsers() { |
| 35 | + Set<String> activeLessonIds = coreRedisTemplate.opsForSet().members(LessonApplyStreamConstant.DIRTY_SET_KEY); |
| 36 | + |
| 37 | + if (activeLessonIds == null || activeLessonIds.isEmpty()) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + for (String lessonIdStr : activeLessonIds) { |
| 42 | + Long lessonId = Long.parseLong(lessonIdStr); |
| 43 | + processAdmissionForLesson(lessonId); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private void processAdmissionForLesson(Long lessonId) { |
| 48 | + // 해당 레슨 대기열에서 인원 추출 |
| 49 | + Set<String> requestIds = waitingRoomService.dequeue(lessonId, ADMIT_BATCH_SIZE); |
| 50 | + |
| 51 | + if (requestIds.isEmpty()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // MGET 방식으로 벌크 GET |
| 56 | + List<String> statusKeys = requestIds.stream().map(id -> LessonApplyStreamConstant.STATUS_PREFIX + id).toList(); |
| 57 | + List<String> statusInfos = mqRedisTemplate.opsForValue().multiGet(statusKeys); |
| 58 | + |
| 59 | + if (statusInfos == null) |
| 60 | + return; |
| 61 | + |
| 62 | + // 상태 변경 및 스트림 추가 |
| 63 | + for (int i = 0; i < statusKeys.size(); i++) { |
| 64 | + String info = statusInfos.get(i); |
| 65 | + if (info == null) |
| 66 | + continue; |
| 67 | + |
| 68 | + String[] parts = info.split(":"); |
| 69 | + if (parts.length < 3) |
| 70 | + continue; |
| 71 | + |
| 72 | + String requestId = requestIds.toArray(new String[0])[i]; |
| 73 | + Long userId = Long.parseLong(parts[2]); |
| 74 | + Long originalTimestamp = parts.length >= 4 ? Long.parseLong(parts[3]) : System.currentTimeMillis(); |
| 75 | + |
| 76 | + // 상태 변경 |
| 77 | + String statusKey = statusKeys.get(i); |
| 78 | + String processingInfo = String.format("%s:%d:%d:%d", LessonApplyStreamConstant.STATUS_PROCESSING, lessonId, |
| 79 | + userId, originalTimestamp); |
| 80 | + mqRedisTemplate.opsForValue() |
| 81 | + .set(statusKey, processingInfo, |
| 82 | + java.time.Duration.ofMinutes(LessonApplyStreamConstant.STATUS_TTL_MINUTE)); |
| 83 | + |
| 84 | + // 스트림 추가 |
| 85 | + Map<String, String> content = new HashMap<>(); |
| 86 | + content.put("lessonId", String.valueOf(lessonId)); |
| 87 | + content.put("userId", String.valueOf(userId)); |
| 88 | + content.put("requestId", requestId); |
| 89 | + content.put("timestamp", String.valueOf(originalTimestamp)); |
| 90 | + mqRedisTemplate.opsForStream().add(LessonApplyStreamConstant.STREAM_KEY, content); |
| 91 | + } |
| 92 | + |
| 93 | + log.info("[Legacy] Admitted {} users to MQ via iteration for lesson: {}", requestIds.size(), lessonId); |
| 94 | + } |
| 95 | +} |
0 commit comments