dev #187
@ -1,7 +1,5 @@
|
|||||||
package com.interplug.qcast.batch;
|
package com.interplug.qcast.batch;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ -23,7 +21,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class JobLauncherController {
|
public class JobLauncherController {
|
||||||
private final Map<String, Job> jobs;
|
private final Map<String, Job> jobs; // 여러 Job을 주입받도록 변경
|
||||||
|
|
||||||
private final JobLauncher jobLauncher;
|
private final JobLauncher jobLauncher;
|
||||||
private final JobExplorer jobExplorer;
|
private final JobExplorer jobExplorer;
|
||||||
|
|
||||||
@ -35,16 +34,26 @@ public class JobLauncherController {
|
|||||||
|
|
||||||
// 현재 실행 중인 Job 추적을 위한 Set
|
// 현재 실행 중인 Job 추적을 위한 Set
|
||||||
private final Set<String> runningJobs = ConcurrentHashMap.newKeySet();
|
private final Set<String> runningJobs = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 특정 Job을 매핑으로 실행하는 메소드
|
* 특정 Job을 매핑으로 실행하는 메소드
|
||||||
|
*
|
||||||
|
* @param jobName
|
||||||
|
* @return
|
||||||
|
* @throws JobInstanceAlreadyCompleteException
|
||||||
|
* @throws JobExecutionAlreadyRunningException
|
||||||
|
* @throws JobParametersInvalidException
|
||||||
|
* @throws JobRestartException
|
||||||
*/
|
*/
|
||||||
@GetMapping("/batch/job/{jobName}")
|
@GetMapping("/batch/job/{jobName}") // Path Variable로 jobName을 받음
|
||||||
public Map<String, Object> launchJob(@PathVariable String jobName) {
|
public Map<String, Object> launchJob(@PathVariable String jobName)
|
||||||
Job job = jobs.get(jobName);
|
throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException,
|
||||||
Map<String, Object> resultMap = new HashMap<>();
|
JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
|
|
||||||
|
Job job = jobs.get(jobName);
|
||||||
|
Map<String, Object> resultMap = new HashMap<String, Object>();
|
||||||
if (job == null) {
|
if (job == null) {
|
||||||
|
// return "Job " + jobName + " not found";
|
||||||
resultMap.put("code", "FAILED");
|
resultMap.put("code", "FAILED");
|
||||||
resultMap.put("message", "Job" + jobName + " not found");
|
resultMap.put("message", "Job" + jobName + " not found");
|
||||||
return resultMap;
|
return resultMap;
|
||||||
@ -52,171 +61,180 @@ public class JobLauncherController {
|
|||||||
|
|
||||||
// 실행 중인 Job 확인
|
// 실행 중인 Job 확인
|
||||||
if (runningJobs.contains(jobName) || isJobRunning(jobName)) {
|
if (runningJobs.contains(jobName) || isJobRunning(jobName)) {
|
||||||
resultMap.put("code", "ALREADY_RUNNING");
|
log.warn("Job {} is already running, skipping execution", jobName);
|
||||||
resultMap.put("message", "Job " + jobName + " is already running");
|
resultMap.put("code", "FAILED");
|
||||||
|
resultMap.put("message", "Job "+ jobName +" is already running, skipping execution");
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
runningJobs.add(jobName);
|
|
||||||
|
|
||||||
JobParameters jobParameters = new JobParametersBuilder()
|
JobParameters jobParameters = new JobParametersBuilder().addString("jobName", jobName)
|
||||||
.addString("jobName", jobName)
|
.addDate("time", new Date()).toJobParameters();
|
||||||
.addDate("time", new Date())
|
|
||||||
.toJobParameters();
|
|
||||||
|
|
||||||
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
|
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BatchStatus status = jobExecution.getStatus();
|
BatchStatus status = jobExecution.getStatus();
|
||||||
ExitStatus exitStatus = jobExecution.getExitStatus();
|
ExitStatus exitStatus = jobExecution.getExitStatus();
|
||||||
|
|
||||||
resultMap.put("code", status.toString());
|
resultMap.put("code", status.toString());
|
||||||
resultMap.put("message", exitStatus.getExitDescription());
|
resultMap.put("message", exitStatus.getExitDescription());
|
||||||
|
|
||||||
} catch (JobExecutionAlreadyRunningException e) {
|
|
||||||
resultMap.put("code", "ALREADY_RUNNING");
|
|
||||||
resultMap.put("message", "Job " + jobName + " is already running");
|
|
||||||
} catch (Exception e) {
|
|
||||||
resultMap.put("code", "FAILED");
|
|
||||||
resultMap.put("message", "Error: " + e.getMessage());
|
|
||||||
} finally {
|
|
||||||
runningJobs.remove(jobName);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// return "Job " + jobName + " started";
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Q.CAST 판매점 / 사용자 / 즐겨찾기 / 노출 아이템 동기화 배치
|
* Q.CAST 판매점 / 사용자 / 즐겨찾기 / 노출 아이템 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 30 23 * * *")
|
// @Scheduled(cron = "*/5 * * * * *")
|
||||||
public String storeAdditionalJob() {
|
@Scheduled(cron = "0 50 23 * * *")
|
||||||
|
public String storeAdditionalJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("storeAdditionalJob");
|
return executeScheduledJob("storeAdditionalJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 아이템 동기화 배치
|
* 아이템 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 30 02 * * *")
|
@Scheduled(cron = "0 30 02 * * *")
|
||||||
public String materialJob() {
|
public String materialJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("materialJob");
|
return executeScheduledJob("materialJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BOM 아이템 동기화 배치
|
* BOM 아이템 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 40 02 * * *")
|
@Scheduled(cron = "0 40 02 * * *")
|
||||||
public String bomJob() {
|
public String bomJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("bomJob");
|
return executeScheduledJob("bomJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 영업사원 동기화 배치
|
* 영업사원 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 40 03 * * *")
|
@Scheduled(cron = "0 40 03 * * *")
|
||||||
public String businessChargerJob() {
|
public String businessChargerJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("businessChargerJob");
|
return executeScheduledJob("businessChargerJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 관리자 유저 동기화 배치
|
* 관리자 유저 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 0 01 * * *")
|
@Scheduled(cron = "0 30 01 * * *")
|
||||||
public String adminUserJob() {
|
public String adminUserJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("adminUserJob");
|
return executeScheduledJob("adminUserJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 가격 동기화 배치
|
* 가격 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 30 0 * * *") // 시간 조정: 00:30
|
@Scheduled(cron = "0 20 00 * * *")
|
||||||
public String priceJob() {
|
public String priceJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("priceJob");
|
return executeScheduledJob("priceJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 공통코드 M_COMM_H, M_COMM_L 동기화 배치
|
* 공통코드 M_COMM_H, M_COMM_L 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 10 03 * * *")
|
@Scheduled(cron = "0 10 03 * * *")
|
||||||
public String commonCodeJob() {
|
public String commonCodeJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("commonCodeJob");
|
return executeScheduledJob("commonCodeJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Q.CAST 견적특이사항 / 아이템 표시, 미표시 동기화 배치
|
* Q.CAST 견적특이사항 / 아이템 표시, 미표시 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 45 23 * * *") // 시간 조정: 23:45
|
@Scheduled(cron = "0 30 23 * * *")
|
||||||
public String specialNoteDispItemAdditionalInfoJob() {
|
public String specialNoteDispItemAdditionalInfoJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
return executeScheduledJob("specialNoteDispItemAdditionalJob");
|
return executeScheduledJob("specialNoteDispItemAdditionalJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plan Confrim 동기화 배치
|
* Plan Confrim 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 05 04 * * *")
|
@Scheduled(cron = "0 05 04 * * *")
|
||||||
public String planConfirmJob() {
|
public String planConfirmJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("planConfirmJob");
|
return executeScheduledJob("planConfirmJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 견적서 전송 동기화 배치
|
* 견적서 전송 동기화 배치
|
||||||
|
*
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 20 04 * * *")
|
@Scheduled(cron = "0 20 04 * * *")
|
||||||
public String estimateSyncJob() {
|
public String estimateSyncJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
|
|
||||||
return executeScheduledJob("estimateSyncJob");
|
return executeScheduledJob("estimateSyncJob");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 공통 스케줄러 실행 메소드
|
* 공통 스케줄러 실행 메소드
|
||||||
*/
|
*/
|
||||||
private String executeScheduledJob(String jobName) {
|
private String executeScheduledJob(String jobName) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||||
// 1. 가장 먼저 스케줄러 설정 확인
|
|
||||||
if (!"Y".equals(scheduler) && !"materialJob".equals(jobName) && !"commonCodeJob".equals(jobName)) {
|
|
||||||
log.info("Scheduler disabled, skipping job {}", jobName);
|
|
||||||
return "Scheduler disabled";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Job 존재 확인
|
|
||||||
Job job = jobs.get(jobName);
|
Job job = jobs.get(jobName);
|
||||||
if (job == null) {
|
if (job == null) {
|
||||||
log.error("Job {} not found", jobName);
|
log.error("Job {} not found", jobName);
|
||||||
return "Job " + jobName + " not found";
|
return "Job " + jobName + " not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 다른 Job 실행 중인지 확인
|
if (!"Y".equals(scheduler)
|
||||||
if (isAnyJobRunning()) {
|
&& !"materialJob".equals(jobName)
|
||||||
log.warn("Another job is running, skipping job {}", jobName);
|
&& !"commonCodeJob".equals(jobName)) {
|
||||||
return "Another job is running";
|
log.info("Scheduler disabled, skipping job {}", jobName);
|
||||||
|
return "Scheduler disabled";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 같은 Job이 실행 중인지 확인
|
// 실행 중인 Job 확인
|
||||||
if (runningJobs.contains(jobName) || isJobRunning(jobName)) {
|
if (runningJobs.contains(jobName) || isJobRunning(jobName)) {
|
||||||
log.warn("Job {} is already running", jobName);
|
log.warn("Job {} is already running, skipping execution", jobName);
|
||||||
return "Job already running";
|
return "Job already running";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Job 실행
|
JobParameters jobParameters =
|
||||||
try {
|
new JobParametersBuilder().addDate("time", new Date()).toJobParameters();
|
||||||
runningJobs.add(jobName);
|
|
||||||
log.info("Starting job {}", jobName);
|
|
||||||
|
|
||||||
JobParameters jobParameters = new JobParametersBuilder()
|
jobLauncher.run(job, jobParameters);
|
||||||
.addDate("time", new Date())
|
|
||||||
.toJobParameters();
|
|
||||||
|
|
||||||
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
|
return jobName+ " executed successfully";
|
||||||
log.info("Job {} started successfully", jobName);
|
|
||||||
return "OK";
|
|
||||||
|
|
||||||
} catch (JobExecutionAlreadyRunningException e) {
|
|
||||||
log.warn("Job {} is already running: {}", jobName, e.getMessage());
|
|
||||||
return "Job already running";
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Error executing job {}: {}", jobName, e.getMessage());
|
|
||||||
return "Error: " + e.getMessage();
|
|
||||||
} finally {
|
|
||||||
runningJobs.remove(jobName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -240,75 +258,4 @@ public class JobLauncherController {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 실행 중인 Job이 있는지 확인
|
|
||||||
*/
|
|
||||||
private boolean isAnyJobRunning() {
|
|
||||||
String[] jobNames = {"storeAdditionalJob", "materialJob", "bomJob", "businessChargerJob",
|
|
||||||
"adminUserJob", "priceJob", "commonCodeJob", "specialNoteDispItemAdditionalJob",
|
|
||||||
"planConfirmJob", "estimateSyncJob"};
|
|
||||||
|
|
||||||
return Arrays.stream(jobNames)
|
|
||||||
.anyMatch(this::isJobRunning);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 현재 실행 중인 Job 목록 조회
|
|
||||||
*/
|
|
||||||
@GetMapping("/batch/running-jobs")
|
|
||||||
public Map<String, Object> getRunningJobs() {
|
|
||||||
Map<String, Object> result = new HashMap<>();
|
|
||||||
result.put("runningJobs", new ArrayList<>(runningJobs));
|
|
||||||
|
|
||||||
Map<String, Object> jobStatuses = new HashMap<>();
|
|
||||||
String[] jobNames = {"storeAdditionalJob", "materialJob", "bomJob", "businessChargerJob",
|
|
||||||
"adminUserJob", "priceJob", "commonCodeJob", "specialNoteDispItemAdditionalJob",
|
|
||||||
"planConfirmJob", "estimateSyncJob"};
|
|
||||||
|
|
||||||
for (String jobName : jobNames) {
|
|
||||||
jobStatuses.put(jobName, getJobStatus(jobName));
|
|
||||||
}
|
|
||||||
|
|
||||||
result.put("jobStatuses", jobStatuses);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 특정 Job의 상태 정보 조회
|
|
||||||
*/
|
|
||||||
private Map<String, Object> getJobStatus(String jobName) {
|
|
||||||
Map<String, Object> jobStatus = new HashMap<>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
List<JobInstance> jobInstances = jobExplorer.findJobInstancesByJobName(jobName, 0, 1);
|
|
||||||
if (jobInstances.isEmpty()) {
|
|
||||||
jobStatus.put("status", "NEVER_EXECUTED");
|
|
||||||
return jobStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
JobInstance latestJobInstance = jobInstances.get(0);
|
|
||||||
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(latestJobInstance);
|
|
||||||
|
|
||||||
if (!jobExecutions.isEmpty()) {
|
|
||||||
JobExecution latestExecution = jobExecutions.get(0);
|
|
||||||
jobStatus.put("status", latestExecution.getStatus().toString());
|
|
||||||
jobStatus.put("startTime", latestExecution.getStartTime());
|
|
||||||
jobStatus.put("endTime", latestExecution.getEndTime());
|
|
||||||
|
|
||||||
// Duration 계산 (LocalDateTime용)
|
|
||||||
if (latestExecution.getEndTime() != null && latestExecution.getStartTime() != null) {
|
|
||||||
Duration duration = Duration.between(latestExecution.getStartTime(), latestExecution.getEndTime());
|
|
||||||
jobStatus.put("durationSeconds", duration.getSeconds());
|
|
||||||
jobStatus.put("durationMillis", duration.toMillis());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
jobStatus.put("status", "ERROR");
|
|
||||||
jobStatus.put("error", e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return jobStatus;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user