Merge pull request 'resetFailedJobs가 실행되면서 DB에 끼어있던(STARTED) 상태들을 모두 실패(FAILED)로 바꿈' (#284) from dev into dev-deploy

Reviewed-on: #284
This commit is contained in:
ysCha 2025-12-31 14:23:32 +09:00
commit de9219cf56

View File

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