Skip to content

Commit 1661b7e

Browse files
안훈기안훈기
authored andcommitted
✨Feat: 매칭 상태 자동 전환 Scheduler 구현
1 parent 9875e28 commit 1661b7e

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/main/java/com/be/sportizebe/SportizeBeApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
67

78
@EnableJpaAuditing
9+
@EnableScheduling
810
@SpringBootApplication
911
public class SportizeBeApplication {
1012

src/main/java/com/be/sportizebe/domain/match/repository/MatchRoomRepository.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.be.sportizebe.domain.match.entity.MatchRoom;
44
import com.be.sportizebe.domain.match.repository.projection.MatchNearProjection;
55
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Modifying;
67
import org.springframework.data.jpa.repository.Query;
78
import org.springframework.data.repository.query.Param;
89

@@ -44,4 +45,24 @@ List<MatchNearProjection> findNear(
4445
@Param("radiusM") int radiusM,
4546
@Param("sportsName") String sportsName
4647
);
48+
49+
// scheduledAt이 지난 OPEN/FULL → CLOSED
50+
@Modifying
51+
@Query(value = """
52+
UPDATE match_rooms
53+
SET status = 'CLOSED'
54+
WHERE status IN ('OPEN', 'FULL')
55+
AND scheduled_at <= NOW()
56+
""", nativeQuery = true)
57+
int closeStartedMatches();
58+
59+
// scheduledAt + durationMinutes가 지난 CLOSED → COMPLETED
60+
@Modifying
61+
@Query(value = """
62+
UPDATE match_rooms
63+
SET status = 'COMPLETED'
64+
WHERE status = 'CLOSED'
65+
AND scheduled_at + (duration_minutes * interval '1 minute') <= NOW()
66+
""", nativeQuery = true)
67+
int completeFinishedMatches();
4768
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.be.sportizebe.domain.match.scheduler;
2+
3+
import com.be.sportizebe.domain.match.repository.MatchRoomRepository;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.scheduling.annotation.Scheduled;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Slf4j
11+
@Component
12+
@RequiredArgsConstructor
13+
public class MatchStatusScheduler {
14+
15+
private final MatchRoomRepository matchRoomRepository;
16+
17+
/**
18+
* 1분마다 실행
19+
* 1) scheduledAt 도달 → OPEN/FULL → CLOSED
20+
* 2) scheduledAt + durationMinutes 도달 → CLOSED → COMPLETED
21+
*/
22+
@Scheduled(fixedRate = 60_000)
23+
@Transactional
24+
public void updateMatchStatuses() {
25+
int closed = matchRoomRepository.closeStartedMatches();
26+
int completed = matchRoomRepository.completeFinishedMatches();
27+
28+
if (closed > 0 || completed > 0) {
29+
log.info("[MatchScheduler] CLOSED: {}건, COMPLETED: {}건", closed, completed);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)