From 7de30987854d2cbbcad7c361b1f56c58dc8d8ea6 Mon Sep 17 00:00:00 2001 From: ysCha Date: Wed, 31 Dec 2025 14:22:58 +0900 Subject: [PATCH] =?UTF-8?q?resetFailedJobs=EA=B0=80=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=EB=90=98=EB=A9=B4=EC=84=9C=20DB=EC=97=90=20=EB=81=BC=EC=96=B4?= =?UTF-8?q?=EC=9E=88=EB=8D=98(STARTED)=20=EC=83=81=ED=83=9C=EB=93=A4?= =?UTF-8?q?=EC=9D=84=20=EB=AA=A8=EB=91=90=20=EC=8B=A4=ED=8C=A8(FAILED)?= =?UTF-8?q?=EB=A1=9C=20=EB=B0=94=EA=BF=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qcast/batch/JobLauncherController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/interplug/qcast/batch/JobLauncherController.java b/src/main/java/com/interplug/qcast/batch/JobLauncherController.java index 6aed0869..f2bcddbd 100644 --- a/src/main/java/com/interplug/qcast/batch/JobLauncherController.java +++ b/src/main/java/com/interplug/qcast/batch/JobLauncherController.java @@ -10,12 +10,16 @@ import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; +import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; +import org.springframework.context.event.EventListener; +import java.time.LocalDateTime; @RestController @RequiredArgsConstructor @@ -25,6 +29,7 @@ public class JobLauncherController { private final JobLauncher jobLauncher; private final JobExplorer jobExplorer; + private final JobRepository jobRepository; // 생성자 주입을 위해 필드 추가 @Value("${qsp.master-admin-user-batch-url}") private String qspInterfaceUrl; @@ -32,6 +37,26 @@ public class JobLauncherController { @Value("${spring.profiles.scheduler}") private String scheduler; + /** + * 서버 시작 시 실행 중(STARTED)인 상태로 남은 Job들을 FAILED 처리 + */ + @EventListener(ApplicationReadyEvent.class) + public void resetFailedJobs() { + log.info("Checking for 'STARTED' jobs to reset after reboot..."); + for (String jobName : jobs.keySet()) { + Set runningExecutions = jobExplorer.findRunningJobExecutions(jobName); + for (JobExecution execution : runningExecutions) { + execution.setStatus(BatchStatus.FAILED); + execution.setExitStatus(ExitStatus.FAILED.addExitDescription("Reset on application startup")); + execution.setEndTime(LocalDateTime.now()); // new Date() 대신 LocalDateTime.now() 사용 + jobRepository.update(execution); + log.info("Reset job execution {} for job {}", execution.getId(), jobName); + } + } + } + + + /** * 특정 Job을 매핑으로 실행하는 메소드 *