260715_03_ENH: 이메일 대량 발송 스케쥴러로 분리
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
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.PringoJsonSendRequestDto;
|
||||
import com.kri.notihub.server.dto.send.DestInfoDto;
|
||||
import com.kri.notihub.server.dto.send.SendInternalResultDto;
|
||||
import com.kri.notihub.server.persistence.entity.TBulkSendEntity;
|
||||
import com.kri.notihub.server.persistence.entity.TSendEntity;
|
||||
import com.kri.notihub.server.persistence.repository.BulkSendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.service.SendEmailService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EmailBulkSendScheduler {
|
||||
|
||||
private final SendEmailService sendEmailService;
|
||||
private final SendRepository sendRepository;
|
||||
private final BulkSendRepository bulkSendRepository;
|
||||
|
||||
@Value("${kri.email.bulk-send.batch-size:500}")
|
||||
private int batchSize;
|
||||
|
||||
@Value("${kri.email.bulk-send.sending-stale-minutes:30}")
|
||||
private long sendingStaleMinutes;
|
||||
|
||||
@Scheduled(fixedDelayString = "${kri.email.bulk-send.fixed-delay-ms:10000}")
|
||||
public void sendBulkEmail() {
|
||||
recoverStaleSending();
|
||||
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
List<TSendEntity> standbyList = sendRepository.findBulkStandbySendEmail(
|
||||
SendType.EMAIL.getCode(),
|
||||
NotihubConst.EMAIL_STANDBY,
|
||||
now,
|
||||
PageRequest.of(0, Math.max(1, batchSize))
|
||||
);
|
||||
|
||||
for (TSendEntity send : standbyList) {
|
||||
if (!tryMarkSending(send)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SendInternalResultDto sendResult = sendEmail(send);
|
||||
applySendResult(send, sendResult);
|
||||
sendRepository.save(send);
|
||||
|
||||
if (sendResult.getStatus() == SendStatusType.FAIL) {
|
||||
increaseBulkFailCount(send.getBulkSendKey(), now);
|
||||
} else {
|
||||
sendEmailService.afterPersistSendEntities(List.of(send));
|
||||
}
|
||||
|
||||
sleepSilently(NotihubConst.EMAIL_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverStaleSending() {
|
||||
LocalDateTime staleBefore = DateTimeUtil.nowKstLocalDateTime()
|
||||
.minusMinutes(Math.max(1, sendingStaleMinutes));
|
||||
sendRepository.recoverStaleBulkEmailSending(
|
||||
SendType.EMAIL.getCode(),
|
||||
NotihubConst.EMAIL_SENDING,
|
||||
NotihubConst.EMAIL_STANDBY,
|
||||
staleBefore
|
||||
);
|
||||
}
|
||||
|
||||
private boolean tryMarkSending(TSendEntity send) {
|
||||
if (send == null || send.getSendIdx() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int updated = sendRepository.markEmailSending(
|
||||
send.getSendIdx(),
|
||||
NotihubConst.EMAIL_STANDBY,
|
||||
NotihubConst.EMAIL_SENDING
|
||||
);
|
||||
if (updated != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
send.setResultCode(NotihubConst.EMAIL_SENDING);
|
||||
return true;
|
||||
}
|
||||
|
||||
private SendInternalResultDto sendEmail(TSendEntity send) {
|
||||
PringoJsonSendRequestDto pringoReq = sendEmailService.buildPringoSendRequest(
|
||||
send.getSendEml(),
|
||||
send.getSendNm(),
|
||||
send.getMsgTitle(),
|
||||
send.getMsgBody(),
|
||||
DestInfoDto.from(send)
|
||||
);
|
||||
return sendEmailService.sendPringoEmail(pringoReq);
|
||||
}
|
||||
|
||||
private void applySendResult(TSendEntity send, SendInternalResultDto sendResult) {
|
||||
send.setCmid(sendResult.getCmid());
|
||||
send.setSendStatus(sendResult.getStatus().getCode());
|
||||
send.setResultCode(sendResult.getResultCode());
|
||||
}
|
||||
|
||||
private void increaseBulkFailCount(String bulkSendKey, LocalDateTime now) {
|
||||
if (bulkSendKey == null || bulkSendKey.isBlank()) {
|
||||
return;
|
||||
}
|
||||
|
||||
bulkSendRepository.findByBulkSendKey(bulkSendKey).ifPresent(bulk -> {
|
||||
int fail = bulk.getFailCnt() == null ? 0 : bulk.getFailCnt();
|
||||
bulk.setFailCnt(fail + 1);
|
||||
completeBulkIfNeeded(bulk, now);
|
||||
bulkSendRepository.save(bulk);
|
||||
});
|
||||
}
|
||||
|
||||
private void completeBulkIfNeeded(TBulkSendEntity bulk, LocalDateTime now) {
|
||||
if (bulk.getCompleteDt() != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int sendCnt = bulk.getSendCnt() == null ? 0 : bulk.getSendCnt();
|
||||
int successCnt = bulk.getSuccesCnt() == null ? 0 : bulk.getSuccesCnt();
|
||||
int failCnt = bulk.getFailCnt() == null ? 0 : bulk.getFailCnt();
|
||||
if (sendCnt <= successCnt + failCnt) {
|
||||
bulk.setCompleteDt(now);
|
||||
}
|
||||
}
|
||||
|
||||
private void sleepSilently(long ms) {
|
||||
if (ms <= 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user