diff --git a/cs25-batch/src/main/java/com/example/cs25batch/aop/MailLogAspect.java b/cs25-batch/src/main/java/com/example/cs25batch/aop/MailLogAspect.java index 27cc666f..d8c1ec3c 100644 --- a/cs25-batch/src/main/java/com/example/cs25batch/aop/MailLogAspect.java +++ b/cs25-batch/src/main/java/com/example/cs25batch/aop/MailLogAspect.java @@ -2,6 +2,7 @@ import com.example.cs25batch.adapter.RedisStreamsClient; import com.example.cs25batch.batch.dto.MailDto; +import com.example.cs25batch.batch.service.MailLogBatchService; import com.example.cs25entity.domain.mail.entity.MailLog; import com.example.cs25entity.domain.mail.enums.MailStatus; import com.example.cs25entity.domain.mail.exception.CustomMailException; @@ -26,6 +27,7 @@ @RequiredArgsConstructor public class MailLogAspect { + private final MailLogBatchService mailLogBatchService; private final MailLogRepository mailLogRepository; private final RedisStreamsClient redisClient; @@ -47,19 +49,19 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable { } catch (Exception e){ status = MailStatus.FAILED; caused = e.getMessage(); + mailLogBatchService.saveFailLog(subscription, quiz, LocalDateTime.now(), caused); throw new CustomMailException(MailExceptionCode.EMAIL_SEND_FAILED_ERROR); } finally { - MailLog mailLog = MailLog.builder() - .subscription(subscription) - .quiz(quiz) - .sendDate(LocalDateTime.now()) - .status(status) - .caused(caused) - .build(); - - mailLogRepository.save(mailLog); - mailLogRepository.flush(); - +// MailLog mailLog = MailLog.builder() +// .subscription(subscription) +// .quiz(quiz) +// .sendDate(LocalDateTime.now()) +// .status(status) +// .caused(caused) +// .build(); +// +// mailLogRepository.save(mailLog); +// mailLogRepository.flush(); if (status == MailStatus.FAILED) { log.info("메일 발송 실패 : subscriptionId - {}, cause - {}", subscription.getId(), caused); Map retryMessage = Map.of( @@ -69,6 +71,14 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable { ); redisClient.addDlq("quiz-email-retry-stream", retryMessage); } + else if (status == MailStatus.SENT){ + try { + mailLogBatchService.saveSuccessLog(subscription, quiz, LocalDateTime.now()); + } catch (Exception logEx) { + log.warn("메일 성공 로그 저장 실패: subId={}, quizId={}", + subscription.getId(), quiz.getId(), logEx); + } + } } } } \ No newline at end of file diff --git a/cs25-batch/src/main/java/com/example/cs25batch/batch/service/MailLogBatchService.java b/cs25-batch/src/main/java/com/example/cs25batch/batch/service/MailLogBatchService.java new file mode 100644 index 00000000..f9b74a1f --- /dev/null +++ b/cs25-batch/src/main/java/com/example/cs25batch/batch/service/MailLogBatchService.java @@ -0,0 +1,47 @@ +package com.example.cs25batch.batch.service; + +import com.example.cs25entity.domain.mail.entity.MailLog; +import com.example.cs25entity.domain.mail.enums.MailStatus; +import com.example.cs25entity.domain.mail.repository.MailLogRepository; +import com.example.cs25entity.domain.quiz.entity.Quiz; +import com.example.cs25entity.domain.subscription.entity.Subscription; +import java.time.LocalDateTime; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class MailLogBatchService { + + private final MailLogRepository mailLogRepository; + + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void saveFailLog(Subscription subscription, + Quiz quiz, + LocalDateTime sendDateTime, + String cause) { + MailLog log = MailLog.builder() + .subscription(subscription) + .quiz(quiz) + .sendDate(sendDateTime) + .status(MailStatus.FAILED) + .caused(cause) + .build(); + mailLogRepository.save(log); + } + + @Transactional + public void saveSuccessLog(Subscription subscription, + Quiz quiz, + LocalDateTime sendDateTime) { + mailLogRepository.save(MailLog.builder() + .subscription(subscription) + .quiz(quiz) + .sendDate(sendDateTime) + .status(MailStatus.SENT) + .caused(null) + .build()); + } +}