191 lines
6.0 KiB
Java
191 lines
6.0 KiB
Java
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;
|
|
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.entity.TTmplatEntity;
|
|
import com.kri.notihub.server.persistence.repository.BulkSendRepository;
|
|
import com.kri.notihub.server.persistence.repository.SendRepository;
|
|
import com.kri.notihub.server.persistence.repository.TemplateRepository;
|
|
import com.kri.notihub.server.service.SendEmailService;
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
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;
|
|
private final TemplateRepository templateRepository;
|
|
|
|
@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))
|
|
);
|
|
|
|
Map<Long, TTmplatEntity> templateMap = loadTemplateMap(standbyList);
|
|
|
|
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(resolveDelay(send, templateMap));
|
|
}
|
|
}
|
|
|
|
private Map<Long, TTmplatEntity> loadTemplateMap(List<TSendEntity> standbyList) {
|
|
List<Long> tmplatIdxList = standbyList.stream()
|
|
.map(TSendEntity::getTmplatIdx)
|
|
.filter(Objects::nonNull)
|
|
.distinct()
|
|
.toList();
|
|
|
|
if (tmplatIdxList.isEmpty()) {
|
|
return Map.of();
|
|
}
|
|
|
|
return templateRepository.findAllById(tmplatIdxList).stream()
|
|
.collect(Collectors.toMap(
|
|
TTmplatEntity::getTmplatIdx,
|
|
Function.identity(),
|
|
(a, b) -> a
|
|
));
|
|
}
|
|
|
|
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) {
|
|
PringoSendRequestDto 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 long resolveDelay(TSendEntity send, Map<Long, TTmplatEntity> templateMap) {
|
|
long templateDelay = 0L;
|
|
if (send != null && send.getTmplatIdx() != null) {
|
|
TTmplatEntity template = templateMap.get(send.getTmplatIdx());
|
|
if (template != null && template.getDelayTime() != null) {
|
|
templateDelay = template.getDelayTime();
|
|
}
|
|
}
|
|
|
|
return Math.max(templateDelay, NotihubConst.EMAIL_DELAY);
|
|
}
|
|
|
|
private void sleepSilently(long ms) {
|
|
if (ms <= 0) {
|
|
return;
|
|
}
|
|
try {
|
|
Thread.sleep(ms);
|
|
} catch (InterruptedException e) {
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
}
|
|
}
|