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을 매핑으로 실행하는 메소드 *