Skip to content

Commit

Permalink
#29 - Refactor: 장애 발생을 고려해 JobParameters(year,month)로 하여 매일 오전 4시에 배치…
Browse files Browse the repository at this point in the history
…실행하는 것으로 변경
  • Loading branch information
ahah525 committed Nov 7, 2022
1 parent 79805f7 commit ef3ad91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,12 @@ public Step makeRebateOrderItemStep1(
@StepScope
@Bean
public RepositoryItemReader<OrderItem> orderItemReader(
@Value("#{jobParameters[createDate]}") String createDateStr
@Value("#{jobParameters[year]}") int year,
@Value("#{jobParameters[month]}") int month
) {
log.info("orderItemReader 실행");
log.info("%d-%d".formatted(year, month));
// 1. 정산 데이터를 생성할 날짜 범위 구하기
// 이번 달 15일에 생성해야하는 정산 데이터 날짜 범위 = 저번 달 1일 ~ 말일
LocalDateTime createDate = Ut.date.parse(createDateStr);
// LocalDateTime targetDate = createDate.minusMonths(1);
LocalDateTime targetDate = createDate;

log.info(String.valueOf(createDate));
log.info(String.valueOf(targetDate));

int year = targetDate.getYear();
int month = targetDate.getMonthValue();
int endDay = Ut.date.getEndDay(year, month);
LocalDateTime startOfDay = Ut.date.getStartOfDay(year, month, 1); // 해당일자의 시작일시
LocalDateTime endOfDay = Ut.date.getEndOfDay(year, month, endDay); // 해당일자의 종료일시
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.mutbooks.scheduler;

import com.example.mutbooks.job.makeRebateOrderItem.MakeRebateOrderItemJobConfig;
import com.example.mutbooks.util.Ut;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
Expand All @@ -28,18 +27,18 @@ public class JobScheduler {
private final MakeRebateOrderItemJobConfig makeRebateOrderItemJobConfig;
private final Job makeRebateOrderItemJob; // 빈으로 등록한 Job 을 주입받아 사용

//매 분 00초마다 실행
@Scheduled(cron = "0 * * * * *")

// 매달 15일 오전 4시 0분 0초마다 Job 실행
// @Scheduled(cron = "0 0 4 15 * *")
// 매달 15일 오전 4시에 Job 실행해야하지만, 오류를 고려하여 매일 오전 4시에 실행
// @Scheduled(cron = "0 0 4 * * *") // TODO: 운영시 주석 해제
@Scheduled(cron = "30 * * * * *") // 개발용(매분 30초마다 실행)
public void runJob() {
log.info("scheduler 실행 " + String.valueOf(LocalDateTime.now()));

// 현재 일시 LocalDateTime -> String 변환한 값을 Job Parameter 에 담기
Map<String, JobParameter> confMap = new HashMap<>();
String createDateStr = Ut.date.format(LocalDateTime.now());
confMap.put("createDate", new JobParameter(createDateStr));
LocalDateTime rebateDate = getMakeRebateDataDateTime();
confMap.put("year", new JobParameter((long) rebateDate.getYear()));
confMap.put("month", new JobParameter((long) rebateDate.getMonthValue()));
JobParameters jobParameters = new JobParameters(confMap);

try {
Expand All @@ -54,4 +53,13 @@ public void runJob() {
throw new RuntimeException(e);
}
}

// jobParameter 값 생성
public LocalDateTime getMakeRebateDataDateTime() {
// 정산 데이터 생성 날짜 범위(15일 이후 = 1달 전, 15일 이전 = 2달 전)
// TODO : 운영시 주석 해제
// return LocalDateTime.now().getDayOfMonth() >= 15 ?
// LocalDateTime.now().minusMonths(1) : LocalDateTime.now().minusMonths(2);
return LocalDateTime.now(); // 개발용
}
}

0 comments on commit ef3ad91

Please sign in to comment.