문자 자동발송 배치 실행 시 로그 기록

This commit is contained in:
kangsunlee
2025-07-21 17:10:57 +09:00
parent f58688d1d3
commit 1bb3124d0c
9 changed files with 55 additions and 43 deletions

View File

@@ -31,8 +31,8 @@ public class BatchLogRunner {
safeLogger.warn("호스트 이름 조회 실패", e); safeLogger.warn("호스트 이름 조회 실패", e);
} }
// 실행 로그 insert (RUNNING) // dev 환경 + 실제 날짜에 실행 로그 insert (RUNNING)
Long logId = batchLogPortIn.create(batchType, hostName); Long logId = batchLogPortIn.create(batchType, hostName, "RUNNING");
try { try {
action.run(); action.run();

View File

@@ -19,8 +19,8 @@ class BatchLogDao implements BatchLogPortOut {
} }
@Override @Override
public Long create(BatchLogType type, String hostName) { public Long create(BatchLogType type, String hostName, String status) {
BatchLogCmd cmd = new BatchLogCmd(null, type.name(), "RUNNING", LocalDateTime.now(), hostName); BatchLogCmd cmd = new BatchLogCmd(null, type.name(), status, LocalDateTime.now(), hostName);
mapper.create(cmd); mapper.create(cmd);
return cmd.getId(); return cmd.getId();
} }

View File

@@ -4,7 +4,7 @@ import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
public interface BatchLogPortIn { public interface BatchLogPortIn {
boolean exists(BatchLogType type); boolean exists(BatchLogType type);
Long create(BatchLogType type, String hostName); Long create(BatchLogType type, String hostName, String status);
void createError(BatchLogType type, String hostName, String status, String errorMessage); void createError(BatchLogType type, String hostName, String status, String errorMessage);
void update(Long id, String hostName, String status, String errorMessage); void update(Long id, String hostName, String status, String errorMessage);
} }

View File

@@ -4,7 +4,7 @@ import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
public interface BatchLogPortOut { public interface BatchLogPortOut {
boolean exists(BatchLogType type); boolean exists(BatchLogType type);
Long create(BatchLogType type, String hostName); Long create(BatchLogType type, String hostName, String status);
void createError(BatchLogType type, String hostName, String status, String errorMessage); void createError(BatchLogType type, String hostName, String status, String errorMessage);
void update(Long id, String hostName, String status, String errorMessage); void update(Long id, String hostName, String status, String errorMessage);
} }

View File

@@ -17,8 +17,8 @@ class BatchLogService implements BatchLogPortIn {
} }
@Override @Override
public Long create(BatchLogType type, String hostName) { public Long create(BatchLogType type, String hostName, String status) {
return portOut.create(type, hostName); return portOut.create(type, hostName, status);
} }
@Override @Override

View File

