260715_04_ENH: 이메일 대량 발송 타임 슬립 위치변경
This commit is contained in:
@@ -9,11 +9,17 @@ 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;
|
||||
@@ -27,6 +33,7 @@ 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;
|
||||
@@ -46,6 +53,8 @@ public class EmailBulkSendScheduler {
|
||||
PageRequest.of(0, Math.max(1, batchSize))
|
||||
);
|
||||
|
||||
Map<Long, TTmplatEntity> templateMap = loadTemplateMap(standbyList);
|
||||
|
||||
for (TSendEntity send : standbyList) {
|
||||
if (!tryMarkSending(send)) {
|
||||
continue;
|
||||
@@ -61,10 +70,29 @@ public class EmailBulkSendScheduler {
|
||||
sendEmailService.afterPersistSendEntities(List.of(send));
|
||||
}
|
||||
|
||||
sleepSilently(NotihubConst.EMAIL_DELAY);
|
||||
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));
|
||||
@@ -137,6 +165,18 @@ public class EmailBulkSendScheduler {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user