Skip to content

Commit dac136a

Browse files
authored
Refactor/311 : 메일 발송 실패 트랜잭션 별도 관리 (#423)
* feat : 메일 로그 발송 실패 로그 트랜잭션 분리 코드 추가 * feat : 추가한 서비스 코드 AOP에 적용 * feat : 메일 발송 성공 서비스 코드 추가 * feat : 메일 성공 로그 저장 메서드 적용 * feat : 메일 발송 성공 로그 저장 실패 예외 처리 추가
1 parent 1e3a773 commit dac136a

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

cs25-batch/src/main/java/com/example/cs25batch/aop/MailLogAspect.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.example.cs25batch.adapter.RedisStreamsClient;
44
import com.example.cs25batch.batch.dto.MailDto;
5+
import com.example.cs25batch.batch.service.MailLogBatchService;
56
import com.example.cs25entity.domain.mail.entity.MailLog;
67
import com.example.cs25entity.domain.mail.enums.MailStatus;
78
import com.example.cs25entity.domain.mail.exception.CustomMailException;
@@ -26,6 +27,7 @@
2627
@RequiredArgsConstructor
2728
public class MailLogAspect {
2829

30+
private final MailLogBatchService mailLogBatchService;
2931
private final MailLogRepository mailLogRepository;
3032
private final RedisStreamsClient redisClient;
3133

@@ -47,19 +49,19 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable {
4749
} catch (Exception e){
4850
status = MailStatus.FAILED;
4951
caused = e.getMessage();
52+
mailLogBatchService.saveFailLog(subscription, quiz, LocalDateTime.now(), caused);
5053
throw new CustomMailException(MailExceptionCode.EMAIL_SEND_FAILED_ERROR);
5154
} finally {
52-
MailLog mailLog = MailLog.builder()
53-
.subscription(subscription)
54-
.quiz(quiz)
55-
.sendDate(LocalDateTime.now())
56-
.status(status)
57-
.caused(caused)
58-
.build();
59-
60-
mailLogRepository.save(mailLog);
61-
mailLogRepository.flush();
62-
55+
// MailLog mailLog = MailLog.builder()
56+
// .subscription(subscription)
57+
// .quiz(quiz)
58+
// .sendDate(LocalDateTime.now())
59+
// .status(status)
60+
// .caused(caused)
61+
// .build();
62+
//
63+
// mailLogRepository.save(mailLog);
64+
// mailLogRepository.flush();
6365
if (status == MailStatus.FAILED) {
6466
log.info("메일 발송 실패 : subscriptionId - {}, cause - {}", subscription.getId(), caused);
6567
Map<String, String> retryMessage = Map.of(
@@ -69,6 +71,14 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable {
6971
);
7072
redisClient.addDlq("quiz-email-retry-stream", retryMessage);
7173
}
74+
else if (status == MailStatus.SENT){
75+
try {
76+
mailLogBatchService.saveSuccessLog(subscription, quiz, LocalDateTime.now());
77+
} catch (Exception logEx) {
78+
log.warn("메일 성공 로그 저장 실패: subId={}, quizId={}",
79+
subscription.getId(), quiz.getId(), logEx);
80+
}
81+
}
7282
}
7383
}
7484
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example.cs25batch.batch.service;
2+
3+
import com.example.cs25entity.domain.mail.entity.MailLog;
4+
import com.example.cs25entity.domain.mail.enums.MailStatus;
5+
import com.example.cs25entity.domain.mail.repository.MailLogRepository;
6+
import com.example.cs25entity.domain.quiz.entity.Quiz;
7+
import com.example.cs25entity.domain.subscription.entity.Subscription;
8+
import java.time.LocalDateTime;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Propagation;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
@Service
15+
@RequiredArgsConstructor
16+
public class MailLogBatchService {
17+
18+
private final MailLogRepository mailLogRepository;
19+
20+
@Transactional(propagation = Propagation.REQUIRES_NEW)
21+
public void saveFailLog(Subscription subscription,
22+
Quiz quiz,
23+
LocalDateTime sendDateTime,
24+
String cause) {
25+
MailLog log = MailLog.builder()
26+
.subscription(subscription)
27+
.quiz(quiz)
28+
.sendDate(sendDateTime)
29+
.status(MailStatus.FAILED)
30+
.caused(cause)
31+
.build();
32+
mailLogRepository.save(log);
33+
}
34+
35+
@Transactional
36+
public void saveSuccessLog(Subscription subscription,
37+
Quiz quiz,
38+
LocalDateTime sendDateTime) {
39+
mailLogRepository.save(MailLog.builder()
40+
.subscription(subscription)
41+
.quiz(quiz)
42+
.sendDate(sendDateTime)
43+
.status(MailStatus.SENT)
44+
.caused(null)
45+
.build());
46+
}
47+
}

0 commit comments

Comments
 (0)