Skip to content

Commit

Permalink
fix : 로직 위치 변경 및 스케쥴링 시간 변경 (CC-176) (#50)
Browse files Browse the repository at this point in the history
* fix : 로직 위치 변경 및 스케쥴링 시간 변경 (CC-176)

* fix : import 누락 추가 (CC-176)
  • Loading branch information
putdata authored Aug 23, 2024
1 parent 063e746 commit f0aea94
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import ai.softeer.caecae.findinggame.domain.entity.FindingGame;
import ai.softeer.caecae.findinggame.domain.entity.FindingGameAnswer;
import ai.softeer.caecae.findinggame.domain.entity.FindingGameRealWinner;
import ai.softeer.caecae.findinggame.domain.entity.FindingGameWinner;
import ai.softeer.caecae.findinggame.domain.exception.FindingGameException;
import ai.softeer.caecae.findinggame.repository.FindingGameAnswerDbRepository;
import ai.softeer.caecae.findinggame.repository.FindingGameDbRepository;
Expand Down Expand Up @@ -39,6 +38,8 @@ public class FindingGamePlayService {
private final FindingGameRedisRepository findingGameRedisRepository;
private final FindingGameDbRepository findingGameDbRepository;
private final FindingGameAnswerDbRepository findingGameAnswerDbRepository;
private final FindingGameWinnerRepository findingGameWinnerRepository;
private final UserRepository userRepository;
private final Clock clock;

private static final int MAX_ANSWER_COUNT = 2;
Expand Down Expand Up @@ -163,6 +164,22 @@ public RegisterWinnerResponseDto registWinner(RegisterWinnerRequestDto req) {
.build();
}

/**
* 실제 DB에 선착순 당첨자를 기록하는 서비스 로직
*
* @param gameId
* @param phone
*/
@Transactional
protected void insertWinner(Integer gameId, String phone) {
Integer userId = userRepository.findByPhone(phone).orElseGet(() -> userRepository.save(
User.builder()
.phone(phone)
.build()
)).getId();
findingGameWinnerRepository.insertWinner(userId, gameId);
}

/**
* 캐싱된 전체 숨은그림찾기 게임을 이용하여 현재 진행중인 게임 찾기
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import ai.softeer.caecae.findinggame.domain.entity.FindingGameRealWinner;
import ai.softeer.caecae.findinggame.domain.entity.FindingGameWinner;
import ai.softeer.caecae.findinggame.repository.FindingGameDbRepository;
import ai.softeer.caecae.findinggame.repository.FindingGameRedisRepository;
import ai.softeer.caecae.findinggame.repository.FindingGameWinnerRepository;
import ai.softeer.caecae.user.domain.entity.User;
import ai.softeer.caecae.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -21,13 +16,10 @@
@RequiredArgsConstructor
public class FindingGameScheduleService {
private final FindingGameRedisRepository findingGameRedisRepository;
private final FindingGameDbRepository findingGameDbRepository;
private final FindingGameWinnerRepository findingGameWinnerRepository;
private final UserRepository userRepository;
private final FindingGamePlayService findingGamePlayService;

private final int TRANSACTION_COUNT = 100;
private final int TRANSACTION_COUNT = 50;

@Transactional
@Scheduled(cron = "*/1 * * * * *")
public void insertWinnerToDatabaseScheduler() {
int completed = 0;
Expand All @@ -38,7 +30,7 @@ public void insertWinnerToDatabaseScheduler() {
Integer gameId = realWinner.getGameId();
String phone = realWinner.getPhone();
try {
insertWinner(gameId, phone);
findingGamePlayService.insertWinner(gameId, phone);
} catch (Exception e) {
log.error("선착순 인원 처리 에러 - gameId:{}, phone:{}, 처리 완료된 건: {}", gameId, phone, completed);
return;
Expand All @@ -50,14 +42,4 @@ public void insertWinnerToDatabaseScheduler() {
log.info("선착순 인원 스케쥴링 처리 완료 - {} 건", completed);
}
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
protected void insertWinner(Integer gameId, String phone) {
Integer userId = userRepository.findByPhone(phone).orElseGet(() -> userRepository.save(
User.builder()
.phone(phone)
.build()
)).getId();
findingGameWinnerRepository.insertWinner(userId, gameId);
}
}

0 comments on commit f0aea94

Please sign in to comment.