[배치] hostName -> ip로 변경, batch_check 테이블 create_at, created_ip 칼럼 추가

This commit is contained in:
kangsunlee
2025-07-24 10:25:47 +09:00
parent 3ea9221970
commit 89c15d31f4
13 changed files with 77 additions and 70 deletions

View File

@@ -3,6 +3,7 @@ package app.module.admin.config.application.batch;
import app.module.admin.usecase.batchLog.ports.BatchLogPortIn;
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
import app.module.core.config.application.logging.SafeLogger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -23,25 +24,25 @@ public class BatchLogRunner {
return;
}
String hostName;
try {
hostName = java.net.InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostName = "UNKNOWN";
safeLogger.warn("호스트 이름 조회 실패", e);
}
// dev 환경 + 실제 날짜에 실행 로그 insert (RUNNING)
Long logId = batchLogPortIn.create(batchType, hostName, "RUNNING");
Long logId = batchLogPortIn.create(batchType, getIpAddress(), "RUNNING");
try {
action.run();
// 성공 처리
batchLogPortIn.update(logId, hostName,"SUCCESS", null);
batchLogPortIn.update(logId, getIpAddress(),"SUCCESS", null);
} catch (Exception e) {
// 실패 처리
batchLogPortIn.update(logId, hostName,"FAIL", e.getMessage());
batchLogPortIn.update(logId, getIpAddress(),"FAIL", e.getMessage());
safeLogger.error("[{}] 배치 실패: {}", batchType.name(), e.getMessage(), e);
}
}
private String getIpAddress() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return "unknown";
}
}
}

View File

