22
33import java .util .Optional ;
44
5+ import com .threestar .trainus .domain .lesson .teacher .repository .LessonParticipantRepository ;
56import org .springframework .beans .factory .annotation .Qualifier ;
67import org .springframework .context .annotation .Profile ;
78import org .springframework .data .redis .connection .stream .StreamInfo ;
89import org .springframework .data .redis .core .StringRedisTemplate ;
910import org .springframework .scheduling .annotation .Scheduled ;
1011import org .springframework .stereotype .Component ;
12+ import org .springframework .transaction .annotation .Transactional ;
1113
1214import net .javacrumbs .shedlock .spring .annotation .SchedulerLock ;
1315
2426public class LessonStockReconciliationScheduler {
2527
2628 private final LessonRepository lessonRepository ;
29+ private final LessonParticipantRepository lessonParticipantRepository ;
2730 private final LessonApplyProducer lessonApplyProducer ;
2831
2932 @ Qualifier ("coreRedisTemplate" )
@@ -32,6 +35,7 @@ public class LessonStockReconciliationScheduler {
3235 @ Qualifier ("mqRedisTemplate" )
3336 private final StringRedisTemplate mqRedisTemplate ;
3437
38+ @ Transactional
3539 @ Scheduled (fixedRate = 30000 )
3640 @ SchedulerLock (name = "LessonStockReconciliation" , lockAtMostFor = "25s" , lockAtLeastFor = "20s" )
3741 public void reconcileStock () {
@@ -41,7 +45,7 @@ public void reconcileStock() {
4145 return ;
4246 }
4347
44- log .info ("Starting Smart Stock Reconciliation (DB -> Redis)..." );
48+ log .info ("Starting Smart Stock Reconciliation (Actual DB Count -> Lesson Row -> Redis)..." );
4549
4650 String dirtySetKey = LessonApplyStreamConstant .DIRTY_SET_KEY ;
4751
@@ -57,7 +61,14 @@ public void reconcileStock() {
5761 try {
5862 Long lessonId = Long .valueOf (lessonIdStr );
5963
60- // DB 카운트 조회
64+ // 실제 참여자 수 계산 (DB)
65+ long actualParticipantCount = lessonParticipantRepository .countByLessonId (lessonId );
66+
67+ // Lesson 테이블 카운트 보정 및 상태 변경
68+ lessonRepository .updateParticipantCount (lessonId , (int )actualParticipantCount ,
69+ com .threestar .trainus .domain .lesson .teacher .entity .LessonStatus .RECRUITMENT_COMPLETED );
70+
71+ // 최신 레슨 정보 조회하여 Redis 동기화
6172 Optional <Lesson > lessonOpt = lessonRepository .findById (lessonId );
6273 if (lessonOpt .isPresent ()) {
6374 Lesson lesson = lessonOpt .get ();
@@ -74,7 +85,8 @@ public void reconcileStock() {
7485 coreRedisTemplate .opsForSet ().remove (dirtySetKey , lessonIdStr );
7586
7687 processedCount ++;
77- log .debug ("Reconciled lesson [{}] stock to [{}]" , lessonId , currentStock );
88+ log .debug ("Reconciled lesson [{}] to actual count [{}] and stock [{}]" ,
89+ lessonId , actualParticipantCount , currentStock );
7890 }
7991 } catch (Exception e ) {
8092 log .error ("Failed to reconcile lesson [{}]: {}" , lessonIdStr , e .getMessage ());
0 commit comments