@@ -4,6 +4,7 @@ import app.module.admin.usecase.batchLog.ports.BatchLogPortIn;
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType; import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortIn; import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortIn;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -14,6 +15,9 @@ public class AutoSchedulesController {
private final AutoSchedulesPortIn portIn; private final AutoSchedulesPortIn portIn;
private final BatchLogPortIn batchLogPortIn; private final BatchLogPortIn batchLogPortIn;
private final java.util.Set<String> runningTasks = java.util.concurrent.ConcurrentHashMap.newKeySet(); private final java.util.Set<String> runningTasks = java.util.concurrent.ConcurrentHashMap.newKeySet();
// TODO(ks) : 문자발송 테스트
@Value("${spring.profiles.active}")
private String activeProfiles;
public AutoSchedulesController(AutoSchedulesPortIn portIn, BatchLogPortIn batchLogPortIn) { public AutoSchedulesController(AutoSchedulesPortIn portIn, BatchLogPortIn batchLogPortIn) {
this.portIn = portIn; this.portIn = portIn;
@@ -66,10 +70,18 @@ public class AutoSchedulesController {
*/ */
@Scheduled(cron = "0 0 17 * * ?") @Scheduled(cron = "0 0 17 * * ?")
public void fetchSmsAutoSend() { public void fetchSmsAutoSend() {
if ("dev".equals(activeProfiles)) {
log.info("fetchSmsAutoSend는 dev 환경에서만 실행됩니다. (현재: {})", activeProfiles);
return;
}
String lockKey = "fetchSmsAutoSend" + java.time.LocalDate.now(); String lockKey = "fetchSmsAutoSend" + java.time.LocalDate.now();
try { try {
String host = java.net.InetAddress.getLocalHost().getHostName(); String host = java.net.InetAddress.getLocalHost().getHostName();
// [TEST] dev 환경에서 배치가 실제 실행되고 있는지 체크
batchLogPortIn.create(BatchLogType.AUTO_SMS, host, "BATCH_RUNNING");
// 중복 실행 방지를 위한 간단한 체크 // 중복 실행 방지를 위한 간단한 체크
if (isAlreadyRunning(lockKey)) { if (isAlreadyRunning(lockKey)) {
log.warn("fetchSmsAutoSend 이미 실행 중입니다. 중복 실행을 건너뜁니다."); log.warn("fetchSmsAutoSend 이미 실행 중입니다. 중복 실행을 건너뜁니다.");

View File

@@ -34,8 +34,6 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
private final GetSmsAutoStatusPortIn getSmsAutoStatusPortIn; private final GetSmsAutoStatusPortIn getSmsAutoStatusPortIn;
private final GetIncompleteHouseholdsPortIn getIncompleteHouseholdsPortIn; private final GetIncompleteHouseholdsPortIn getIncompleteHouseholdsPortIn;
private final BatchLogRunner batchLogRunner; private final BatchLogRunner batchLogRunner;
// TODO(ks) : 문자발송 테스트
@Value("${spring.profiles.active}") @Value("${spring.profiles.active}")
private String activeProfiles; private String activeProfiles;
@@ -48,7 +46,6 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
@Override @Override
@Transactional @Transactional
public Optional<Boolean> sendAutoSms() { public Optional<Boolean> sendAutoSms() {
if ("dev".equals(activeProfiles)) { // TODO(ks) : 문자발송 테스트
GetSmsAutoStatus getSmsAutoStatus = getSmsAutoStatusPortIn.operate().orElse(null); GetSmsAutoStatus getSmsAutoStatus = getSmsAutoStatusPortIn.operate().orElse(null);
if (getSmsAutoStatus == null || Boolean.FALSE.equals(getSmsAutoStatus.getActive())) { if (getSmsAutoStatus == null || Boolean.FALSE.equals(getSmsAutoStatus.getActive())) {
@@ -69,9 +66,10 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
LocalDate baseDate = (day == 1) ? today.minusMonths(1) : today; LocalDate baseDate = (day == 1) ? today.minusMonths(1) : today;
String targetMonth = DateUiUtil.getMonthValue(baseDate); String targetMonth = DateUiUtil.getMonthValue(baseDate);
// activeProfiles 가 'dev'인 경우일 때 테스트용 임가 데이터만 조회합니다.
List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate( List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate(
new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(), dayRange.getToDay()) new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(),
); dayRange.getToDay(), activeProfiles));
if (!CollectionUtils.isEmpty(incompleteHouseholds)) { if (!CollectionUtils.isEmpty(incompleteHouseholds)) {
batchLogRunner.run(BatchLogType.AUTO_SMS, () -> { batchLogRunner.run(BatchLogType.AUTO_SMS, () -> {
@@ -86,7 +84,6 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
log.info("문자 발송 날짜 아님."); log.info("문자 발송 날짜 아님.");
return Optional.of(false); return Optional.of(false);
} }
}
return Optional.of(true); return Optional.of(true);
} }

View File

@@ -13,4 +13,5 @@ public class GetIncompleteHouseholdsCmd {
private String recordMonth; private String recordMonth;
private int fromDay; private int fromDay;
private int toDay; private int toDay;
private String activeProfiles;
} }

View File

@@ -24,7 +24,9 @@
) )
AND dr.all_status NOT IN ('DONE', 'TODAY_NONE') AND dr.all_status NOT IN ('DONE', 'TODAY_NONE')
) )
AND region_main_code = 'code.region.main.99' /* TODO(ks) : 문자발송 테스트 */ <if test="activeProfiles != null and activeProfiles == 'dev'">
AND phone != '010-1111-1111' /* TODO(ks) : 문자발송 테스트 */ AND region_main_code = 'code.region.main.99'
AND phone != '010-1111-1111'
</if>
</select> </select>
</mapper> </mapper>