-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae8c56a
commit ded8ea4
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/main/java/kr/pickple/back/batch/GameClosedTasklet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
public class CommonConfig { | ||
|
||
} | ||
|
||
|