Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#243] Spring Batch를 사용한 게스트 모집 글 상태 변경 자동화 #254

Merged
merged 5 commits into from
Dec 2, 2023

Conversation

Hchanghyeon
Copy link
Member

@Hchanghyeon Hchanghyeon commented Dec 1, 2023

👨‍💻 작업 사항

📑 PR 개요

  • Spring Batch와 스케줄러를 이용하여 게스트 모집 상태 변경을 자동화 한다.

✅ 작업 목록

  • build.gradle에 Spring Batch 의존성 추가
  • dev DB에 Spring Batch 전용 테이블 구성
  • 스케줄러 설정
  • 모집 중 -> 모집 마감 Task 구현
  • 모집 마감 -> 경기 종료 Task 구현
  • 2개의 Task를 이어 Batch Job 설정

프로젝트 구성도 및 환경
배치3


🙏 리뷰어에게

  • Spring Batch 작업의 Step을 Chunk와 Tasklet 2가지 방식으로 구현할 수 있는데 Tasklet을 선택했습니다.
  • 용어 정리
    • Job: 배치 처리 과정(Step)을 하나의 단위로 만든 객체
    • Step: Job의 배치 처리를 정의하고 순차적인 단계를 캡슐화(실질적인 작업을 하는 곳)
      • Tasklet: 하나의 메서드로 구성되어있는 간단한 인터페이스이고 보통 배치 처리 과정이 쉬운 경우 사용 됨(대규모에는 적합하지 않음)
      • Chunk: 한 번에 하나씩 데이터(row)를 읽어 Chunk라는 덩어리를 만든 뒤, Chunk 단위로 트랜잭션을 다루는 방법(대용량 데이터를 처리할 경우 사용)
  • Spring Batch 작업이 도는 데이터 레코드의 수가 비지니스 특성상 엄청 많지 않다고 판단되어 Tasklet으로 선택했습니다!
    • 더 간단하고 구현하기 편해서

Prefix

PR 코멘트를 작성할 때 항상 Prefix를 붙여주세요.

  • P1: 꼭 반영해주세요 (Request changes)
  • P2: 적극적으로 고려해주세요 (Request changes)
  • P3: 웬만하면 반영해 주세요 (Comment)
  • P4: 반영해도 좋고 넘어가도 좋습니다 (Approve)
  • P5: 그냥 사소한 의견입니다 (Approve)

@Hchanghyeon Hchanghyeon requested review from a team, kylekim2123 and jay-so and removed request for a team December 1, 2023 07:34
@Hchanghyeon Hchanghyeon self-assigned this Dec 1, 2023
@Hchanghyeon Hchanghyeon added the 신규 기능 [feat] 새로운 기능을 추가한다. label Dec 1, 2023
@Hchanghyeon Hchanghyeon added this to the 6차 스프린트 milestone Dec 1, 2023
@Hchanghyeon Hchanghyeon linked an issue Dec 1, 2023 that may be closed by this pull request
5 tasks
Comment on lines 24 to 33
public Job job(
final JobRepository jobRepository,
final Step updateGameStatusToClosedStep,
final Step updateGameStatusToEndedStep
) {
return new JobBuilder("updateGameStatus", jobRepository)
.start(updateGameStatusToClosedStep)
.next(updateGameStatusToEndedStep)
.build();
}
Copy link
Member Author

@Hchanghyeon Hchanghyeon Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나의 Job을 의미하며
.start()에 들어가는 Step은 게임 상태를 모집 중 -> 모집 마감으로 변경하는 Step이고
.next()에 들어가는 Step은 게임 상태를 모집 마감 -> 경기 종료로 변경하는 Step입니다.

한마디로 하나의 Job안에 2개의 Step이 들어있고 .start()를 이용하여 첫번째 Step을 진행하고 .next()를 이용하여 두번째 Step이 진행됩니다!

Comment on lines 35 to 43
@Bean
public Tasklet gameClosedTasklet() {
return new GameClosedTasklet(gameRepository);
}

@Bean
public Tasklet gameEndedTasklet() {
return new GameEndedTasklet(gameRepository);
}
Copy link
Member Author

@Hchanghyeon Hchanghyeon Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tasklet 객체는 Step에 사용되는 실제로 배치 작업을 수행하는 객체 입니다.

Comment on lines 26 to 43
@Scheduled(cron = "0 0/30 * * * *")
public void runJob() {
try {
jobLauncher.run(
job,
new JobParametersBuilder()
.addString("dateTime", LocalDateTime.now().toString())
.toJobParameters()
);
} catch (
JobExecutionAlreadyRunningException |
JobInstanceAlreadyCompleteException |
JobParametersInvalidException |
JobRestartException e
) {
log.error("[Scheduler - Batch] run job exception: ", e);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매 30분마다 jobLauncher에 의해 설정 해둔 Job이 수행됩니다.

@Hchanghyeon Hchanghyeon marked this pull request as ready for review December 1, 2023 08:14
@Hchanghyeon Hchanghyeon changed the title [게스트모집] Spring Batch를 사용한 모집 상태 변경 자동화 [#243] Spring Batch를 사용한 게스트 모집 글 상태 변경 자동화 Dec 1, 2023
Copy link
Collaborator

@charlesuu charlesuu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

창현님 설명 상세하게 작성해주시고, 라이브로도 한 번 설명해주셔서 잘 이해할 수 있었습니다. 짱짱맨~~

Copy link
Collaborator

@hanjo8813 hanjo8813 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spring 5로 넘어오면서 레퍼런스가 많이 없어서 batch 까지는 못할것 같다고 생각했는데.. 역시;;
굿굿 👍

Comment on lines 17 to 20
public class BatchConfig extends DefaultBatchConfiguration {

@Bean
public Job job(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재는 job이 하나지만, 여러개로 늘어날 가능성을 고려해 네이밍하면 좋을것 같아요
BatchConfig -> 어쩌구BatchConfig

그리고 job bean도 중복될수 있으니 name 파라미터나 메소드명으로 네이밍 신경쓰기~!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀하신대로 확장 가능성을 고려하여 Class 파일명, 메서드명 모두 수정했습니다! 감사합니다 👍👍

Copy link

sonarqubecloud bot commented Dec 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

38.6% 38.6% Coverage
0.0% 0.0% Duplication

@Hchanghyeon Hchanghyeon merged commit a5c61f6 into dev Dec 2, 2023
@Hchanghyeon Hchanghyeon deleted the feature/#243 branch December 2, 2023 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
신규 기능 [feat] 새로운 기능을 추가한다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[게스트모집] Spring Batch를 이용한 모집 상태 변경 자동화
3 participants