-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 스케줄러 책임 분리 및 알림 발송 재시도 로직 추가 #77
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
35 changes: 17 additions & 18 deletions
35
src/main/java/io/wisoft/prepair/prepair_api/external/notification/email/EmailService.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,41 +1,40 @@ | ||
| package io.wisoft.prepair.prepair_api.external.notification.email; | ||
|
|
||
| import io.wisoft.prepair.prepair_api.common.exception.BusinessException; | ||
| import io.wisoft.prepair.prepair_api.common.exception.ErrorCode; | ||
| import jakarta.mail.MessagingException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.mail.MailException; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.MimeMessageHelper; | ||
| import org.springframework.retry.annotation.Backoff; | ||
| import org.springframework.retry.annotation.Retryable; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class EmailService { | ||
|
|
||
| private final EmailTemplateBuilder emailTemplateBuilder; | ||
| private final JavaMailSender mailSender; | ||
|
|
||
| public void sendInterviewQuestion(String email, String nickname, String questionTag, String question) { | ||
| @Retryable( | ||
| value = {MessagingException.class, MailException.class}, | ||
| maxAttempts = 3, | ||
| backoff = @Backoff(delay = 1000, multiplier = 2) | ||
| ) | ||
| public void send(String email, String nickname, String questionTag, String question) throws MessagingException { | ||
| String html = emailTemplateBuilder.buildInterviewQuestionHtml(nickname, questionTag, question); | ||
| send(email, "[PrePair] 오늘의 면접 질문이 도착했어요!", html); | ||
| sendMail(email, "[PrePair] 오늘의 면접 질문이 도착했어요!", html); | ||
| } | ||
|
|
||
| private void send(String to, String subject, String html) { | ||
| try { | ||
| MimeMessage message = mailSender.createMimeMessage(); | ||
| MimeMessageHelper helper = new MimeMessageHelper(message, false, "UTF-8"); | ||
| private void sendMail(String to, String subject, String html) throws MessagingException { | ||
| MimeMessage message = mailSender.createMimeMessage(); | ||
| MimeMessageHelper helper = new MimeMessageHelper(message, false, "UTF-8"); | ||
|
|
||
| helper.setTo(to); | ||
| helper.setSubject(subject); | ||
| helper.setText(html, true); | ||
| helper.setTo(to); | ||
| helper.setSubject(subject); | ||
| helper.setText(html, true); | ||
|
|
||
| mailSender.send(message); | ||
| } catch (MessagingException | MailException e) { | ||
| throw new BusinessException(ErrorCode.EMAIL_SEND_FAILED); | ||
| } | ||
| mailSender.send(message); | ||
| } | ||
| } | ||
| } |
14 changes: 10 additions & 4 deletions
14
src/main/java/io/wisoft/prepair/prepair_api/external/notification/kakao/KakaoService.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
66 changes: 66 additions & 0 deletions
66
...io/wisoft/prepair/prepair_api/interview/question/service/QuestionNotificationService.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,66 @@ | ||
| package io.wisoft.prepair.prepair_api.interview.question.service; | ||
|
|
||
| import io.wisoft.prepair.prepair_api.external.member.dto.MemberSchedulerInfo; | ||
| import io.wisoft.prepair.prepair_api.external.member.enums.Notification; | ||
| import io.wisoft.prepair.prepair_api.external.notification.email.EmailService; | ||
| import io.wisoft.prepair.prepair_api.external.notification.kakao.KakaoService; | ||
| import io.wisoft.prepair.prepair_api.interview.question.entity.InterviewQuestion; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class QuestionNotificationService { | ||
|
|
||
| private final EmailService emailService; | ||
| private final KakaoService kakaoService; | ||
|
|
||
| public void notifyMember(MemberSchedulerInfo member, InterviewQuestion question) { | ||
| Notification notification = member.notification(); | ||
|
|
||
| if (notification == Notification.EMAIL || notification == Notification.BOTH) { | ||
| sendEmail(member, question); | ||
| } | ||
|
|
||
| if (notification == Notification.KAKAO || notification == Notification.BOTH) { | ||
| sendKakao(member, question); | ||
| } | ||
| } | ||
|
|
||
| private void sendEmail(MemberSchedulerInfo member, InterviewQuestion question) { | ||
| try { | ||
| emailService.send( | ||
| member.email(), | ||
| member.nickname(), | ||
| question.getQuestionTag(), | ||
| question.getQuestion() | ||
| ); | ||
| log.info("이메일 발송 완료 | memberId={}", member.id()); | ||
| } catch (Exception e) { | ||
| log.error("이메일 발송 실패 | memberId={}", member.id(), e); | ||
| } | ||
| } | ||
|
|
||
| private void sendKakao(MemberSchedulerInfo member, InterviewQuestion question) { | ||
| if (member.kakaoAccessToken() == null || member.kakaoAccessToken().isBlank()) { | ||
| log.warn("멤버 스킵 | memberId={} | reason=no_kakao_token", member.id()); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| kakaoService.send( | ||
| member.kakaoAccessToken(), | ||
| question.getQuestion(), | ||
| question.getQuestionTag() | ||
| ); | ||
| log.info("카카오 발송 완료 | memberId={}", member.id()); | ||
| } catch (HttpClientErrorException.Unauthorized e) { | ||
| log.warn("카카오 발송 실패 | memberId={} | reason=token_expired", member.id()); | ||
| } catch (Exception e) { | ||
| log.error("카카오 발송 실패 | memberId={}", member.id(), e); | ||
| } | ||
| } | ||
| } |
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
22 changes: 13 additions & 9 deletions
22
src/main/java/io/wisoft/prepair/prepair_api/scheduler/TodayQuestionScheduler.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
Oops, something went wrong.
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.
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.
questionRepository.findByMemberId(member.id())는 해당 회원의 모든 과거 질문 데이터를 조회합니다. 서비스 사용 기간이 길어짐에 따라 조회되는 데이터 양이 많아지면 성능 저하와 메모리 부하를 초래할 수 있습니다. 프롬프트 생성에 필요한 최근 질문들만 조회하도록 제한(예:findTop10ByMemberIdOrderByCreatedAtDesc)하는 로직으로 개선이 필요합니다.