@@ -3,6 +3,7 @@ package app.module.admin.usecase.batchCheck.ports.application;
import app.module.admin.usecase.batchCheck.ports.application.domain.BatchCheckInCmd;
import app.module.admin.usecase.batchLog.ports.BatchLogPortIn;
import java.net.InetAddress;
import java.net.UnknownHostException;
import lombok.RequiredArgsConstructor;
import app.module.admin.usecase.batchCheck.ports.application.domain.BatchCheckOutCmd;
import app.module.admin.usecase.batchCheck.ports.BatchCheckPortIn;
@@ -21,30 +22,30 @@ class BatchCheckService implements BatchCheckPortIn {
@Override
public boolean operate(BatchCheckInCmd inCmd) {
try {
portOut.operate(new BatchCheckOutCmd(inCmd.getType(), inCmd.getRunDate()));
portOut.operate(new BatchCheckOutCmd(inCmd.getType(), inCmd.getRunDate(), getIpAddress()));
return true;
} catch (DuplicateKeyException e) {
log.warn("{} 가 이미 실행 중입니다. 중복 실행을 건너뜁니다.", inCmd.getMethodName());
log.warn("[batch] {} 가 이미 실행 중입니다. 중복 실행을 건너뜁니다.", inCmd.getMethodName());
batchLogPortIn.createError(inCmd.getType(), getHostName(), "WARNING",
inCmd.getMethodName() + " 가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
batchLogPortIn.createError(inCmd.getType(), getIpAddress(), "WARNING",
"[batch]" + inCmd.getMethodName() + " 가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
return false;
} catch (Exception e) {
log.error("중복 실행 체크 예외 발생 : {} / {}", inCmd.getMethodName(), e.getMessage(), e);
log.error("[batch] 중복 실행 체크 예외 발생 : {} / {}", inCmd.getMethodName(), e.getMessage(), e);
batchLogPortIn.createError(inCmd.getType(), getHostName(), "ERROR",
inCmd.getMethodName() + " 중복 체크 중 예외 발생: " + e.getMessage());
batchLogPortIn.createError(inCmd.getType(), getIpAddress(), "ERROR",
"[batch]" + inCmd.getMethodName() + " 중복 체크 중 예외 발생: " + e.getMessage());
return false;
}
}
private String getHostName() {
private String getIpAddress() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return "unknown";
}
}

View File

@@ -14,4 +14,5 @@ import lombok.Setter;
public class BatchCheckOutCmd {
private BatchLogType type;
private Date runDate;
private String ip;
}

View File

@@ -19,19 +19,19 @@ class BatchLogDao implements BatchLogPortOut {
}
@Override
public Long create(BatchLogType type, String hostName, String status) {
BatchLogCmd cmd = new BatchLogCmd(null, type.name(), status, LocalDateTime.now(), hostName);
public Long create(BatchLogType type, String ip, String status) {
BatchLogCmd cmd = new BatchLogCmd(null, type.name(), status, ip, LocalDateTime.now());
mapper.create(cmd);
return cmd.getId();
}
@Override
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
mapper.createError(type.name(), hostName, status, errorMessage);
public void createError(BatchLogType type, String ip, String status, String errorMessage) {
mapper.createError(type.name(), ip, status, errorMessage);
}
@Override
public void update(Long id, String hostName, String status, String errorMessage) {
mapper.update(id, hostName, status, errorMessage);
public void update(Long id, String ip, String status, String errorMessage) {
mapper.update(id, ip, status, errorMessage);
}
}

View File

@@ -7,6 +7,6 @@ import org.apache.ibatis.annotations.Mapper;
public interface BatchLogMapper {
boolean exists(String type);
Long create(BatchLogCmd cmd);
Long createError(String type, String hostName, String status, String errorMessage);
void update(Long id, String hostName, String status, String errorMessage);
Long createError(String type, String ip, String status, String errorMessage);
void update(Long id, String ip, String status, String errorMessage);
}

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, String status);
void createError(BatchLogType type, String hostName, String status, String errorMessage);
void update(Long id, String hostName, String status, String errorMessage);
Long create(BatchLogType type, String ip, String status);
void createError(BatchLogType type, String ip, String status, String errorMessage);
void update(Long id, String ip, 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, String status);
void createError(BatchLogType type, String hostName, String status, String errorMessage);
void update(Long id, String hostName, String status, String errorMessage);
Long create(BatchLogType type, String ip, String status);
void createError(BatchLogType type, String ip, String status, String errorMessage);
void update(Long id, String ip, String status, String errorMessage);
}

View File

@@ -17,17 +17,17 @@ class BatchLogService implements BatchLogPortIn {
}
@Override
public Long create(BatchLogType type, String hostName, String status) {
return portOut.create(type, hostName, status);
public Long create(BatchLogType type, String ip, String status) {
return portOut.create(type, ip, status);
}
@Override
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
portOut.createError(type, hostName, status, errorMessage);
public void createError(BatchLogType type, String ip, String status, String errorMessage) {
portOut.createError(type, ip, status, errorMessage);
}
@Override
public void update(Long id, String hostName, String status, String errorMessage) {
portOut.update(id, hostName, status, errorMessage);
public void update(Long id, String ip, String status, String errorMessage) {
portOut.update(id, ip, status, errorMessage);
}
}

View File

@@ -15,6 +15,6 @@ public class BatchLogCmd {
private Long id;
private String type;
private String status;
private String ip;
private LocalDateTime startAt;
private String hostName;
}

View File

@@ -37,7 +37,7 @@ public class AutoSchedulesController {
String methodName = "fetchMonthlyStatus";
if (!schedulerEnabled) {
log.info("{} 는 local 환경에서는 실행되지 않습니다. (현재: {})", methodName, activeProfiles);
log.info("[batch] {} 는 {} 환경에서는 실행되지 않습니다.", methodName, activeProfiles);
return;
}
@@ -48,16 +48,16 @@ public class AutoSchedulesController {
}
try {
log.info("스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostName(), this,
log.info("[batch] 스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostAddress(), this,
+System.identityHashCode(this), Thread.currentThread().getName());
// 월별입력상태 실행
portIn.operate();
} catch (Exception e) {
log.error("예외 발생 : {} / {}", methodName, e.getMessage(), e);
log.error("[batch] 예외 발생 : {} / {}", methodName, e.getMessage(), e);
} finally {
log.info("스케줄 종료 : {}", methodName);
log.info("[batch] 스케줄 종료 : {}", methodName);
}
}
@@ -65,12 +65,12 @@ public class AutoSchedulesController {
* 문자자동발송 등록
* 매일 오후 5시 마다 실행되는 스케줄러
*/
@Scheduled(cron = "0 0 18 * * ?")
@Scheduled(cron = "0 0 17 * * ?")
public void fetchSmsAutoSend() {
String methodName = "fetchSmsAutoSend";
if (!schedulerEnabled) {
log.info("{} 는 local 환경에서는 실행되지 않습니다. (현재: {})", methodName, activeProfiles);
log.info("[batch] {} 는 {} 환경에서는 실행되지 않습니다.", methodName, activeProfiles);
return;
}
@@ -81,16 +81,16 @@ public class AutoSchedulesController {
}
try {
log.info("스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostName(), this,
log.info("[batch] 스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostAddress(), this,
+System.identityHashCode(this), Thread.currentThread().getName());
// 문자자동발송 실행
portIn.sendAutoSms();
} catch (Exception e) {
log.error("예외 발생 : {} / {}", methodName, e.getMessage(), e);
log.error("[batch] 예외 발생 : {} / {}", methodName, e.getMessage(), e);
} finally {
log.info("스케줄 종료 : {}", methodName);
log.info("[batch] 스케줄 종료 : {}", methodName);
}
}
@@ -103,7 +103,7 @@ public class AutoSchedulesController {
String methodName = "fetchMonthlyCarryOver";
if (!schedulerEnabled) {
log.info("{} 는 local 환경에서는 실행되지 않습니다. (현재: {})", methodName, activeProfiles);
log.info("[batch] {} 는 {} 환경에서는 실행되지 않습니다.", methodName, activeProfiles);
return;
}
@@ -114,16 +114,16 @@ public class AutoSchedulesController {
}
try {
log.info("스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostName(), this,
log.info("[batch] 스케줄 실행 : {} / {} / {} / {} / {}",
methodName, java.net.InetAddress.getLocalHost().getHostAddress(), this,
+System.identityHashCode(this), Thread.currentThread().getName());
// 월초이월금 실행
portIn.monthlyCarryOver();
} catch (Exception e) {
log.error("예외 발생 : {} / {}", methodName, e.getMessage(), e);
log.error("[batch] 예외 발생 : {} / {}", methodName, e.getMessage(), e);
} finally {
log.info("스케줄 종료 : {}", methodName);
log.info("[batch] 스케줄 종료 : {}", methodName);
}
}
}

View File

@@ -2,8 +2,8 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="app.module.admin.usecase.batchCheck.adapters.persistence.BatchCheckMapper" >
<insert id="create">
/* create : 배치 실행 여부 등록 */
INSERT INTO batch_check(type, run_date)
VALUES (#{type}, #{runDate})
/* create : 배치 중복 실행 방지를 위한 등록 */
INSERT INTO batch_check(type, run_date, created_ip, created_at)
VALUES (#{type}, #{runDate}, #{ip}, now())
</insert>
</mapper>

View File

@@ -19,8 +19,8 @@
SELECT nextval('batch_log_seq')
</selectKey>
INSERT INTO batch_log (id, type, status, start_at, created_at, created_host)
VALUES (#{id}, #{type}, #{status}, #{startAt}, now(), #{hostName})
INSERT INTO batch_log (id, type, status, start_at, created_at, created_ip)
VALUES (#{id}, #{type}, #{status}, #{startAt}, now(), #{ip})
</insert>
<!-- 에러 로그 insert -->
@@ -29,8 +29,8 @@
SELECT nextval('batch_log_seq')
</selectKey>
INSERT INTO batch_log (id, type, status, error_message, start_at, created_at, created_host)
VALUES (#{id}, #{type}, #{status}, #{errorMessage}, now(), now(), #{hostName})
INSERT INTO batch_log (id, type, status, error_message, start_at, created_at, created_ip)
VALUES (#{id}, #{type}, #{status}, #{errorMessage}, now(), now(), #{ip})
</insert>
<!-- 상태 업데이트 -->
@@ -39,7 +39,7 @@
SET status = #{status},
error_message = #{errorMessage},
end_at = now(),
created_host = #{hostName}
created_ip = #{ip}
WHERE id = #{id}
</update>
</mapper>