Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 퀴즈방 생성 이후 10분간 status 가 ready인 방 정리 #531

Merged
merged 2 commits into from
May 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,4 +24,6 @@ public interface QuizRoomRepository extends JpaRepository<QuizRoom, Long> {
@Modifying
@Query("UPDATE QuizRoom q SET q.status = :status WHERE q.id = :id")
void updateStatusById(Long id, Status status);

List<QuizRoom> findByStatusAndCreatedAtBefore(Status status, LocalDateTime createdAt);
}
Original file line number Diff line number Diff line change
@@ -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("스케쥴러 종료");
}
}
Loading