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

#152 [fix] 스케줄링 관련 버그 해결 #153

Merged
merged 1 commit into from
Jan 16, 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
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;
}
}