Skip to content

Commit

Permalink
feat: Batch, Scheduler 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Hchanghyeon committed Dec 2, 2023
1 parent ae8c56a commit ded8ea4
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/main/java/kr/pickple/back/batch/BatchConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kr.pickple.back.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import kr.pickple.back.game.repository.GameRepository;
import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class BatchConfig extends DefaultBatchConfiguration {

private final GameRepository gameRepository;

@Bean
public Job job(
final JobRepository jobRepository,
final Step updateGameStatusToClosedStep,
final Step updateGameStatusToEndedStep
) {
return new JobBuilder("updateGameStatus", jobRepository)
.start(updateGameStatusToClosedStep)
.next(updateGameStatusToEndedStep)
.build();
}

@Bean
public Tasklet gameClosedTasklet() {
return new GameClosedTasklet(gameRepository);
}

@Bean
public Tasklet gameEndedTasklet() {
return new GameEndedTasklet(gameRepository);
}

@Bean
public Step updateGameStatusToClosedStep(
final JobRepository jobRepository,
final Tasklet gameClosedTasklet,
final PlatformTransactionManager transactionManager
) {
return new StepBuilder("updateGameStatusToClosedStep", jobRepository)
.tasklet(gameClosedTasklet, transactionManager)
.build();
}

@Bean
public Step updateGameStatusToEndedStep(
final JobRepository jobRepository,
final Tasklet gameEndedTasklet,
final PlatformTransactionManager transactionManager
) {
return new StepBuilder("updateGameStatusToEndedStep", jobRepository)
.tasklet(gameEndedTasklet, transactionManager)
.build();
}
}
44 changes: 44 additions & 0 deletions src/main/java/kr/pickple/back/batch/BatchScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kr.pickple.back.batch;

import java.time.LocalDateTime;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
@RequiredArgsConstructor
public class BatchScheduler {

private final JobLauncher jobLauncher;
private final Job job;

@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);
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/kr/pickple/back/batch/GameClosedTasklet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kr.pickple.back.batch;

import static kr.pickple.back.game.domain.GameStatus.*;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import kr.pickple.back.game.domain.Game;
import kr.pickple.back.game.repository.GameRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class GameClosedTasklet implements Tasklet {

private final GameRepository gameRepository;

@Override
public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) {
log.info("[Batch Start] - Update Game Status");

final LocalDateTime nowDateTime = LocalDateTime.now();
final List<Game> openGames = gameRepository.findGamesByStatusAndPlayDateStartTimeBeforeNow(OPEN,
nowDateTime);

openGames.forEach(openGame -> {
openGame.updateGameStatus(CLOSED);
log.info("[Batch Processing] - OPEN -> CLOSED updatedGameId: {}", openGame.getId());
});

log.info("[Batch End] - OPEN -> CLOSED UpdatedGameCount: {}", openGames.size());

return RepeatStatus.FINISHED;
}
}
41 changes: 41 additions & 0 deletions src/main/java/kr/pickple/back/batch/GameEndedTasklet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kr.pickple.back.batch;

import static kr.pickple.back.game.domain.GameStatus.*;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import kr.pickple.back.game.domain.Game;
import kr.pickple.back.game.repository.GameRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class GameEndedTasklet implements Tasklet {

private final GameRepository gameRepository;

@Override
public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) {
log.info("[Batch Start] - Update Game Status");

final LocalDateTime nowDateTime = LocalDateTime.now();
final List<Game> closedGames = gameRepository.findGamesByStatusAndPlayDateEndTimeBeforeNow(CLOSED,
nowDateTime);

closedGames.forEach(closedGame -> {
closedGame.updateGameStatus(ENDED);
log.info("[Batch Processing] - CLOSED -> ENDED updatedGameId: {}", closedGame.getId());
});

log.info("[Batch End] - CLOSED -> ENDED UpdatedGameCount: {}", closedGames.size());

return RepeatStatus.FINISHED;
}
}
2 changes: 2 additions & 0 deletions src/main/java/kr/pickple/back/common/config/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
public class CommonConfig {

}


0 comments on commit ded8ea4

Please sign in to comment.