diff --git a/src/main/java/com/interplug/qcast/batch/JobLauncherController.java b/src/main/java/com/interplug/qcast/batch/JobLauncherController.java index f2bcddbd..0dac1462 100644 --- a/src/main/java/com/interplug/qcast/batch/JobLauncherController.java +++ b/src/main/java/com/interplug/qcast/batch/JobLauncherController.java @@ -1,5 +1,6 @@ package com.interplug.qcast.batch; +import java.time.Duration; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -292,19 +293,42 @@ public class JobLauncherController { */ private boolean isJobRunning(String jobName) { try { - List jobInstances = jobExplorer.findJobInstancesByJobName(jobName, 0, 1); - if (jobInstances.isEmpty()) { + Set runningExecutions = jobExplorer.findRunningJobExecutions(jobName); + if (runningExecutions.isEmpty()) { return false; } - JobInstance latestJobInstance = jobInstances.get(0); - List jobExecutions = jobExplorer.getJobExecutions(latestJobInstance); + boolean isActuallyBlocked = false; + for (JobExecution execution : runningExecutions) { + LocalDateTime startTime = execution.getStartTime(); + if (startTime == null) continue; - return jobExecutions.stream() - .anyMatch(execution -> execution.getStatus() == BatchStatus.STARTED - || execution.getStatus() == BatchStatus.STARTING); + // 현재 시간과 시작 시간의 차이 계산 + Duration duration = Duration.between(startTime, LocalDateTime.now()); + long hours = duration.toHours(); + + if (hours >= 1) { + // 1시간 이상 경과한 경우 로그 출력 및 DB 강제 업데이트 + log.warn("Job {} (Execution ID: {}) 가 실행된 지 {}시간이 지났습니다. 상태를 FAILED로 초기화하고 재실행을 시도합니다.", + jobName, execution.getId(), hours); + + execution.setStatus(BatchStatus.FAILED); + execution.setExitStatus(ExitStatus.FAILED.addExitDescription("Forcefully reset: Running for more than 1 hour")); + execution.setEndTime(LocalDateTime.now()); + jobRepository.update(execution); + + log.info("Job {} 의 이전 실행 상태가 성공적으로 초기화되었습니다.", jobName); + } else { + // 1시간 미만으로 실행 중인 잡이 있다면 진짜 실행 중인 것으로 간주 + log.info("Job {} 가 현재 실행 중입니다. (시작 시간: {}, 경과 시간: {}분)", + jobName, startTime, duration.toMinutes()); + isActuallyBlocked = true; + } + } + + return isActuallyBlocked; } catch (Exception e) { - log.error("Error checking job running status: {}", e.getMessage()); + log.error("Job 상태 확인 중 오류 발생: {}", e.getMessage()); return false; } }