Skip to content

Commit

Permalink
refac: tx 및 layer 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Oct 5, 2024
1 parent f2f36e6 commit 7baf546
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 { }

Expand Down Expand Up @@ -50,9 +47,7 @@ class PoseCountUpdateJob(
}
}

txTemplates.writer.executesOrNull {
poseCountService.saveAllSync(poseCounts)
}
poseCountService.saveAll(poseCounts)
}

logger.info { "finished PoseCountUpdateJob.run()" }
Expand Down
28 changes: 14 additions & 14 deletions src/main/kotlin/com/hero/alignlab/batch/user/job/UserLevelJob.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ class GroupService(
groupRepository.findAll(pageable)
}
}

suspend fun findByOwnerUid(uid: Long): Group? {
return withContext(Dispatchers.IO) {
groupRepository.findByOwnerUid(uid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -18,4 +22,14 @@ class SystemActionLogService(
systemActionLogRepository.save(systemActionLog)
}
}

suspend fun countActiveUser(
fromCreatedAt: LocalDateTime,
toCreatedAt: LocalDateTime,
limit: Long = 3L
): List<CountActiveUser> {
return withContext(Dispatchers.IO) {
systemActionLogRepository.countActiveUser(fromCreatedAt, toCreatedAt, limit)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,6 +33,12 @@ class PoseCountService(
return poseCountRepository.saveAll(poseCounts)
}

suspend fun saveAll(poseCounts: List<PoseCount>): List<PoseCount> {
return txTemplates.writer.coExecute {
saveAllSync(poseCounts)
}
}

suspend fun findByUidAndDateOrNull(uid: Long, date: LocalDate): PoseCount? {
return withContext(Dispatchers.IO) {
poseCountRepository.findByUidAndDate(uid, date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,16 @@ class UserInfoService(
userInfoRepository.existsByNicknameAndIdNot(nickname, id)
}
}

suspend fun findAllById(ids: List<Long>): List<UserInfo> {
return withContext(Dispatchers.IO) {
userInfoRepository.findAllById(ids)
}
}

suspend fun saveAll(users: List<UserInfo>): List<UserInfo> {
return txTemplates.writer.coExecute {
userInfoRepository.saveAll(users)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
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
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
) {
/**
Expand All @@ -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)
Expand Down

0 comments on commit 7baf546

Please sign in to comment.