diff --git a/chatty-be/src/main/java/com/chatty/chatty/ChattyApplication.java b/chatty-be/src/main/java/com/chatty/chatty/ChattyApplication.java index e8a887798..5289f486b 100644 --- a/chatty-be/src/main/java/com/chatty/chatty/ChattyApplication.java +++ b/chatty-be/src/main/java/com/chatty/chatty/ChattyApplication.java @@ -8,7 +8,9 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +@EnableScheduling @EnableAsync @EnableFeignClients @EnableJpaAuditing diff --git a/chatty-be/src/main/java/com/chatty/chatty/quizroom/repository/QuizRoomRepository.java b/chatty-be/src/main/java/com/chatty/chatty/quizroom/repository/QuizRoomRepository.java index e3562da8b..6b674960e 100644 --- a/chatty-be/src/main/java/com/chatty/chatty/quizroom/repository/QuizRoomRepository.java +++ b/chatty-be/src/main/java/com/chatty/chatty/quizroom/repository/QuizRoomRepository.java @@ -2,6 +2,8 @@ import com.chatty.chatty.quizroom.entity.QuizRoom; import com.chatty.chatty.quizroom.entity.Status; +import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -22,4 +24,6 @@ public interface QuizRoomRepository extends JpaRepository { @Modifying @Query("UPDATE QuizRoom q SET q.status = :status WHERE q.id = :id") void updateStatusById(Long id, Status status); + + List findByStatusAndCreatedAtBefore(Status status, LocalDateTime createdAt); } diff --git a/chatty-be/src/main/java/com/chatty/chatty/quizroom/service/QuizRoomScheduler.java b/chatty-be/src/main/java/com/chatty/chatty/quizroom/service/QuizRoomScheduler.java new file mode 100644 index 000000000..c419616a8 --- /dev/null +++ b/chatty-be/src/main/java/com/chatty/chatty/quizroom/service/QuizRoomScheduler.java @@ -0,0 +1,29 @@ +package com.chatty.chatty.quizroom.service; + +import com.chatty.chatty.quizroom.entity.Status; +import com.chatty.chatty.quizroom.repository.QuizRoomRepository; +import java.time.LocalDateTime; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +@Slf4j +public class QuizRoomScheduler { + + private final QuizRoomService quizRoomService; + private final QuizRoomRepository quizRoomRepository; + + @Scheduled(fixedDelay = 1000 * 60 * 10) + public void checkRoomStatusAfterTimeout() { + log.info("스케쥴러 실행"); + quizRoomRepository.findByStatusAndCreatedAtBefore(Status.READY, LocalDateTime.now().minusMinutes(10)) + .forEach(quizRoom -> { + log.info("{}번 퀴즈방 시간초과 -> 스케쥴러 실행", quizRoom.getId()); + quizRoomService.finishRoom(quizRoom.getId()); + }); + log.info("스케쥴러 종료"); + } +}