add make dataset
This commit is contained in:
0
imagery-make-dataset/dev.backup
Executable file → Normal file
0
imagery-make-dataset/dev.backup
Executable file → Normal file
@@ -1,8 +1,10 @@
|
||||
package com.kamco.cd.geojsonscheduler.batch;
|
||||
|
||||
import com.kamco.cd.geojsonscheduler.listener.BatchHistoryListener;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
@@ -19,8 +21,11 @@ public class ExportGeoJsonJobConfig {
|
||||
private final ExportGeoJsonTasklet exportGeoJsonTasklet;
|
||||
|
||||
@Bean
|
||||
public Job exportGeoJsonJob() {
|
||||
return new JobBuilder("exportGeoJsonJob", jobRepository).start(exportGeoJsonStep()).build();
|
||||
public Job exportGeoJsonJob(BatchHistoryListener historyListener) { // 1. 리스너 주입 받기
|
||||
return new JobBuilder("exportGeoJsonJob", jobRepository)
|
||||
.listener(historyListener) // 2. 리스너 등록
|
||||
.start(exportGeoJsonStep())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -39,6 +39,9 @@ public class ExportGeoJsonTasklet implements Tasklet {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
|
||||
|
||||
// 1. StepContext를 통해 바로 가져오기 (가장 추천)
|
||||
String jobName = chunkContext.getStepContext().getJobName();
|
||||
|
||||
// 진행중인 회차 중, complete_cnt 가 존재하는 회차 목록 가져오기
|
||||
List<AnalCntInfo> analList = repository.findAnalCntInfoList();
|
||||
|
||||
@@ -47,7 +50,10 @@ public class ExportGeoJsonTasklet implements Tasklet {
|
||||
continue;
|
||||
}
|
||||
|
||||
//추론 ID
|
||||
String resultUid = info.getResultUid();
|
||||
|
||||
//insert 하기 jobname, resultUid , 시작시간
|
||||
// 어제까지 검수 완료된 총 데이터의 도엽별 목록 가져오기
|
||||
List<AnalMapSheetList> analMapList = repository.findCompletedAnalMapSheetList(info.getAnalUid());
|
||||
|
||||
@@ -56,6 +62,7 @@ public class ExportGeoJsonTasklet implements Tasklet {
|
||||
continue;
|
||||
}
|
||||
|
||||
//insert 하기 jobname, resultUid , 시작시간
|
||||
boolean anyProcessed = false;
|
||||
|
||||
for (AnalMapSheetList mapSheet : analMapList) {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.kamco.cd.geojsonscheduler.listener;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionListener;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class BatchHistoryListener implements JobExecutionListener {
|
||||
|
||||
private final BatchHistoryService batchHistoryService;
|
||||
|
||||
public BatchHistoryListener(BatchHistoryService batchHistoryService) {
|
||||
this.batchHistoryService = batchHistoryService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeJob(JobExecution jobExecution) {
|
||||
// 1. UUID 생성 (또는 파라미터에서 가져오기)
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
// 2. JobExecutionContext에 UUID 저장 (afterJob에서 쓰기 위해)
|
||||
jobExecution.getExecutionContext().put("batch_uuid", uuid);
|
||||
|
||||
// 3. Job 이름과 비즈니스 ID(파라미터 등) 가져오기
|
||||
String jobName = jobExecution.getJobInstance().getJobName();
|
||||
String businessId = jobExecution.getJobParameters().getString("id", "UNKNOWN"); // 파라미터 'id'가 있다고 가정
|
||||
|
||||
// 4. 시작 기록
|
||||
batchHistoryService.startBatch(uuid, jobName, businessId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
// 1. 저장해둔 UUID 꺼내기
|
||||
UUID uuid = (UUID) jobExecution.getExecutionContext().get("batch_uuid");
|
||||
|
||||
// 2. 성공 여부 판단 (COMPLETED면 성공, 그 외 실패)
|
||||
boolean isSuccess = jobExecution.getStatus() == BatchStatus.COMPLETED;
|
||||
|
||||
// 3. 종료 기록
|
||||
if (uuid != null) {
|
||||
batchHistoryService.finishBatch(uuid, isSuccess);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.kamco.cd.geojsonscheduler.listener;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class BatchHistoryService {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public BatchHistoryService(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 배치 시작 시 이력 저장 (INSERT)
|
||||
*/
|
||||
@Transactional
|
||||
public void startBatch(UUID uuid, String jobName, String businessId) {
|
||||
String sql = """
|
||||
INSERT INTO public.batch_history
|
||||
(uuid, job, id, created_dttm, updated_dttm, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
Timestamp now = Timestamp.valueOf(LocalDateTime.now());
|
||||
|
||||
// 초기 상태는 'STARTED'로 저장
|
||||
jdbcTemplate.update(sql,
|
||||
uuid,
|
||||
jobName,
|
||||
businessId,
|
||||
now, // created_dttm
|
||||
now, // updated_dttm
|
||||
"STARTED"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 배치 종료 시 이력 업데이트 (UPDATE)
|
||||
*/
|
||||
@Transactional
|
||||
public void finishBatch(UUID uuid, boolean isSuccess) {
|
||||
String sql = """
|
||||
UPDATE public.batch_history
|
||||
SET status = ?,
|
||||
updated_dttm = ?,
|
||||
completed_dttm = ?
|
||||
WHERE uuid = ?
|
||||
""";
|
||||
|
||||
Timestamp now = Timestamp.valueOf(LocalDateTime.now());
|
||||
String status = isSuccess ? "COMPLETED" : "FAILED";
|
||||
|
||||
jdbcTemplate.update(sql,
|
||||
status,
|
||||
now, // updated_dttm (마지막 변경 시간)
|
||||
now, // completed_dttm (완료 시간)
|
||||
uuid
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user