Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@
@RequiredArgsConstructor
public class MailLogAspect {

private final MailLogBatchService mailLogBatchService;
private final MailLogRepository mailLogRepository;
private final RedisStreamsClient redisClient;

Expand All @@ -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<String, String> retryMessage = Map.of(
Expand All @@ -69,6 +71,9 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable {
);
redisClient.addDlq("quiz-email-retry-stream", retryMessage);
}
else if (status == MailStatus.SENT){
mailLogBatchService.saveSuccessLog(subscription, quiz, LocalDateTime.now());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}