Merge branch 'bo_sms-auto-send' into 'dev'

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

See merge request korea-research/korea-research!137
This commit is contained in:
이강선 매니저/ST
2025-07-21 08:13:13 +00:00
9 changed files with 55 additions and 43 deletions

View File

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

View File

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

View File

@@ -4,7 +4,7 @@ import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
public interface BatchLogPortIn {
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 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 {
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 update(Long id, String hostName, String status, String errorMessage);
}

View File

@@ -17,8 +17,8 @@ class BatchLogService implements BatchLogPortIn {
}
@Override
public Long create(BatchLogType type, String hostName) {
return portOut.create(type, hostName);
public Long create(BatchLogType type, String hostName, String status) {
return portOut.create(type, hostName, status);
}
@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.schedule.autoSchedules.ports.AutoSchedulesPortIn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -14,6 +15,9 @@ public class AutoSchedulesController {
private final AutoSchedulesPortIn portIn;
private final BatchLogPortIn batchLogPortIn;
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) {
this.portIn = portIn;
@@ -66,10 +70,18 @@ public class AutoSchedulesController {
*/
@Scheduled(cron = "0 0 17 * * ?")
public void fetchSmsAutoSend() {
if ("dev".equals(activeProfiles)) {
log.info("fetchSmsAutoSend는 dev 환경에서만 실행됩니다. (현재: {})", activeProfiles);
return;
}
String lockKey = "fetchSmsAutoSend" + java.time.LocalDate.now();
try {
String host = java.net.InetAddress.getLocalHost().getHostName();
// [TEST] dev 환경에서 배치가 실제 실행되고 있는지 체크
batchLogPortIn.create(BatchLogType.AUTO_SMS, host, "BATCH_RUNNING");
// 중복 실행 방지를 위한 간단한 체크
if (isAlreadyRunning(lockKey)) {
log.warn("fetchSmsAutoSend 이미 실행 중입니다. 중복 실행을 건너뜁니다.");

View File

@@ -34,8 +34,6 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
private final GetSmsAutoStatusPortIn getSmsAutoStatusPortIn;
private final GetIncompleteHouseholdsPortIn getIncompleteHouseholdsPortIn;
private final BatchLogRunner batchLogRunner;
// TODO(ks) : 문자발송 테스트
@Value("${spring.profiles.active}")
private String activeProfiles;
@@ -48,44 +46,43 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
@Override
@Transactional
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())) {
log.info("문자 발송 비활성화 상태");
if (getSmsAutoStatus == null || Boolean.FALSE.equals(getSmsAutoStatus.getActive())) {
log.info("문자 발송 비활성화 상태");
return Optional.of(false);
}
LocalDate today = LocalDate.now();
int day = today.getDayOfMonth();
if (day == 8 || day == 15 || day == 22 || day == 1) {
DayRange dayRange = DateUiUtil.getDayRange(today).orElse(null);
if (dayRange == null) {
log.error("잘못된 날짜({})에 문자 자동 발송 로직이 실행됨 - 발송 대상일이 아님", today);
return Optional.of(false);
}
LocalDate today = LocalDate.now();
int day = today.getDayOfMonth();
LocalDate baseDate = (day == 1) ? today.minusMonths(1) : today;
String targetMonth = DateUiUtil.getMonthValue(baseDate);
if (day == 8 || day == 15 || day == 22 || day == 1) {
DayRange dayRange = DateUiUtil.getDayRange(today).orElse(null);
if (dayRange == null) {
log.error("잘못된 날짜({})에 문자 자동 발송 로직이 실행됨 - 발송 대상일이 아님", today);
return Optional.of(false);
}
// activeProfiles 가 'dev'인 경우일 때 테스트용 임가 데이터만 조회합니다.
List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate(
new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(),
dayRange.getToDay(), activeProfiles));
LocalDate baseDate = (day == 1) ? today.minusMonths(1) : today;
String targetMonth = DateUiUtil.getMonthValue(baseDate);
List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate(
new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(), dayRange.getToDay())
);
if (!CollectionUtils.isEmpty(incompleteHouseholds)) {
batchLogRunner.run(BatchLogType.AUTO_SMS, () -> {
mmsPortIn.sendAutoSms(new CreateSmsAutoSendCmd(
incompleteHouseholds,
getSmsAutoStatus.getSendPhone(),
getSmsAutoStatus.getMessage()
));
});
}
} else {
log.info("문자 발송 날짜 아님.");
return Optional.of(false);
if (!CollectionUtils.isEmpty(incompleteHouseholds)) {
batchLogRunner.run(BatchLogType.AUTO_SMS, () -> {
mmsPortIn.sendAutoSms(new CreateSmsAutoSendCmd(
incompleteHouseholds,
getSmsAutoStatus.getSendPhone(),
getSmsAutoStatus.getMessage()
));
});
}
} else {
log.info("문자 발송 날짜 아님.");
return Optional.of(false);
}
return Optional.of(true);

View File

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

View File

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