Skip to content

Commit

Permalink
[#140] refactor: 로깅 수정 및 매직상수 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
JuHyun419 committed Sep 10, 2022
1 parent 769f1f7 commit 011716c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/main/kotlin/com/yapp/web2/batch/job/TrashRefreshJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.JobScope
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.core.launch.support.RunIdIncrementer
import org.springframework.batch.item.ItemProcessor
import org.springframework.batch.item.ItemReader
import org.springframework.batch.item.ItemWriter
import org.springframework.batch.item.support.ListItemReader
import org.springframework.context.annotation.Bean
Expand All @@ -25,41 +28,52 @@ class TrashRefreshJob(
private val jobCompletionListener: JobCompletionListener
) {

companion object {
const val TRASH_BOOKMARKS_DELETE_JOB = "TRASH_BOOKMARKS_DELETE_JOB"
const val TRASH_BOOKMARKS_DELETE_STEP = "TRASH_BOOKMARKS_DELETE_STEP"
}

private val log = LoggerFactory.getLogger(TrashRefreshJob::class.java)

@Bean("bookmarkTrashRefreshJob")
@Bean
fun bookmarkTrashRefreshJob(): Job {
return jobBuilderFactory.get("bookmarkTrashRefreshJob")
return jobBuilderFactory.get(TRASH_BOOKMARKS_DELETE_JOB)
.start(trashRefreshStep())
.incrementer(RunIdIncrementer())
.listener(jobCompletionListener)
.build()
}

@Bean
@JobScope
fun trashRefreshStep(): Step {
return stepBuilderFactory.get("trashRefreshStep")
.chunk<Bookmark, Bookmark>(10)
return stepBuilderFactory.get(TRASH_BOOKMARKS_DELETE_STEP)
.chunk<Bookmark, Bookmark>(100)
.reader(deleteBookmarkReader())
.processor(deleteBookmarkProcessor())
.writer(NoOperationItemWriter())
.build()
}

@Bean
fun deleteBookmarkReader(): ListItemReader<Bookmark> {
@StepScope
fun deleteBookmarkReader(): ItemReader<Bookmark> {
val deleteCycle = 30L
val deleteBookmarkList = bookmarkRepository.findAllByDeletedIsTrueAndDeleteTimeBefore(
LocalDateTime.now().minusDays(30)
LocalDateTime.now().minusDays(deleteCycle)
)
log.info("휴지통에서 30일이 지난 북마크는 자동으로 삭제합니다. 삭제할 북마크 갯수: ${deleteBookmarkList.size}")

return ListItemReader(deleteBookmarkList)
}

@Bean
@StepScope
fun deleteBookmarkProcessor(): ItemProcessor<Bookmark, Bookmark> {
return ItemProcessor {
log.info("Bookmark to delete info => userId: ${it.userId}, folderId: ${it.folderId}, folderName: ${it.folderName} title: ${it.title}")
log.info("휴지통에서 삭제할 북마크 정보 => " +
"userId: ${it.userId}, folderId: ${it.folderId}, folderName: ${it.folderName} title: ${it.title}"
)
bookmarkRepository.delete(it)
it
}
Expand Down

0 comments on commit 011716c

Please sign in to comment.