Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/be/sportizebe/SportizeBeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableJpaAuditing
@EnableScheduling
@SpringBootApplication
public class SportizeBeApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.be.sportizebe.domain.match.entity.MatchRoom;
import com.be.sportizebe.domain.match.repository.projection.MatchNearProjection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

Expand Down Expand Up @@ -44,4 +45,24 @@ List<MatchNearProjection> findNear(
@Param("radiusM") int radiusM,
@Param("sportsName") String sportsName
);

// scheduledAt이 지난 OPEN/FULL → CLOSED
@Modifying
@Query(value = """
UPDATE match_rooms
SET status = 'CLOSED'
WHERE status IN ('OPEN', 'FULL')
AND scheduled_at <= NOW()
""", nativeQuery = true)
int closeStartedMatches();

// scheduledAt + durationMinutes가 지난 CLOSED → COMPLETED
@Modifying
@Query(value = """
UPDATE match_rooms
SET status = 'COMPLETED'
WHERE status = 'CLOSED'
AND scheduled_at + (duration_minutes * interval '1 minute') <= NOW()
""", nativeQuery = true)
int completeFinishedMatches();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.be.sportizebe.domain.match.scheduler;

import com.be.sportizebe.domain.match.repository.MatchRoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@RequiredArgsConstructor
public class MatchStatusScheduler {

private final MatchRoomRepository matchRoomRepository;

/**
* 1분마다 실행
* 1) scheduledAt 도달 → OPEN/FULL → CLOSED
* 2) scheduledAt + durationMinutes 도달 → CLOSED → COMPLETED
*/
@Scheduled(fixedRate = 60_000)
@Transactional
public void updateMatchStatuses() {
int closed = matchRoomRepository.closeStartedMatches();
int completed = matchRoomRepository.completeFinishedMatches();

if (closed > 0 || completed > 0) {
log.info("[MatchScheduler] CLOSED: {}건, COMPLETED: {}건", closed, completed);
}
}
}