mirror of https://github.com/usbharu/Hideout.git
feat: トランザクションの問題で正常にドメインイベントを受け取ったあとの処理を実行できなかった問題を修正
This commit is contained in:
parent
8d244b74c1
commit
e9d776f71a
|
@ -2,16 +2,12 @@ package dev.usbharu.hideout.core.domain.shared.repository
|
|||
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface DomainEventPublishableRepository<T : DomainEventStorable> {
|
||||
val domainEventPublisher: DomainEventPublisher
|
||||
suspend fun update(entity: T) {
|
||||
println(entity.getDomainEvents().joinToString())
|
||||
val current = TransactionManager.current()
|
||||
current.registerInterceptor()
|
||||
entity.getDomainEvents().distinctBy {
|
||||
if (it.collectable) {
|
||||
it.name
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
package dev.usbharu.hideout.core.infrastructure.exposedrepository
|
||||
|
||||
import dev.usbharu.hideout.core.domain.exception.SpringDataAccessExceptionSQLExceptionTranslator
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.slf4j.MDCContext
|
||||
import org.jetbrains.exposed.sql.Transaction
|
||||
import org.jetbrains.exposed.sql.statements.StatementInterceptor
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.slf4j.Logger
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
|
@ -35,7 +39,20 @@ abstract class AbstractRepository {
|
|||
@Value("\${hideout.debug.trace-query-call:false}")
|
||||
private var traceQueryCall: Boolean = false
|
||||
|
||||
protected suspend fun <T> query(block: () -> T): T = try {
|
||||
class TransactionInterceptor(private val transaction: Transaction) {
|
||||
|
||||
fun onComplete(block: suspend (transaction: Transaction) -> Unit) {
|
||||
transaction.registerInterceptor(object : StatementInterceptor {
|
||||
override fun afterCommit(transaction: Transaction) {
|
||||
runBlocking(MDCContext()) {
|
||||
block(transaction)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
protected suspend fun <T> query(block: TransactionInterceptor.() -> T): T = try {
|
||||
if (traceQueryCall) {
|
||||
@Suppress("ThrowingExceptionsWithoutMessageOrCause")
|
||||
logger.trace(
|
||||
|
@ -49,7 +66,7 @@ ${Throwable().stackTrace.joinToString("\n")}
|
|||
)
|
||||
}
|
||||
|
||||
block.invoke()
|
||||
block.invoke(TransactionInterceptor(TransactionManager.current()))
|
||||
} catch (e: SQLException) {
|
||||
if (traceQueryException) {
|
||||
logger.trace("FAILED EXECUTE SQL", e)
|
||||
|
|
|
@ -45,8 +45,11 @@ class ExposedActorInstanceRelationshipRepository(override val domainEventPublish
|
|||
it[muting] = actorInstanceRelationship.muting
|
||||
it[doNotSendPrivate] = actorInstanceRelationship.doNotSendPrivate
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(actorInstanceRelationship)
|
||||
}
|
||||
}
|
||||
|
||||
return actorInstanceRelationship
|
||||
}
|
||||
|
||||
|
@ -56,9 +59,12 @@ class ExposedActorInstanceRelationshipRepository(override val domainEventPublish
|
|||
actorId eq actorInstanceRelationship.actorId.id and
|
||||
(instanceId eq actorInstanceRelationship.instanceId.instanceId)
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(actorInstanceRelationship)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findByActorIdAndInstanceId(
|
||||
actorId: ActorId,
|
||||
|
|
|
@ -59,8 +59,11 @@ class ExposedActorRepository(
|
|||
this[ActorsAlsoKnownAs.actorId] = actor.id.id
|
||||
this[ActorsAlsoKnownAs.alsoKnownAs] = it.id
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(actor)
|
||||
}
|
||||
}
|
||||
|
||||
return actor
|
||||
}
|
||||
|
||||
|
@ -68,9 +71,11 @@ class ExposedActorRepository(
|
|||
query {
|
||||
Actors.deleteWhere { id eq actor.id.id }
|
||||
ActorsAlsoKnownAs.deleteWhere { actorId eq actor.id.id }
|
||||
}
|
||||
onComplete {
|
||||
update(actor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun findById(id: ActorId): Actor? {
|
||||
return query {
|
||||
|
|
|
@ -98,8 +98,11 @@ class ExposedPostRepository(
|
|||
this[PostsVisibleActors.postId] = post.id.id
|
||||
this[PostsVisibleActors.actorId] = it.id
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(post)
|
||||
}
|
||||
}
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
|
@ -148,10 +151,12 @@ class ExposedPostRepository(
|
|||
this[PostsVisibleActors.postId] = it.first
|
||||
this[PostsVisibleActors.actorId] = it.second
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
posts.forEach {
|
||||
update(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
return posts
|
||||
}
|
||||
|
||||
|
@ -195,9 +200,12 @@ class ExposedPostRepository(
|
|||
Posts.deleteWhere {
|
||||
id eq post.id.id
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(post)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findByActorIdAndVisibilityInList(
|
||||
actorId: ActorId,
|
||||
|
|
|
@ -47,8 +47,11 @@ class ExposedRelationshipRepository(override val domainEventPublisher: DomainEve
|
|||
it[followRequesting] = relationship.followRequesting
|
||||
it[mutingFollowRequest] = relationship.mutingFollowRequest
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(relationship)
|
||||
}
|
||||
}
|
||||
|
||||
return relationship
|
||||
}
|
||||
|
||||
|
@ -57,9 +60,12 @@ class ExposedRelationshipRepository(override val domainEventPublisher: DomainEve
|
|||
Relationships.deleteWhere {
|
||||
actorId eq relationship.actorId.id and (targetActorId eq relationship.targetActorId.id)
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(relationship)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findByActorIdAndTargetId(actorId: ActorId, targetId: ActorId): Relationship? = query {
|
||||
Relationships.selectAll().where {
|
||||
|
|
|
@ -24,8 +24,12 @@ class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPu
|
|||
it[visibility] = timeline.visibility.name
|
||||
it[isSystem] = timeline.isSystem
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(timeline)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return timeline
|
||||
}
|
||||
|
||||
|
@ -34,9 +38,12 @@ class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPu
|
|||
Timelines.deleteWhere {
|
||||
Timelines.id eq timeline.id.value
|
||||
}
|
||||
}
|
||||
onComplete {
|
||||
update(timeline)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findByIds(ids: List<TimelineId>): List<Timeline> {
|
||||
return query {
|
||||
|
|
|
@ -48,6 +48,7 @@ class UserDetailRepositoryImpl(override val domainEventPublisher: DomainEventPub
|
|||
it[password] = userDetail.password.password
|
||||
it[autoAcceptFolloweeFollowRequest] = userDetail.autoAcceptFolloweeFollowRequest
|
||||
it[lastMigration] = userDetail.lastMigration
|
||||
it[homeTimelineId] = userDetail.homeTimelineId?.value
|
||||
}
|
||||
} else {
|
||||
UserDetails.update({ UserDetails.id eq userDetail.id.id }) {
|
||||
|
@ -55,21 +56,27 @@ class UserDetailRepositoryImpl(override val domainEventPublisher: DomainEventPub
|
|||
it[password] = userDetail.password.password
|
||||
it[autoAcceptFolloweeFollowRequest] = userDetail.autoAcceptFolloweeFollowRequest
|
||||
it[lastMigration] = userDetail.lastMigration
|
||||
it[homeTimelineId] = userDetail.homeTimelineId?.value
|
||||
}
|
||||
}
|
||||
|
||||
onComplete {
|
||||
update(userDetail)
|
||||
}
|
||||
userDetail
|
||||
}
|
||||
update(userDetail)
|
||||
|
||||
return userDetail1
|
||||
}
|
||||
|
||||
override suspend fun delete(userDetail: UserDetail): Unit {
|
||||
query {
|
||||
UserDetails.deleteWhere { id eq userDetail.id.id }
|
||||
}
|
||||
onComplete {
|
||||
update(userDetail)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun findByActorId(actorId: Long): UserDetail? = query {
|
||||
return@query UserDetails
|
||||
|
|
Loading…
Reference in New Issue