Skip to content

Commit 56b21db

Browse files
authored
release: 1.4.7 (#291)
2 parents d5b3e92 + 0f967f3 commit 56b21db

10 files changed

+65
-35
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.gitanimals.core.redis
2+
3+
import com.fasterxml.jackson.core.type.TypeReference
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
6+
import org.slf4j.LoggerFactory
7+
import org.slf4j.MDC
8+
import org.springframework.data.redis.connection.Message
9+
import org.springframework.data.redis.connection.MessageListener
10+
import org.springframework.data.redis.core.StringRedisTemplate
11+
12+
abstract class TraceableMessageListener(
13+
private val redisTemplate: StringRedisTemplate,
14+
private val objectMapper: ObjectMapper,
15+
): MessageListener {
16+
17+
private val logger = LoggerFactory.getLogger(this::class.simpleName)
18+
19+
override fun onMessage(message: Message, pattern: ByteArray?) {
20+
runCatching {
21+
val traceId: String = objectMapper.readValue(
22+
redisTemplate.stringSerializer.deserialize(message.body),
23+
object: TypeReference<Map<String, String>>(){},
24+
)["traceId"] ?: throw IllegalArgumentException("Cannot find traceId on message: $message")
25+
MDC.put(TRACE_ID, traceId)
26+
onMessage(message)
27+
}.onFailure {
28+
logger.error("Fail to listen message: $message, error: $it", it)
29+
}.also {
30+
MDC.remove(TRACE_ID)
31+
}
32+
}
33+
34+
abstract fun onMessage(message: Message)
35+
}

โ€Žsrc/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.ktโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package org.gitanimals.core.redis
22

33
import com.fasterxml.jackson.annotation.JsonIgnore
4+
import org.gitanimals.core.IdGenerator
5+
import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
6+
import org.slf4j.MDC
47
import org.springframework.context.ApplicationEvent
58

69
abstract class TransactionCommitRedisPubSubEvent(
10+
val traceId: String = runCatching { MDC.get(TRACE_ID) }.getOrElse {
11+
IdGenerator.generate().toString()
12+
},
713
@JsonIgnore
814
val channel: String,
915
source: Any,
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.gitanimals.guild.infra
22

33
import org.gitanimals.core.redis.RedisPubSubChannel
4-
import org.slf4j.LoggerFactory
54
import org.springframework.context.annotation.Bean
65
import org.springframework.context.annotation.Configuration
76
import org.springframework.data.redis.connection.RedisConnectionFactory
@@ -14,8 +13,6 @@ class GuildRedisEventSubscriber(
1413
private val guildUpdateGuildContributionMessageListener: GuildUpdateGuildContributionMessageListener,
1514
) {
1615

17-
private val logger = LoggerFactory.getLogger(this::class.simpleName)
18-
1916
@Bean
2017
fun guildRedisListenerContainer(): RedisMessageListenerContainer {
2118
return RedisMessageListenerContainer().apply {
@@ -24,9 +21,6 @@ class GuildRedisEventSubscriber(
2421
guildUpdateGuildContributionMessageListener,
2522
ChannelTopic(RedisPubSubChannel.USER_CONTRIBUTION_UPDATED),
2623
)
27-
this.setErrorHandler {
28-
logger.error("Fail to listen message ${it.message}", it)
29-
}
3024
}
3125
}
3226
}

โ€Žsrc/main/kotlin/org/gitanimals/guild/infra/GuildUpdateGuildContributionMessageListener.ktโ€Ž

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.gitanimals.guild.infra
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.gitanimals.core.redis.TraceableMessageListener
45
import org.gitanimals.guild.domain.GuildService
56
import org.gitanimals.guild.infra.event.UserContributionUpdated
67
import org.slf4j.LoggerFactory
78
import org.springframework.beans.factory.annotation.Qualifier
89
import org.springframework.data.redis.connection.Message
9-
import org.springframework.data.redis.connection.MessageListener
1010
import org.springframework.data.redis.core.StringRedisTemplate
1111
import org.springframework.stereotype.Component
1212

@@ -15,18 +15,17 @@ class GuildUpdateGuildContributionMessageListener(
1515
private val guildService: GuildService,
1616
private val objectMapper: ObjectMapper,
1717
@Qualifier("gitanimalsRedisTemplate") private val redisTemplate: StringRedisTemplate,
18-
) : MessageListener {
18+
) : TraceableMessageListener(objectMapper = objectMapper, redisTemplate = redisTemplate) {
1919

2020
private val logger = LoggerFactory.getLogger(this::class.simpleName)
2121

22-
override fun onMessage(message: Message, pattern: ByteArray?) {
22+
override fun onMessage(message: Message) {
2323
runCatching {
24-
objectMapper.readValue(
24+
val userContributionUpdated = objectMapper.readValue(
2525
redisTemplate.stringSerializer.deserialize(message.body),
2626
UserContributionUpdated::class.java,
2727
)
28-
}.onSuccess {
29-
updateGuildContributions(it)
28+
updateGuildContributions(userContributionUpdated)
3029
}.onFailure {
3130
logger.error("Cannot update guild contributions message: $message", it)
3231
throw it

โ€Žsrc/main/kotlin/org/gitanimals/guild/infra/event/UserContributionUpdated.ktโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gitanimals.guild.infra.event
33
import java.time.Instant
44

55
data class UserContributionUpdated(
6+
val traceId: String,
67
val username: String,
78
val contributions: Long,
89
val userContributionUpdated: Boolean,

โ€Žsrc/main/kotlin/org/gitanimals/rank/infra/RankRedisEventSubscriber.ktโ€Ž

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.gitanimals.rank.infra
22

33
import org.gitanimals.core.redis.RedisPubSubChannel
4-
import org.slf4j.LoggerFactory
54
import org.springframework.context.annotation.Bean
65
import org.springframework.context.annotation.Configuration
76
import org.springframework.data.redis.connection.RedisConnectionFactory
@@ -15,8 +14,6 @@ class RankRedisEventSubscriber(
1514
private val updateUserContributionMessageListener: UpdateUserContributionMessageListener,
1615
) {
1716

18-
private val logger = LoggerFactory.getLogger(this::class.simpleName)
19-
2017
@Bean
2118
fun rankRedisListenerContainer(): RedisMessageListenerContainer {
2219
return RedisMessageListenerContainer().apply {
@@ -29,10 +26,6 @@ class RankRedisEventSubscriber(
2926
rankUpdateGuildContributionMessageListener,
3027
ChannelTopic(RedisPubSubChannel.GUILD_CONTRIBUTION_UPDATED),
3128
)
32-
this.setErrorHandler {
33-
it.printStackTrace() // FOR DEBUGGING
34-
logger.error("Fail to listen message ${it.message}", it)
35-
}
3629
}
3730
}
3831
}

โ€Žsrc/main/kotlin/org/gitanimals/rank/infra/RankUpdateGuildContributionMessageListener.ktโ€Ž

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.gitanimals.rank.infra
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.gitanimals.core.redis.TraceableMessageListener
45
import org.gitanimals.rank.domain.GuildContributionRank
56
import org.gitanimals.rank.domain.GuildContributionRankService
67
import org.gitanimals.rank.infra.event.GuildContributionUpdated
78
import org.slf4j.LoggerFactory
89
import org.springframework.beans.factory.annotation.Qualifier
910
import org.springframework.data.redis.connection.Message
10-
import org.springframework.data.redis.connection.MessageListener
1111
import org.springframework.data.redis.core.StringRedisTemplate
1212
import org.springframework.stereotype.Component
1313

@@ -16,22 +16,22 @@ class RankUpdateGuildContributionMessageListener(
1616
private val objectMapper: ObjectMapper,
1717
private val guildContributionService: GuildContributionRankService,
1818
@Qualifier("gitanimalsRedisTemplate") private val redisTemplate: StringRedisTemplate,
19-
) : MessageListener {
19+
) : TraceableMessageListener(objectMapper = objectMapper, redisTemplate = redisTemplate) {
2020

2121
private val logger = LoggerFactory.getLogger(this::class.simpleName)
2222

23-
override fun onMessage(message: Message, pattern: ByteArray?) {
23+
override fun onMessage(message: Message) {
2424
runCatching {
25-
objectMapper.readValue(
25+
val guildContributionUpdated = objectMapper.readValue(
2626
redisTemplate.stringSerializer.deserialize(message.body),
2727
GuildContributionUpdated::class.java,
2828
)
29-
}.onSuccess {
29+
3030
val updatedGuildContributionRank = GuildContributionRank.create(
31-
image = it.guildImage,
32-
guildName = it.guildTitle,
33-
guildId = it.guildId,
34-
totalContributions = it.contributions,
31+
image = guildContributionUpdated.guildImage,
32+
guildName = guildContributionUpdated.guildTitle,
33+
guildId = guildContributionUpdated.guildId,
34+
totalContributions = guildContributionUpdated.contributions,
3535
)
3636

3737
guildContributionService.updateContribution(updatedGuildContributionRank)

โ€Žsrc/main/kotlin/org/gitanimals/rank/infra/UpdateUserContributionMessageListener.ktโ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitanimals.rank.infra
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.gitanimals.core.redis.TraceableMessageListener
45
import org.gitanimals.rank.app.IdentityApi
56
import org.gitanimals.rank.app.RenderApi
67
import org.gitanimals.rank.domain.UserContributionRank
@@ -9,7 +10,6 @@ import org.gitanimals.rank.infra.event.UserContributionUpdated
910
import org.slf4j.LoggerFactory
1011
import org.springframework.beans.factory.annotation.Qualifier
1112
import org.springframework.data.redis.connection.Message
12-
import org.springframework.data.redis.connection.MessageListener
1313
import org.springframework.data.redis.core.StringRedisTemplate
1414
import org.springframework.stereotype.Component
1515

@@ -20,20 +20,20 @@ class UpdateUserContributionMessageListener(
2020
private val objectMapper: ObjectMapper,
2121
private val userContributionRankService: UserContributionRankService,
2222
@Qualifier("gitanimalsRedisTemplate") private val redisTemplate: StringRedisTemplate,
23-
) : MessageListener {
23+
) : TraceableMessageListener(objectMapper = objectMapper, redisTemplate = redisTemplate) {
2424

2525
private val logger = LoggerFactory.getLogger(this::class.simpleName)
2626

27-
override fun onMessage(message: Message, pattern: ByteArray?) {
27+
override fun onMessage(message: Message) {
2828
runCatching {
29-
objectMapper.readValue(
29+
val userContributionUpdated = objectMapper.readValue(
3030
redisTemplate.stringSerializer.deserialize(message.body),
3131
UserContributionUpdated::class.java,
3232
)
33-
}.onSuccess {
34-
val user = identityApi.getUserByName(it.username)
33+
34+
val user = identityApi.getUserByName(userContributionUpdated.username)
3535
val renderUserWithTopLevelPersona =
36-
renderApi.getUserWithTopLevelPersona(username = it.username)
36+
renderApi.getUserWithTopLevelPersona(username = userContributionUpdated.username)
3737

3838
val updatedUserContributionRank = UserContributionRank.create(
3939
image = user.profileImage,

โ€Žsrc/main/kotlin/org/gitanimals/rank/infra/event/GuildContributionUpdated.ktโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitanimals.rank.infra.event
22

33
data class GuildContributionUpdated(
4+
val traceId: String,
45
val guildId: Long,
56
val guildTitle: String,
67
val guildImage: String,

โ€Žsrc/main/kotlin/org/gitanimals/rank/infra/event/UserContributionUpdated.ktโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gitanimals.rank.infra.event
33
import java.time.Instant
44

55
data class UserContributionUpdated(
6+
val traceId: String,
67
val username: String,
78
val contributions: Long,
89
val userContributionUpdated: Boolean,

0 commit comments

Comments
ย (0)