261022_01_ENH: 예약 발송 취소 구현, record로 전환 가능한 Class Dto record로 전환, KKO 템플릿 등록시 사용 준비도 등록가능 템플릿으로 지정
This commit is contained in:
@@ -2,6 +2,7 @@ package com.kri.notihub.server.scheduler;
|
||||
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.notihub.server.common.consts.NotihubConst;
|
||||
import com.kri.notihub.server.common.enums.SendStatusType;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.common.external.pringo.dto.PringoSendRequestDto;
|
||||
import com.kri.notihub.server.dto.send.DestInfoDto;
|
||||
@@ -9,30 +10,29 @@ import com.kri.notihub.server.dto.send.SendInternalResultDto;
|
||||
import com.kri.notihub.server.persistence.entity.TSendEntity;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.service.SendEmailService;
|
||||
import com.kri.notihub.server.service.SendResultService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SendReservationEmailScheduler {
|
||||
|
||||
private final SendEmailService sendEmlService;
|
||||
private final SendRepository sendRepository;
|
||||
private final SendResultService resultService;
|
||||
|
||||
@Scheduled(cron = "${kri.email.reservation-send.cron:0 */5 * * * *}")
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
public void sendReservationEmail() {
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
|
||||
while (true) {
|
||||
List<TSendEntity> updatedSends = new ArrayList<>();
|
||||
|
||||
List<TSendEntity> reservationList = sendRepository.findReservedSendEmail(
|
||||
SendType.EMAIL.getCode(),
|
||||
NotihubConst.EMAIL_STANDBY,
|
||||
@@ -45,30 +45,28 @@ public class SendReservationEmailScheduler {
|
||||
}
|
||||
|
||||
for (TSendEntity s : reservationList) {
|
||||
PringoSendRequestDto pringoReq = sendEmlService.buildPringoSendRequest(
|
||||
s.getSendEml(),
|
||||
s.getSendNm(),
|
||||
s.getMsgTitle(),
|
||||
s.getMsgBody(),
|
||||
s.getTmplatIdx(),
|
||||
s.getAtchFl(),
|
||||
DestInfoDto.from(s)
|
||||
);
|
||||
|
||||
SendInternalResultDto sendResult = sendEmlService.sendPringoEmail(pringoReq);
|
||||
|
||||
s.setCmid(sendResult.getCmid());
|
||||
s.setSendStatus(sendResult.getStatus().getCode());
|
||||
s.setResultCode(sendResult.getResultCode());
|
||||
updatedSends.add(s);
|
||||
TSendEntity claimed = sendEmlService.claimReservedEmail(s.getSendIdx());
|
||||
if (claimed == null) {
|
||||
continue;
|
||||
}
|
||||
SendInternalResultDto sendResult;
|
||||
try {
|
||||
PringoSendRequestDto pringoReq = sendEmlService.buildPringoSendRequest(
|
||||
claimed.getSendEml(), claimed.getSendNm(), claimed.getMsgTitle(),
|
||||
claimed.getMsgBody(), claimed.getTmplatIdx(), claimed.getAtchFl(),
|
||||
DestInfoDto.from(claimed));
|
||||
sendResult = sendEmlService.sendPringoEmail(pringoReq);
|
||||
} catch (Exception ex) {
|
||||
log.warn("Reserved email preparation failed (sendIdx={})", claimed.getSendIdx());
|
||||
sendResult = SendInternalResultDto.of(claimed.getCmid(),
|
||||
SendStatusType.FAIL, NotihubConst.EMAIL_SEND_FAIL);
|
||||
}
|
||||
TSendEntity persisted = resultService.record(claimed.getSendIdx(), sendResult.cmid(),
|
||||
sendResult.status(), sendResult.resultCode(), null);
|
||||
if (persisted != null) {
|
||||
sendEmlService.afterPersistSendEntities(List.of(persisted));
|
||||
}
|
||||
}
|
||||
|
||||
//TSendEntity update
|
||||
sendRepository.saveAll(updatedSends);
|
||||
sendRepository.flush();
|
||||
|
||||
//결과들을 모았다가 afterPersistSendEntities
|
||||
sendEmlService.afterPersistSendEntities(updatedSends);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user