-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from COW-dev/main
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/kotlin/com/mjucow/eatda/scheduled/ExpiredBannerBatchJob.kt
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,72 @@ | ||
package com.mjucow.eatda.scheduled | ||
|
||
import com.mjucow.eatda.jooq.tables.Banner.BANNER | ||
import com.mjucow.eatda.jooq.tables.records.ExpiredBannerRecord | ||
import org.jooq.DSLContext | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.support.TransactionTemplate | ||
import java.time.Instant | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
@Component | ||
class ExpiredBannerBatchJob( | ||
private val db: DSLContext, | ||
private val transactionTemplate: TransactionTemplate, | ||
) { | ||
@Scheduled(cron = "0 0 22 * * *", zone = "Asia/Seoul") // 매일 오후 10시 동작 | ||
fun scheduleTaskUsingCronExpression() { | ||
val now = Instant.now() | ||
transactionTemplate.execute { | ||
val expiredBanners = findAllExpiredBanners(now) | ||
if (expiredBanners.isEmpty()) { | ||
return@execute | ||
} | ||
|
||
expiredBannerBulkInsert(expiredBanners) | ||
|
||
bulkDeleteBanners(expiredBanners) | ||
} | ||
} | ||
|
||
private fun findAllExpiredBanners(now: Instant): List<ExpiredTargetBanner> { | ||
return db | ||
.select(BANNER.ID, BANNER.LINK, BANNER.IMAGE_ADDRESS, BANNER.EXPIRED_AT) | ||
.from(BANNER) | ||
.where( | ||
BANNER.EXPIRED_AT.isNotNull | ||
.and(BANNER.EXPIRED_AT.lessOrEqual(LocalDateTime.ofInstant(now, ZONE_ID))) | ||
) | ||
.fetch() | ||
.into(ExpiredTargetBanner::class.java) | ||
.toList() | ||
} | ||
|
||
private fun expiredBannerBulkInsert(expireTargetBanners: List<ExpiredTargetBanner>) { | ||
val expiredBanners = expireTargetBanners.map { banner -> | ||
ExpiredBannerRecord().apply { | ||
this.link = banner.link | ||
this.imageAddress = banner.imageAddress | ||
this.expiredAt = banner.expiredAt | ||
} | ||
} | ||
db.batchInsert(expiredBanners).execute() | ||
} | ||
|
||
private fun bulkDeleteBanners(expiredBanners: List<ExpiredTargetBanner>) { | ||
val deletedBannerIds = expiredBanners.map { it.id } | ||
db.deleteFrom(BANNER).where(BANNER.ID.`in`(deletedBannerIds)).execute() | ||
} | ||
|
||
data class ExpiredTargetBanner( | ||
val id: Long, | ||
val link: String, | ||
val imageAddress: String, | ||
val expiredAt: LocalDateTime, | ||
) | ||
|
||
companion object { | ||
val ZONE_ID: ZoneId = ZoneId.of("Asia/Seoul") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/mjucow/eatda/scheduled/ScheduleJobConfig.kt
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,17 @@ | ||
package com.mjucow.eatda.scheduled | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.scheduling.annotation.EnableScheduling | ||
|
||
@Configuration | ||
@EnableScheduling | ||
class ScheduleJobConfig { | ||
// @Bean | ||
// fun taskScheduler(): TaskScheduler { | ||
// val threadPoolTaskScheduler = ThreadPoolTaskScheduler().apply { | ||
// poolSize = 5 | ||
// setThreadNamePrefix("ThreadPoolTaskScheduler") | ||
// } | ||
// return threadPoolTaskScheduler | ||
// } | ||
} |
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
79 changes: 79 additions & 0 deletions
79
src/test/kotlin/com/mjucow/eatda/scheduled/ExpiredBannerBatchJobTest.kt
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,79 @@ | ||
package com.mjucow.eatda.scheduled | ||
|
||
import com.mjucow.eatda.domain.AbstractDataTest | ||
import com.mjucow.eatda.domain.banner.entity.objectmother.BannerMother | ||
import com.mjucow.eatda.jooq.tables.ExpiredBanner.EXPIRED_BANNER | ||
import com.mjucow.eatda.jooq.tables.records.BannerRecord | ||
import com.mjucow.eatda.jooq.tables.records.ExpiredBannerRecord | ||
import com.mjucow.eatda.persistence.banner.BannerRepository | ||
import org.assertj.core.api.Assertions | ||
import org.jooq.DSLContext | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.context.annotation.Import | ||
import java.time.LocalDateTime | ||
import java.util.stream.IntStream | ||
|
||
@Import(value = [ExpiredBannerBatchJob::class]) | ||
class ExpiredBannerBatchJobTest : AbstractDataTest() { | ||
@Autowired | ||
lateinit var db: DSLContext | ||
|
||
@Autowired | ||
lateinit var bannerRepository: BannerRepository | ||
|
||
@Autowired | ||
lateinit var expiredBannerBatchJob: ExpiredBannerBatchJob | ||
|
||
@DisplayName("배치할 거 없으면 따로 동작 안하기") | ||
@Test | ||
fun test1() { | ||
// given | ||
|
||
// when | ||
expiredBannerBatchJob.scheduleTaskUsingCronExpression() | ||
|
||
// then | ||
val createdExpiredBanners = findAllExpiredBanner() | ||
Assertions.assertThat(createdExpiredBanners).isEmpty() | ||
} | ||
|
||
@DisplayName("배치할 거 있으면 expiredBanner로 데이터 복사하기") | ||
@Test | ||
fun test2() { | ||
// given | ||
val now = LocalDateTime.now(ZONE_ID) | ||
val expiredAt = now.minusDays(1) | ||
val count = 3 | ||
db.batchInsert( | ||
IntStream | ||
.range(0, count) | ||
.mapToObj { | ||
BannerRecord().apply { | ||
this.imageAddress = BannerMother.IMAGE_ADDRESS | ||
this.link = BannerMother.LINK | ||
this.expiredAt = expiredAt | ||
this.createdAt = now | ||
this.updatedAt = now | ||
} | ||
}.toList() | ||
).execute() | ||
|
||
// when | ||
expiredBannerBatchJob.scheduleTaskUsingCronExpression() | ||
|
||
// then | ||
val createdExpiredBanners = findAllExpiredBanner() | ||
Assertions.assertThat(createdExpiredBanners).hasSize(count) | ||
Assertions.assertThat(bannerRepository.findAll()).isEmpty() | ||
} | ||
|
||
private fun findAllExpiredBanner(): List<ExpiredBannerRecord> { | ||
return db.selectQuery(EXPIRED_BANNER).toList() | ||
} | ||
|
||
companion object { | ||
val ZONE_ID = ExpiredBannerBatchJob.ZONE_ID | ||
} | ||
} |