Skip to content

Commit

Permalink
#152 [fix] 스케줄링 관련 버그 해결
Browse files Browse the repository at this point in the history
#152 [fix] 스케줄링 관련 버그 해결
  • Loading branch information
sohyundoh authored Jan 16, 2024
2 parents a53c934 + 2f44e2b commit 325d264
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
2 changes: 2 additions & 0 deletions module-api/src/main/java/com/mile/ApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableAspectJAutoProxy
@EnableScheduling
@EnableFeignClients
@SpringBootApplication
@ImportAutoConfiguration(FeignAutoConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum ErrorMessage {
KEYWORD_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 글모임의 글감 키워드가 존재하지 않습니다."),
WRITERS_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 모임의 작가가 요청한 개수 이상 존재하지 않습니다"),
WRITER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 작가는 존재하지 않습니다."),
RECCOMEND_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "추천 글감을 받아오는데 실패했습니다."),
RECOMMEND_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "추천 글감을 받아오는데 실패했습니다."),
MOIM_TOPIC_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 모임의 글감이 존재하지 않습니다."),
TOPIC_POST_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 글감의 글이 존재하지 않습니다."),
MOIM_POST_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 모임의 글이 존재하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,25 @@
public class RecommendService {
private final RecommendRepository recommendRepository;
private static Long GROUND_ID = 0L;
private static final int INDEX = 1;
private static String recommendContent = "우정과 사랑";
private static final Long INDEX = 1L;

public RecommendResponse getRandomRecommendation() {
return RecommendResponse.of(getRandomContentDaily());
}

private String getRandomContentDaily() {
return recommendContent;
return RecommendResponse.of(
recommendRepository.findById(GROUND_ID).orElseGet(() -> {
resetGroundId();
return recommendRepository.findById(GROUND_ID).orElseThrow(
() -> new NotFoundException(ErrorMessage.RECOMMEND_NOT_FOUND)
);
}
).getContent());
}

@Scheduled(cron = "0 0 0 * * *")
private void setRandomContentDaily() {
recommendContent = recommendRepository.findById(increaseId()).orElseGet(() -> {
resetGroundId();
return recommendRepository.findById(increaseId()).orElseThrow(
() -> new NotFoundException(ErrorMessage.RECCOMEND_NOT_FOUND)
);
}).getContent();
}

private Long increaseId() {
private void increaseId() {
GROUND_ID += INDEX;
return GROUND_ID;
}

private void resetGroundId() {
GROUND_ID = 0L;
GROUND_ID = 1L;
}
}

0 comments on commit 325d264

Please sign in to comment.