-
Notifications
You must be signed in to change notification settings - Fork 0
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
[feat #116] 알림 전송 배치 작업 #142
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dea3cdb
fix : 테스트 버그 수정
dlswns2480 1c695cc
feat : 배치알림 조회 구현
dlswns2480 5e745cc
feat : multi job 관련 설정
dlswns2480 8038bf0
feat : 알림 전송 step에 필요한 usecase
dlswns2480 1375034
feat : 알림 전송 step
dlswns2480 750a735
feat : 알림 전송 job
dlswns2480 fec44e8
feat : Chunk size object 관리
dlswns2480 74658e0
feat : 알림 가는 시점의 컨텐츠를 조회하는 걸로 수
dlswns2480 45e434b
feat : AlertContent 삭제 hard delete로 변경
dlswns2480 372d5df
feat : LocalDate Supplier 설정
dlswns2480 1e70e4b
feat : 배치알림, 매핑 엔티티 저장 이벤트 로직
dlswns2480 1f54fd8
feat : 배치알림, 매핑 엔티티 저장 포트, 서비
dlswns2480 404a677
edit : 메인 클래스 원상복구
dlswns2480 7295ba1
merge : conflict 해결
dlswns2480 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions
17
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertProcessor.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.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.ItemProcessor | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertProcessor( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemProcessor<AlertBatch, AlertBatch> { | ||
|
||
override fun process(alertBatch: AlertBatch): AlertBatch { | ||
alertUseCase.sendMessage(alertBatch) | ||
return alertBatch | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertReader.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,29 @@ | ||
package com.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.model.AlertBatchValue | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.ItemReader | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertReader( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemReader<AlertBatch> { | ||
|
||
private var currentPage: Int = 0 | ||
private var alertBatchList: MutableList<AlertBatch> = mutableListOf() | ||
|
||
override fun read(): AlertBatch? { | ||
if (alertBatchList.isEmpty()) { | ||
val alertBatches = alertUseCase.loadAllAlertBatch(currentPage++, AlertBatchValue.CHUNK_SIZE) | ||
alertBatchList.addAll(alertBatches) | ||
|
||
if (alertBatchList.isEmpty()) { | ||
return null | ||
} | ||
} | ||
|
||
return alertBatchList.removeFirst() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
adapters/in-batch/src/main/kotlin/com/pokit/alert/component/AlertWriter.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,19 @@ | ||
package com.pokit.alert.component | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.`in`.AlertUseCase | ||
import org.springframework.batch.item.Chunk | ||
import org.springframework.batch.item.ItemWriter | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AlertWriter( | ||
private val alertUseCase: AlertUseCase, | ||
) : ItemWriter<AlertBatch> { | ||
override fun write(alertBatches: Chunk<out AlertBatch>) { | ||
val batchIds = alertBatches.map { it.id } | ||
val alertContents = alertUseCase.fetchAllAlertContent(batchIds) | ||
|
||
alertUseCase.createAlerts(alertContents) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
adapters/in-batch/src/main/kotlin/com/pokit/alert/job/SendAlertConfig.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,43 @@ | ||
package com.pokit.alert.job | ||
|
||
import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl | ||
import com.navercorp.spring.batch.plus.kotlin.configuration.step.SimpleStepBuilderDsl | ||
import com.pokit.alert.component.AlertProcessor | ||
import com.pokit.alert.component.AlertReader | ||
import com.pokit.alert.component.AlertWriter | ||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.model.AlertBatchValue | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.transaction.PlatformTransactionManager | ||
import org.springframework.transaction.TransactionDefinition | ||
import org.springframework.transaction.interceptor.DefaultTransactionAttribute | ||
|
||
|
||
@Configuration | ||
class SendAlertConfig( | ||
private val transactionManager: PlatformTransactionManager, | ||
private val batch: BatchDsl, | ||
private val alertReader: AlertReader, | ||
private val alertProcessor: AlertProcessor, | ||
private val alertWriter: AlertWriter | ||
) { | ||
@Bean(name = ["sendAlertJob"]) | ||
fun sendAlertJob() = batch { | ||
job("sendAlertJob") { | ||
step(sendAlertStep()) | ||
} | ||
} | ||
|
||
@Bean | ||
fun sendAlertStep() = batch { | ||
step("sendAlertStep") { | ||
chunk(AlertBatchValue.CHUNK_SIZE, transactionManager, fun SimpleStepBuilderDsl<AlertBatch, AlertBatch>.() { | ||
reader(alertReader) | ||
processor(alertProcessor) | ||
writer(alertWriter) | ||
transactionAttribute(DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NOT_SUPPORTED)) | ||
}) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
adapters/in-batch/src/main/kotlin/com/pokit/alert/scheduler/SendAlertScheduler.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,32 @@ | ||
package com.pokit.alert.scheduler | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.JobParametersBuilder | ||
import org.springframework.batch.core.launch.JobLauncher | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SendAlertScheduler( | ||
private val jobLauncher: JobLauncher, | ||
@Qualifier("sendAlertJob") private val sendAlertJob: Job | ||
) { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
companion object { | ||
private const val 매일_오전_8시 = "0 0 8 * * *" | ||
} | ||
|
||
@Scheduled(cron = 매일_오전_8시) | ||
fun sendAlert() { | ||
val jobParameters = JobParametersBuilder() | ||
.addLong("run.id", System.currentTimeMillis()) | ||
.toJobParameters() | ||
|
||
logger.info { "[ALERT BATCH] start daily send alert job" } | ||
jobLauncher.run(sendAlertJob, jobParameters) | ||
logger.info { "[ALERT BATCH] end daily send alert job" } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
adapters/in-batch/src/main/kotlin/com/pokit/config/SchedulerConfig.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,24 @@ | ||
package com.pokit.config | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.scheduling.TaskScheduler | ||
import org.springframework.scheduling.annotation.EnableScheduling | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler | ||
|
||
@Configuration | ||
@EnableScheduling | ||
class SchedulerConfig { | ||
companion object { | ||
private const val POOL_SIZE = 3 | ||
} | ||
|
||
@Bean("schedulerTask") | ||
fun taskScheduler(): TaskScheduler { | ||
val executor = ThreadPoolTaskScheduler() | ||
executor.poolSize = POOL_SIZE | ||
executor.threadNamePrefix = "scheduler-thread-" | ||
executor.initialize() | ||
return executor | ||
} | ||
} |
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
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
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
37 changes: 37 additions & 0 deletions
37
...out-persistence/src/main/kotlin/com/pokit/out/persistence/alert/impl/AlertBatchAdapter.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,37 @@ | ||
package com.pokit.out.persistence.alert.impl | ||
|
||
import com.pokit.alert.model.AlertBatch | ||
import com.pokit.alert.port.out.AlertBatchPort | ||
import com.pokit.out.persistence.alert.persist.AlertBatchEntity | ||
import com.pokit.out.persistence.alert.persist.AlertBatchRepository | ||
import com.pokit.out.persistence.alert.persist.toDomain | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Repository | ||
import java.time.LocalDate | ||
|
||
@Repository | ||
class AlertBatchAdapter( | ||
private val alertBatchRepository: AlertBatchRepository | ||
) : AlertBatchPort { | ||
override fun loadAllByShouldBeSentAt(now: LocalDate, pageable: Pageable): Page<AlertBatch> { | ||
return alertBatchRepository.findAllByShouldBeSentAtAfterAndSent(now, false, pageable) | ||
.map { it.toDomain() } | ||
} | ||
|
||
override fun send(alertBatch: AlertBatch) { | ||
alertBatchRepository.findByIdOrNull(alertBatch.id) | ||
?.sent() | ||
} | ||
Comment on lines
+18
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 알림 설정 (y) 로 하면 알림 등록하는 부분은.. 이전에 커밋했던가요 ..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰 이상 없으면 컨텐츠 생성, 수정작업에 해당 로직 추가하려했습니다! 지금은 없슴다ㅎ |
||
|
||
override fun loadByUserIdAndDate(userId: Long, date: LocalDate): AlertBatch? { | ||
return alertBatchRepository.findByUserIdAndShouldBeSentAtAndSent(userId, date, false) | ||
?.run { toDomain() } | ||
} | ||
|
||
override fun persist(alertBatch: AlertBatch): AlertBatch { | ||
val alertBatchEntity = AlertBatchEntity.of(alertBatch) | ||
return alertBatchRepository.save(alertBatchEntity).toDomain() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...t-persistence/src/main/kotlin/com/pokit/out/persistence/alert/impl/AlertContentAdapter.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,27 @@ | ||
package com.pokit.out.persistence.alert.impl | ||
|
||
import com.pokit.alert.model.AlertContent | ||
import com.pokit.alert.port.out.AlertContentPort | ||
import com.pokit.out.persistence.alert.persist.AlertContentEntity | ||
import com.pokit.out.persistence.alert.persist.AlertContentRepository | ||
import com.pokit.out.persistence.alert.persist.toDomain | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class AlertContentAdapter( | ||
private val alertContentRepository: AlertContentRepository | ||
) : AlertContentPort { | ||
override fun loadAllInAlertBatchIds(ids: List<Long>): List<AlertContent> { | ||
return alertContentRepository.findAllByAlertBatchIdIn(ids) | ||
.map { it.toDomain() } | ||
} | ||
|
||
override fun deleteAll(ids: List<Long>) { | ||
alertContentRepository.deleteAllByIdInBatch(ids) | ||
} | ||
|
||
override fun persist(alertContent: AlertContent): AlertContent { | ||
val alertContentEntity = AlertContentEntity.of(alertContent) | ||
return alertContentRepository.save(alertContentEntity).toDomain() | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...rsistence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertBatchRepository.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,12 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDate | ||
|
||
interface AlertBatchRepository : JpaRepository<AlertBatchEntity, Long> { | ||
fun findAllByShouldBeSentAtAfterAndSent(now: LocalDate, send: Boolean, pageable: Pageable): Page<AlertBatchEntity> | ||
|
||
fun findByUserIdAndShouldBeSentAtAndSent(userId: Long, date: LocalDate, sent: Boolean): AlertBatchEntity? | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...istence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertContentRepository.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,7 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface AlertContentRepository : JpaRepository<AlertContentEntity, Long> { | ||
fun findAllByAlertBatchIdIn(ids: List<Long>): List<AlertContentEntity> | ||
} |
5 changes: 5 additions & 0 deletions
5
...ersistence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertJdbcRepository.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,5 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
interface AlertJdbcRepository { | ||
fun bulkInsert(alertEntities: List<AlertEntity>) | ||
} |
31 changes: 31 additions & 0 deletions
31
...stence/src/main/kotlin/com/pokit/out/persistence/alert/persist/AlertJdbcRepositoryImpl.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,31 @@ | ||
package com.pokit.out.persistence.alert.persist | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class AlertJdbcRepositoryImpl( | ||
private val jdbcTemplate: JdbcTemplate | ||
) : AlertJdbcRepository { | ||
override fun bulkInsert(alertEntities: List<AlertEntity>) { | ||
val sql = """ | ||
INSERT INTO alert ( | ||
user_id, content_id, content_thumb_nail | ||
, title, is_deleted, created_at, updated_at | ||
) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) | ||
""".trimIndent() | ||
|
||
|
||
val batchArgs = alertEntities.map { alert -> | ||
arrayOf( | ||
alert.userId, | ||
alert.contentId, | ||
alert.contentThumbNail, | ||
alert.title, | ||
alert.deleted | ||
) | ||
} | ||
|
||
jdbcTemplate.batchUpdate(sql, batchArgs) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 pool size 왜 3으로 둔거예요 ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cpu 코어 1이니까 2로하려다가 3으로 했습니다! 근데 2로 수정하겠습니다