-
Notifications
You must be signed in to change notification settings - Fork 2
common: MissionCyclePolicy를 common으로 이동하여 날짜 계산 로직 통일 #167
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
Merged
ryuwldnjs
merged 1 commit into
main
from
166-refactor-common-missioncyclepolicy를-common으로-이동하여-날짜-계산-로직-통일
Feb 18, 2026
The head ref may contain hidden characters: "166-refactor-common-missioncyclepolicy\uB97C-common\uC73C\uB85C-\uC774\uB3D9\uD558\uC5EC-\uB0A0\uC9DC-\uACC4\uC0B0-\uB85C\uC9C1-\uD1B5\uC77C"
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/com/ryu/studyhelper/common/MissionCyclePolicy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.ryu.studyhelper.common; | ||
|
|
||
| import java.time.Clock; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.LocalTime; | ||
|
|
||
| /** | ||
| * 미션 사이클 도메인 정책 | ||
| * 매일 오전 6시를 기준으로 미션 사이클이 갱신된다. | ||
| */ | ||
| public class MissionCyclePolicy { | ||
|
|
||
| public static final LocalTime MISSION_RESET_TIME = LocalTime.of(6, 0); | ||
|
|
||
| /** | ||
| * 현재 미션 사이클의 시작 시각을 반환한다. | ||
| * 오전 6시 이전이면 전날 오전 6시를 반환한다. | ||
| */ | ||
| public static LocalDateTime getMissionCycleStart(Clock clock) { | ||
| LocalDateTime now = LocalDateTime.now(clock); | ||
| return toMissionDate(now).atTime(MISSION_RESET_TIME); | ||
| } | ||
|
|
||
| /** | ||
| * 주어진 시각이 속하는 미션 날짜를 반환한다. | ||
| * 오전 6시 이전이면 전날로 취급한다. | ||
| */ | ||
| public static LocalDate toMissionDate(LocalDateTime dateTime) { | ||
| if (dateTime.toLocalTime().isBefore(MISSION_RESET_TIME)) { | ||
| return dateTime.toLocalDate().minusDays(1); | ||
| } | ||
| return dateTime.toLocalDate(); | ||
| } | ||
| } |
22 changes: 0 additions & 22 deletions
22
src/main/java/com/ryu/studyhelper/recommendation/service/MissionCyclePolicy.java
This file was deleted.
Oops, something went wrong.
151 changes: 76 additions & 75 deletions
151
src/main/java/com/ryu/studyhelper/recommendation/service/RecommendationEmailService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,94 +1,95 @@ | ||
| package com.ryu.studyhelper.recommendation.service; | ||
| package com.ryu.studyhelper.recommendation.service; | ||
|
|
||
| import com.ryu.studyhelper.recommendation.dto.internal.BatchResult; | ||
| import com.ryu.studyhelper.infrastructure.mail.sender.MailSender; | ||
| import com.ryu.studyhelper.recommendation.domain.member.EmailSendStatus; | ||
| import com.ryu.studyhelper.recommendation.domain.member.MemberRecommendation; | ||
| import com.ryu.studyhelper.recommendation.mailbuilder.RecommendationMailBuilder; | ||
| import com.ryu.studyhelper.recommendation.repository.MemberRecommendationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import com.ryu.studyhelper.common.MissionCyclePolicy; | ||
| import com.ryu.studyhelper.recommendation.dto.internal.BatchResult; | ||
| import com.ryu.studyhelper.infrastructure.mail.sender.MailSender; | ||
| import com.ryu.studyhelper.recommendation.domain.member.EmailSendStatus; | ||
| import com.ryu.studyhelper.recommendation.domain.member.MemberRecommendation; | ||
| import com.ryu.studyhelper.recommendation.mailbuilder.RecommendationMailBuilder; | ||
| import com.ryu.studyhelper.recommendation.repository.MemberRecommendationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.time.Clock; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.time.Clock; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 추천 이메일 발송 | ||
| * 배치(sendAll)와 수동(send) 모두 담당 | ||
| */ | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| @Slf4j | ||
| public class RecommendationEmailService { | ||
|
|
||
| private final Clock clock; | ||
| private final MailSender mailSender; | ||
| private final RecommendationMailBuilder recommendationMailBuilder; | ||
| private final MemberRecommendationRepository memberRecommendationRepository; | ||
| /** | ||
| * 추천 이메일 발송 | ||
| * 배치(sendAll)와 수동(send) 모두 담당 | ||
| */ | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| @Slf4j | ||
| public class RecommendationEmailService { | ||
|
|
||
| /** | ||
| * 배치: PENDING 상태의 추천들에 대해 이메일 발송 | ||
| * 미션 사이클 기준(06:00~06:00)으로 조회 | ||
| */ | ||
| public BatchResult sendAll() { | ||
| LocalDateTime now = LocalDateTime.now(clock); | ||
| LocalDateTime missionCycleStart = MissionCyclePolicy.getMissionCycleStart(clock); | ||
| log.info("이메일 발송 배치 시작: {} (미션 사이클: {} 06:00 ~)", now.toLocalDate(), missionCycleStart.toLocalDate()); | ||
| private final Clock clock; | ||
| private final MailSender mailSender; | ||
| private final RecommendationMailBuilder recommendationMailBuilder; | ||
| private final MemberRecommendationRepository memberRecommendationRepository; | ||
|
|
||
| List<MemberRecommendation> pendingRecommendations = memberRecommendationRepository | ||
| .findPendingRecommendationsByCreatedAtBetween(missionCycleStart, now, EmailSendStatus.PENDING); | ||
|
|
||
| int successCount = 0; | ||
| int failCount = 0; | ||
| /** | ||
| * 배치: PENDING 상태의 추천들에 대해 이메일 발송 | ||
| * 미션 사이클 기준(06:00~06:00)으로 조회 | ||
| */ | ||
| public BatchResult sendAll() { | ||
| LocalDateTime now = LocalDateTime.now(clock); | ||
| LocalDateTime missionCycleStart = MissionCyclePolicy.getMissionCycleStart(clock); | ||
| log.info("이메일 발송 배치 시작: {} (미션 사이클: {} 06:00 ~)", now.toLocalDate(), missionCycleStart.toLocalDate()); | ||
|
|
||
| for (MemberRecommendation mr : pendingRecommendations) { | ||
| if (sendEmail(mr)) { | ||
| successCount++; | ||
| } else { | ||
| failCount++; | ||
| } | ||
| } | ||
| List<MemberRecommendation> pendingRecommendations = memberRecommendationRepository | ||
| .findPendingRecommendationsByCreatedAtBetween(missionCycleStart, now, EmailSendStatus.PENDING); | ||
|
|
||
| log.info("이메일 발송 배치 완료 - 대상: {}개, 성공: {}개, 실패: {}개", | ||
| pendingRecommendations.size(), successCount, failCount); | ||
| return new BatchResult(pendingRecommendations.size(), successCount, failCount); | ||
| } | ||
| int successCount = 0; | ||
| int failCount = 0; | ||
|
|
||
| /** | ||
| * 수동 추천: 해당 추천의 팀원들에게 이메일 즉시 발송 | ||
| */ | ||
| public void send(List<MemberRecommendation> memberRecommendations) { | ||
| for (MemberRecommendation mr : memberRecommendations) { | ||
| sendEmail(mr); | ||
| for (MemberRecommendation mr : pendingRecommendations) { | ||
| if (sendEmail(mr)) { | ||
| successCount++; | ||
| } else { | ||
| failCount++; | ||
| } | ||
| } | ||
|
|
||
| private boolean sendEmail(MemberRecommendation mr) { | ||
| try { | ||
| String email = mr.getMember().getEmail(); | ||
| if (email == null || email.isBlank()) { | ||
| mr.markEmailAsFailed(); | ||
| memberRecommendationRepository.save(mr); | ||
| log.warn("회원 ID {}에 이메일이 없습니다", mr.getMember().getId()); | ||
| return false; | ||
| } | ||
|
|
||
| mailSender.send(recommendationMailBuilder.build(mr)); | ||
| log.info("이메일 발송 배치 완료 - 대상: {}개, 성공: {}개, 실패: {}개", | ||
| pendingRecommendations.size(), successCount, failCount); | ||
| return new BatchResult(pendingRecommendations.size(), successCount, failCount); | ||
| } | ||
|
|
||
| mr.markEmailAsSent(); | ||
| memberRecommendationRepository.save(mr); | ||
| log.debug("회원 '{}' 이메일 발송 완료", mr.getMember().getHandle()); | ||
| return true; | ||
| /** | ||
| * 수동 추천: 해당 추천의 팀원들에게 이메일 즉시 발송 | ||
| */ | ||
| public void send(List<MemberRecommendation> memberRecommendations) { | ||
| for (MemberRecommendation mr : memberRecommendations) { | ||
| sendEmail(mr); | ||
| } | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| private boolean sendEmail(MemberRecommendation mr) { | ||
| try { | ||
| String email = mr.getMember().getEmail(); | ||
| if (email == null || email.isBlank()) { | ||
| mr.markEmailAsFailed(); | ||
| memberRecommendationRepository.save(mr); | ||
| log.error("회원 ID {} 이메일 발송 실패", mr.getMember().getId(), e); | ||
| log.warn("회원 ID {}에 이메일이 없습니다", mr.getMember().getId()); | ||
| return false; | ||
| } | ||
|
|
||
| mailSender.send(recommendationMailBuilder.build(mr)); | ||
|
|
||
| mr.markEmailAsSent(); | ||
| memberRecommendationRepository.save(mr); | ||
| log.debug("회원 '{}' 이메일 발송 완료", mr.getMember().getHandle()); | ||
| return true; | ||
|
|
||
| } catch (Exception e) { | ||
| mr.markEmailAsFailed(); | ||
| memberRecommendationRepository.save(mr); | ||
| log.error("회원 ID {} 이메일 발송 실패", mr.getMember().getId(), e); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
src/main/java/com/ryu/studyhelper/recommendation/service/RecommendationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/java/com/ryu/studyhelper/recommendation/service/ScheduledRecommendationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catch 블록 내
save()호출이 실패할 경우 예외가 전파됩니다.Line 90의
memberRecommendationRepository.save(mr)가 DB 장애 등으로 실패하면, 이 예외는 catch 블록 밖으로 전파되어sendAll()의 나머지 처리를 중단시킵니다. 클래스 레벨@Transactional과 결합하면 이전 성공 건까지 모두 롤백될 수 있습니다.🛡️ catch 블록 내 save 보호 제안
} catch (Exception e) { - mr.markEmailAsFailed(); - memberRecommendationRepository.save(mr); - log.error("회원 ID {} 이메일 발송 실패", mr.getMember().getId(), e); + try { + mr.markEmailAsFailed(); + memberRecommendationRepository.save(mr); + } catch (Exception saveEx) { + log.error("회원 ID {} 실패 상태 저장 중 오류", mr.getMember().getId(), saveEx); + } + log.error("회원 ID {} 이메일 발송 실패", mr.getMember().getId(), e); return false; }📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추후 별도 이슈에서 해결예정
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryuwldnjs, 네, 이해했습니다! 해당 내용은 추후 별도 이슈로 처리하는 것으로 확인했습니다.필요하시다면 제가 이 내용을 추적할 수 있도록 GitHub 이슈를 생성해드릴 수 있습니다. 이슈 생성을 원하시면 말씀해주세요! 😊
🧠 Learnings used