[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.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
@EnableScheduling
|
||||
@ComponentScan(basePackages = "app.module")
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
|
||||
mapper.createError(type.name(), hostName, status, errorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long id, String status, String errorMessage) {
|
||||
mapper.update(id, status, errorMessage);
|
||||
|
||||
@@ -7,5 +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 status, String errorMessage);
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@ import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
public interface BatchLogPortIn {
|
||||
boolean exists(BatchLogType type);
|
||||
Long create(BatchLogType type);
|
||||
void createError(BatchLogType type, String hostName, 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 {
|
||||
boolean exists(BatchLogType type);
|
||||
Long create(BatchLogType type);
|
||||
void createError(BatchLogType type, String hostName, String status, String errorMessage);
|
||||
void update(Long id, String status, String errorMessage);
|
||||
}
|
||||
@@ -21,6 +21,11 @@ class BatchLogService implements BatchLogPortIn {
|
||||
return portOut.create(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createError(BatchLogType type, String hostName, String status, String errorMessage) {
|
||||
portOut.createError(type, hostName, status, errorMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long id, String status, String errorMessage) {
|
||||
portOut.update(id, status, errorMessage);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
@@ -10,9 +12,20 @@ import org.springframework.stereotype.Component;
|
||||
public class AutoSchedulesController {
|
||||
|
||||
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.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초 마다 실행되는 스케줄러
|
||||
*/
|
||||
//@Scheduled(cron = "0 0 0 1 * *")
|
||||
@Scheduled(cron = "1 0 0 19,20 * *")
|
||||
@Scheduled(cron = "0 0 * * * *")
|
||||
public void fetchMonthlyStatus() {
|
||||
String lockKey = "fetchMonthlyStatus_" + java.time.LocalDate.now();
|
||||
try {
|
||||
|
||||
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());
|
||||
|
||||
portIn.operate();
|
||||
|
||||
} catch (Exception 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 * * ?")
|
||||
public void fetchSmsAutoSend() {
|
||||
String lockKey = "fetchSmsAutoSend" + java.time.LocalDate.now();
|
||||
try {
|
||||
|
||||
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());
|
||||
|
||||
portIn.sendAutoSms();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
log.info("스케줄 종료 : fetchSmsAutoSend / {}", lockKey);
|
||||
|
||||
releaseLock(lockKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,18 +96,30 @@ public class AutoSchedulesController {
|
||||
* 매월 16일 0시 0초에 실행되는 스케줄러
|
||||
*/
|
||||
// @Scheduled(cron = "0 0 0 16 * *")
|
||||
@Scheduled(cron = "0 0 0 19,20 * *")
|
||||
@Scheduled(cron = "0 0 * * * *")
|
||||
public void fetchMonthlyCarryOver() {
|
||||
String lockKey = "fetchMonthlyCarryOver_" + java.time.LocalDate.now();
|
||||
try {
|
||||
|
||||
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());
|
||||
|
||||
portIn.monthlyCarryOver();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
log.info("스케줄 종료 : fetchMonthlyCarryOver / {}", lockKey);
|
||||
|
||||
releaseLock(lockKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,16 @@
|
||||
VALUES (#{id}, #{type}, #{status}, #{startAt}, now())
|
||||
</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 batch_log
|
||||
|
||||
Reference in New Issue
Block a user