261023_01_ENH: 비동기 메시지 발송 부분 성공 상태 추가 및 수신처 없는 대상 건너뛰기

This commit is contained in:
2026-10-23 15:11:38 +09:00
parent 05b1aca898
commit fc62f9cb38
4 changed files with 35 additions and 5 deletions

View File

@@ -3,11 +3,11 @@ package com.kri.contact.server.common.enums;
import java.util.List;
public enum ListAsyncJobStatus {
QUEUED, RUNNING, CANCEL_REQUESTED, CANCELLED, SUCCEEDED, FAILED;
QUEUED, RUNNING, CANCEL_REQUESTED, CANCELLED, SUCCEEDED, FAILED, PARTIALLY_SUCCEEDED;
public static final List<ListAsyncJobStatus> ACTIVE = List.of(QUEUED, RUNNING, CANCEL_REQUESTED);
public boolean isTerminal() {
return this == CANCELLED || this == SUCCEEDED || this == FAILED;
return this == CANCELLED || this == SUCCEEDED || this == FAILED || this == PARTIALLY_SUCCEEDED;
}
}

View File

@@ -179,6 +179,9 @@ public class ListAsyncJobExecutor implements ApplicationRunner {
public void complete(Runnable changes) {
store.complete(task.key, changes, this::checkCancelled);
}
public void completePartially(String code, String message, Map<String, Object> detail) {
store.completePartially(task.key, code, message, detail, this::checkCancelled);
}
public void reject(String code, String message, Map<String, Object> detail) {
checkCancelled();
store.reject(task.key, code, message, detail);

View File

@@ -100,6 +100,21 @@ public class ListAsyncJobStore {
BusinessException.notFound("LIST_JOB_NOT_FOUND", "작업을 찾을 수 없습니다."));
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void completePartially(String key, String code, String message, Map<String, Object> detail,
Runnable checkCancelled) {
var job = lock(key);
requireRunning(job);
checkCancelled.run();
job.setErrorDetailJson(detail);
job.setJobStatus(ListAsyncJobStatus.PARTIALLY_SUCCEEDED);
job.setJobStage("COMPLETED");
job.setErrorCode(code);
job.setErrorMessage(message);
job.setFinishedDt(DateTimeUtil.nowKstLocalDateTime());
job.setModDt(job.getFinishedDt());
}
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public List<ListAsyncJobActivity> activity(
Long projectId, List<String> heldKeys) {
@@ -114,7 +129,8 @@ public class ListAsyncJobStore {
return jobs.summarizeFinishedMutations(projectId,
List.of(ListAsyncJobType.LIST_UPLOAD, ListAsyncJobType.LIST_RESET,
ListAsyncJobType.LIST_UPDATE, ListAsyncJobType.LIST_ASSIGN),
List.of(ListAsyncJobStatus.SUCCEEDED, ListAsyncJobStatus.FAILED, ListAsyncJobStatus.CANCELLED),
List.of(ListAsyncJobStatus.SUCCEEDED, ListAsyncJobStatus.PARTIALLY_SUCCEEDED,
ListAsyncJobStatus.FAILED, ListAsyncJobStatus.CANCELLED),
heldKeys.isEmpty() ? List.of("") : heldKeys);
}

View File

@@ -61,6 +61,7 @@ public class ListAsyncMessageService {
var failedSids = new HashSet<String>();
var keys = new HashSet<String>();
int duplicates = 0;
int noDestinationSkippedSids = 0;
try (var prepared = spool.open(context.jobKey())) {
var buffer = new ArrayList<NotiHubDestInfoDto>();
for (int offset = 0; offset < frozen.size(); offset += 1000) {
@@ -90,6 +91,10 @@ public class ListAsyncMessageService {
continue;
}
if (candidates.isEmpty()) {
boolean noDestination = request.destHeaderIds().stream()
.map(row.itemMap()::get)
.allMatch(value -> value == null || value.trim().isEmpty());
if (noDestination) { noDestinationSkippedSids++; continue; }
failures.add(Map.of("code", "NO_DESTINATION", "sids", List.of(row.sid())));
failedSids.add(row.sid());
}
@@ -113,6 +118,7 @@ public class ListAsyncMessageService {
progress.put("targetSidCnt", frozen.size());
progress.put("preparedRecipientCnt", prepared.targets());
progress.put("preparationFailedSidCnt", failedSids.size());
progress.put("noDestinationSkippedSidCnt", noDestinationSkippedSids);
progress.put("duplicateSkippedRecipientCnt", duplicates);
for (String metric : List.of("acceptedRecipientCnt", "failedRecipientCnt", "unknownRecipientCnt",
"attemptedRecipientCnt", "processedRecipientCnt", "inFlightRecipientCnt")) progress.put(metric, 0L);
@@ -145,6 +151,7 @@ public class ListAsyncMessageService {
progress.put("bulkSendKey", bulkKey);
context.progress("DISPATCHING", null, progress);
long failed = failedSids.size();
long accepted = 0;
for (int number = 1; number <= prepared.batches(); number++) {
context.checkCancelled();
access.requireProject(projectId, user);
@@ -168,12 +175,16 @@ public class ListAsyncMessageService {
}
// Persist the observed outcome even if cancellation arrived during the HTTP call.
context.messageBatch(number, result);
accepted += ((Number) result.get("acceptedRecipientCnt")).longValue();
failed += ((Number) result.get("failedRecipientCnt")).longValue() + ((Number) result.get("unknownRecipientCnt")).longValue();
}
prepared.close();
finishCycle(context);
if (failed > 0) context.reject("MESSAGE_PARTIAL_FAILURE", "전체 대상 처리를 마쳤으며 실패 또는 접수 불명 대상이 있습니다.", context.job().getErrorDetailJson());
else context.complete(() -> {});
if (failed == 0) context.complete(() -> {});
else if (accepted > 0) context.completePartially("MESSAGE_PARTIAL_FAILURE",
"일부 수신처가 접수되었으며 실패 또는 접수 불명 대상이 있습니다.", context.job().getErrorDetailJson());
else context.reject("MESSAGE_SEND_FAILED", "접수 성공이 확인된 수신처가 없습니다. 실패 또는 접수 불명 내역을 확인해 주세요.",
context.job().getErrorDetailJson());
} catch (IOException failure) {
var wrapped = BusinessException.internalError("MESSAGE_PREPARATION_FAILED", "발송 준비 파일 처리에 실패했습니다.");
wrapped.addSuppressed(failure);