[TEST] 배치 중복 실행 방지
This commit is contained in:
@@ -6,11 +6,9 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
|
|||||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
@EnableScheduling
|
|
||||||
@ComponentScan(basePackages = "app.module")
|
@ComponentScan(basePackages = "app.module")
|
||||||
public class AdminApplication extends SpringBootServletInitializer {
|
public class AdminApplication extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package app.module.admin.config.application.bean;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableScheduling
|
||||||
|
public class SchedulingConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ThreadPoolTaskScheduler taskScheduler() {
|
||||||
|
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||||
|
scheduler.setPoolSize(5);
|
||||||
|
scheduler.setThreadNamePrefix("scheduled-task-");
|
||||||
|
scheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||||
|
scheduler.setAwaitTerminationSeconds(60);
|
||||||
|
return scheduler;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,11 @@ class BatchLogDao implements BatchLogPortOut {
|
|||||||
return cmd.getId();
|
return cmd.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
|
||||||
|
mapper.createError(type.name(), hostName, status, errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Long id, String status, String errorMessage) {
|
public void update(Long id, String status, String errorMessage) {
|
||||||
mapper.update(id, status, errorMessage);
|
mapper.update(id, status, errorMessage);
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
public interface BatchLogMapper {
|
public interface BatchLogMapper {
|
||||||
boolean exists(String type);
|
boolean exists(String type);
|
||||||
Long create(BatchLogCmd cmd);
|
Long create(BatchLogCmd cmd);
|
||||||
|
Long createError(String type, String hostName, String status, String errorMessage);
|
||||||
void update(Long id, String status, String errorMessage);
|
void update(Long id, String status, String errorMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ 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);
|
Long create(BatchLogType type);
|
||||||
|
void createError(BatchLogType type, String hostName, String status, String errorMessage);
|
||||||
void update(Long id, String status, String errorMessage);
|
void update(Long id, String status, String errorMessage);
|
||||||
}
|
}
|
||||||
@@ -5,5 +5,6 @@ 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);
|
Long create(BatchLogType type);
|
||||||
|
void createError(BatchLogType type, String hostName, String status, String errorMessage);
|
||||||
void update(Long id, String status, String errorMessage);
|
void update(Long id, String status, String errorMessage);
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,11 @@ class BatchLogService implements BatchLogPortIn {
|
|||||||
return portOut.create(type);
|
return portOut.create(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
|
||||||
|
portOut.createError(type, hostName, status, errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Long id, String status, String errorMessage) {
|
public void update(Long id, String status, String errorMessage) {
|
||||||
portOut.update(id, status, errorMessage);
|
portOut.update(id, status, errorMessage);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package app.module.admin.usecase.schedule.autoSchedules.adapters.web;
|
package app.module.admin.usecase.schedule.autoSchedules.adapters.web;
|
||||||
|
|
||||||
|
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 app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortIn;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
@@ -10,9 +12,20 @@ import org.springframework.stereotype.Component;
|
|||||||
public class AutoSchedulesController {
|
public class AutoSchedulesController {
|
||||||
|
|
||||||
private final AutoSchedulesPortIn portIn;
|
private final AutoSchedulesPortIn portIn;
|
||||||
|
private final BatchLogPortIn batchLogPortIn;
|
||||||
|
private final java.util.Set<String> runningTasks = java.util.concurrent.ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
public AutoSchedulesController(AutoSchedulesPortIn portIn) {
|
public AutoSchedulesController(AutoSchedulesPortIn portIn, BatchLogPortIn batchLogPortIn) {
|
||||||
this.portIn = portIn;
|
this.portIn = portIn;
|
||||||
|
this.batchLogPortIn = batchLogPortIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAlreadyRunning(String lockKey) {
|
||||||
|
return !runningTasks.add(lockKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void releaseLock(String lockKey) {
|
||||||
|
runningTasks.remove(lockKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,18 +33,30 @@ public class AutoSchedulesController {
|
|||||||
* 매 월 1일 0초 마다 실행되는 스케줄러
|
* 매 월 1일 0초 마다 실행되는 스케줄러
|
||||||
*/
|
*/
|
||||||
//@Scheduled(cron = "0 0 0 1 * *")
|
//@Scheduled(cron = "0 0 0 1 * *")
|
||||||
@Scheduled(cron = "1 0 0 19,20 * *")
|
@Scheduled(cron = "0 0 * * * *")
|
||||||
public void fetchMonthlyStatus() {
|
public void fetchMonthlyStatus() {
|
||||||
|
String lockKey = "fetchMonthlyStatus_" + java.time.LocalDate.now();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String host = java.net.InetAddress.getLocalHost().getHostName();
|
String host = java.net.InetAddress.getLocalHost().getHostName();
|
||||||
|
|
||||||
|
// 중복 실행 방지를 위한 간단한 체크
|
||||||
|
if (isAlreadyRunning(lockKey)) {
|
||||||
|
log.warn("fetchMonthlyStatus가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
|
||||||
|
batchLogPortIn.createError(BatchLogType.MONTHLY_STATUS, host, "WARNING", "fetchMonthlyStatus가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
log.info("스케줄 실행 : fetchMonthlyStatus / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
log.info("스케줄 실행 : fetchMonthlyStatus / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
||||||
|
|
||||||
portIn.operate();
|
portIn.operate();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
log.info("스케줄 종료 : fetchMonthlyStatus / {}", lockKey);
|
||||||
|
|
||||||
|
releaseLock(lockKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,16 +66,28 @@ public class AutoSchedulesController {
|
|||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 0 17 * * ?")
|
@Scheduled(cron = "0 0 17 * * ?")
|
||||||
public void fetchSmsAutoSend() {
|
public void fetchSmsAutoSend() {
|
||||||
|
String lockKey = "fetchSmsAutoSend" + java.time.LocalDate.now();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String host = java.net.InetAddress.getLocalHost().getHostName();
|
String host = java.net.InetAddress.getLocalHost().getHostName();
|
||||||
|
|
||||||
|
// 중복 실행 방지를 위한 간단한 체크
|
||||||
|
if (isAlreadyRunning(lockKey)) {
|
||||||
|
log.warn("fetchSmsAutoSend 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
|
||||||
|
batchLogPortIn.createError(BatchLogType.AUTO_SMS, host, "WARNING", "fetchSmsAutoSend 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
log.info("스케줄 실행 : fetchSmsAutoSend / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
log.info("스케줄 실행 : fetchSmsAutoSend / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
||||||
|
|
||||||
portIn.sendAutoSms();
|
portIn.sendAutoSms();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
log.info("스케줄 종료 : fetchSmsAutoSend / {}", lockKey);
|
||||||
|
|
||||||
|
releaseLock(lockKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,18 +96,30 @@ public class AutoSchedulesController {
|
|||||||
* 매월 16일 0시 0초에 실행되는 스케줄러
|
* 매월 16일 0시 0초에 실행되는 스케줄러
|
||||||
*/
|
*/
|
||||||
// @Scheduled(cron = "0 0 0 16 * *")
|
// @Scheduled(cron = "0 0 0 16 * *")
|
||||||
@Scheduled(cron = "0 0 0 19,20 * *")
|
@Scheduled(cron = "0 0 * * * *")
|
||||||
public void fetchMonthlyCarryOver() {
|
public void fetchMonthlyCarryOver() {
|
||||||
|
String lockKey = "fetchMonthlyCarryOver_" + java.time.LocalDate.now();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String host = java.net.InetAddress.getLocalHost().getHostName();
|
String host = java.net.InetAddress.getLocalHost().getHostName();
|
||||||
|
|
||||||
|
// 중복 실행 방지를 위한 간단한 체크
|
||||||
|
if (isAlreadyRunning(lockKey)) {
|
||||||
|
log.warn("fetchMonthlyCarryOver가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
|
||||||
|
batchLogPortIn.createError(BatchLogType.CARRYOVER, host, "WARNING", "fetchMonthlyCarryOver가 이미 실행 중입니다. 중복 실행을 건너뜁니다.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
log.info("스케줄 실행 : fetchMonthlyCarryOver / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
log.info("스케줄 실행 : fetchMonthlyCarryOver / {} / {} / {} / {}", host, this, + System.identityHashCode(this), Thread.currentThread().getName());
|
||||||
|
|
||||||
portIn.monthlyCarryOver();
|
portIn.monthlyCarryOver();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
log.info("스케줄 종료 : fetchMonthlyCarryOver / {}", lockKey);
|
||||||
|
|
||||||
|
releaseLock(lockKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,16 @@
|
|||||||
VALUES (#{id}, #{type}, #{status}, #{startAt}, now())
|
VALUES (#{id}, #{type}, #{status}, #{startAt}, now())
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<!-- 에러 로그 insert -->
|
||||||
|
<insert id="createError">
|
||||||
|
<selectKey keyProperty="id" resultType="long" order="BEFORE">
|
||||||
|
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>
|
||||||
|
|
||||||
<!-- 상태 업데이트 -->
|
<!-- 상태 업데이트 -->
|
||||||
<update id="update">
|
<update id="update">
|
||||||
UPDATE batch_log
|
UPDATE batch_log
|
||||||
|
|||||||
@@ -401,6 +401,7 @@ CREATE TABLE batch_log (
|
|||||||
end_at TIMESTAMP,
|
end_at TIMESTAMP,
|
||||||
error_message VARCHAR(2000),
|
error_message VARCHAR(2000),
|
||||||
created_at TIMESTAMP DEFAULT now(),
|
created_at TIMESTAMP DEFAULT now(),
|
||||||
|
created_host VARCHAR(100),
|
||||||
|
|
||||||
CONSTRAINT batch_log_pkey PRIMARY KEY (id)
|
CONSTRAINT batch_log_pkey PRIMARY KEY (id)
|
||||||
);
|
);
|
||||||
@@ -412,6 +413,7 @@ COMMENT ON COLUMN batch_log.start_at IS '배치 시작 시간';
|
|||||||
COMMENT ON COLUMN batch_log.end_at IS '배치 종료 시간';
|
COMMENT ON COLUMN batch_log.end_at IS '배치 종료 시간';
|
||||||
COMMENT ON COLUMN batch_log.error_message IS '에러 메시지 (실패한 경우)';
|
COMMENT ON COLUMN batch_log.error_message IS '에러 메시지 (실패한 경우)';
|
||||||
COMMENT ON COLUMN batch_log.created_at IS '생성일';
|
COMMENT ON COLUMN batch_log.created_at IS '생성일';
|
||||||
|
COMMENT ON COLUMN batch_log.created_host IS '생성HOST';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user