diff --git a/src/main/kotlin/com/hero/alignlab/batch/posecount/job/PoseCountUpdateJob.kt b/src/main/kotlin/com/hero/alignlab/batch/posecount/job/PoseCountUpdateJob.kt index b386b66..5033067 100644 --- a/src/main/kotlin/com/hero/alignlab/batch/posecount/job/PoseCountUpdateJob.kt +++ b/src/main/kotlin/com/hero/alignlab/batch/posecount/job/PoseCountUpdateJob.kt @@ -1,7 +1,5 @@ package com.hero.alignlab.batch.posecount.job -import com.hero.alignlab.common.extension.executesOrNull -import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.pose.application.PoseCountService import com.hero.alignlab.domain.pose.application.PoseSnapshotService import com.hero.alignlab.domain.pose.domain.vo.PoseTotalCount @@ -15,7 +13,6 @@ class PoseCountUpdateJob( private val poseCountService: PoseCountService, private val poseSnapshotService: PoseSnapshotService, private val userInfoService: UserInfoService, - private val txTemplates: TransactionTemplates, ) { private val logger = KotlinLogging.logger { } @@ -50,9 +47,7 @@ class PoseCountUpdateJob( } } - txTemplates.writer.executesOrNull { - poseCountService.saveAllSync(poseCounts) - } + poseCountService.saveAll(poseCounts) } logger.info { "finished PoseCountUpdateJob.run()" } diff --git a/src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt b/src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt index 9a8dd2c..4f8eb60 100644 --- a/src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt +++ b/src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt @@ -1,32 +1,32 @@ package com.hero.alignlab.batch.user.job -import com.hero.alignlab.domain.log.infrastructure.SystemActionLogRepository -import com.hero.alignlab.domain.user.infrastructure.UserInfoRepository +import com.hero.alignlab.domain.log.application.SystemActionLogService +import com.hero.alignlab.domain.user.application.UserInfoService import org.springframework.stereotype.Component import java.time.LocalDateTime @Component class UserLevelJob( - private val systemActionLogRepository: SystemActionLogRepository, - private val userInfoRepository: UserInfoRepository, + private val systemActionLogService: SystemActionLogService, + private val userInfoService: UserInfoService, ) { - fun run() { + suspend fun run() { val toDate = LocalDateTime.now() - val fromDate = LocalDateTime.now().minusDays(1) + val fromDate = toDate.minusDays(1) - val activeUsers = systemActionLogRepository.countActiveUser(fromDate, toDate, 1000) - .filter { it.count > 100 } - .map { it.uid } + val activeUsers = systemActionLogService.countActiveUser(fromDate, toDate, 1000) + .filter { user -> user.count > 100 } + .map { user -> user.uid } - val users = userInfoRepository.findAllById(activeUsers) - .mapNotNull { - if (it.maxLevel) { + val users = userInfoService.findAllById(activeUsers) + .mapNotNull { user -> + if (user.maxLevel) { return@mapNotNull null } - it.apply { this.level += 1 } + user.apply { this.level += 1 } } - userInfoRepository.saveAll(users) + userInfoService.saveAll(users) } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/group/application/GroupService.kt b/src/main/kotlin/com/hero/alignlab/domain/group/application/GroupService.kt index 48c95de..f5f1524 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/group/application/GroupService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/group/application/GroupService.kt @@ -77,4 +77,10 @@ class GroupService( groupRepository.findAll(pageable) } } + + suspend fun findByOwnerUid(uid: Long): Group? { + return withContext(Dispatchers.IO) { + groupRepository.findByOwnerUid(uid) + } + } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt b/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt index b39ab09..bdb4df1 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/log/application/SystemActionLogService.kt @@ -4,8 +4,12 @@ import com.hero.alignlab.common.extension.coExecute import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.log.domain.SystemActionLog import com.hero.alignlab.domain.log.infrastructure.SystemActionLogRepository +import com.hero.alignlab.domain.log.infrastructure.model.CountActiveUser +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional +import java.time.LocalDateTime @Service class SystemActionLogService( @@ -18,4 +22,14 @@ class SystemActionLogService( systemActionLogRepository.save(systemActionLog) } } + + suspend fun countActiveUser( + fromCreatedAt: LocalDateTime, + toCreatedAt: LocalDateTime, + limit: Long = 3L + ): List { + return withContext(Dispatchers.IO) { + systemActionLogRepository.countActiveUser(fromCreatedAt, toCreatedAt, limit) + } + } } diff --git a/src/main/kotlin/com/hero/alignlab/domain/pose/application/PoseCountService.kt b/src/main/kotlin/com/hero/alignlab/domain/pose/application/PoseCountService.kt index 995b7ec..c293602 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/pose/application/PoseCountService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/pose/application/PoseCountService.kt @@ -1,5 +1,6 @@ package com.hero.alignlab.domain.pose.application +import com.hero.alignlab.common.extension.coExecute import com.hero.alignlab.common.extension.coExecuteOrNull import com.hero.alignlab.common.model.HeroPageRequest import com.hero.alignlab.config.database.TransactionTemplates @@ -32,6 +33,12 @@ class PoseCountService( return poseCountRepository.saveAll(poseCounts) } + suspend fun saveAll(poseCounts: List): List { + return txTemplates.writer.coExecute { + saveAllSync(poseCounts) + } + } + suspend fun findByUidAndDateOrNull(uid: Long, date: LocalDate): PoseCount? { return withContext(Dispatchers.IO) { poseCountRepository.findByUidAndDate(uid, date) diff --git a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt index d8bb49d..c0e1a2a 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/user/application/UserInfoService.kt @@ -129,4 +129,16 @@ class UserInfoService( userInfoRepository.existsByNicknameAndIdNot(nickname, id) } } + + suspend fun findAllById(ids: List): List { + return withContext(Dispatchers.IO) { + userInfoRepository.findAllById(ids) + } + } + + suspend fun saveAll(users: List): List { + return txTemplates.writer.coExecute { + userInfoRepository.saveAll(users) + } + } } diff --git a/src/main/kotlin/com/hero/alignlab/event/listener/GroupEventListener.kt b/src/main/kotlin/com/hero/alignlab/event/listener/GroupEventListener.kt index ecd0b89..97b12e9 100644 --- a/src/main/kotlin/com/hero/alignlab/event/listener/GroupEventListener.kt +++ b/src/main/kotlin/com/hero/alignlab/event/listener/GroupEventListener.kt @@ -1,9 +1,6 @@ package com.hero.alignlab.event.listener -import com.hero.alignlab.common.extension.executes -import com.hero.alignlab.config.database.TransactionTemplates -import com.hero.alignlab.domain.group.domain.GroupUser -import com.hero.alignlab.domain.group.infrastructure.GroupUserRepository +import com.hero.alignlab.domain.group.application.GroupUserService import com.hero.alignlab.event.model.CreateGroupEvent import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Component @@ -11,18 +8,12 @@ import org.springframework.transaction.event.TransactionalEventListener @Component class GroupEventListener( - private val groupUserRepository: GroupUserRepository, - private val txTemplates: TransactionTemplates, + private val groupUserService: GroupUserService, ) { private val logger = KotlinLogging.logger { } @TransactionalEventListener fun handle(event: CreateGroupEvent) { - txTemplates.newTxWriter.executes { - GroupUser( - groupId = event.group.id, - uid = event.group.ownerUid - ).run { groupUserRepository.save(this) } - } + groupUserService.saveSync(event.group.id, event.group.ownerUid) } } diff --git a/src/main/kotlin/com/hero/alignlab/event/listener/WithdrawEventListener.kt b/src/main/kotlin/com/hero/alignlab/event/listener/WithdrawEventListener.kt index 74dad2d..b083a9e 100644 --- a/src/main/kotlin/com/hero/alignlab/event/listener/WithdrawEventListener.kt +++ b/src/main/kotlin/com/hero/alignlab/event/listener/WithdrawEventListener.kt @@ -2,7 +2,7 @@ package com.hero.alignlab.event.listener import com.hero.alignlab.domain.dev.application.DevDeleteService import com.hero.alignlab.domain.group.application.GroupFacade -import com.hero.alignlab.domain.group.infrastructure.GroupRepository +import com.hero.alignlab.domain.group.application.GroupService import com.hero.alignlab.event.model.WithdrawEvent import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -14,7 +14,7 @@ import org.springframework.transaction.event.TransactionalEventListener class WithdrawEventListener( /** 탈퇴 회원이 그룹장인 경우 승계 작업 필요. */ private val groupFacade: GroupFacade, - private val groupRepository: GroupRepository, + private val groupService: GroupService, private val devDeleteService: DevDeleteService, ) { /** @@ -24,7 +24,7 @@ class WithdrawEventListener( fun handle(event: WithdrawEvent) { /** 그룹 승계 및 탈퇴 */ CoroutineScope(Dispatchers.IO).launch { - val group = groupRepository.findByOwnerUid(event.uid) + val group = groupService.findByOwnerUid(event.uid) if (group != null) { groupFacade.withdraw(group.id, event.uid)