@@ -20,11 +20,14 @@ import ai.tock.shared.TOCK_BOT_DATABASE
20
20
import ai.tock.shared.ensureIndex
21
21
import ai.tock.shared.error
22
22
import ai.tock.shared.injector
23
+ import ai.tock.shared.longProperty
23
24
import ai.tock.shared.watch
24
25
import com.github.salomonbrys.kodein.instance
25
26
import com.mongodb.client.MongoCollection
26
27
import com.mongodb.client.MongoDatabase
27
28
import com.mongodb.client.model.CreateCollectionOptions
29
+ import com.mongodb.client.model.IndexOptions
30
+ import java.util.concurrent.TimeUnit
28
31
import mu.KotlinLogging
29
32
import org.litote.kmongo.and
30
33
import org.litote.kmongo.eq
@@ -49,6 +52,10 @@ internal object ChannelMongoDAO : ChannelDAO {
49
52
private fun MongoDatabase.collectionExists (collectionName : String ): Boolean =
50
53
listCollectionNames().contains(collectionName)
51
54
55
+ private val messageQueueTtl = longProperty(" tock_web_sse_message_queue_ttl_days" , - 1 )
56
+ private val messageQueueMaxCount = longProperty(" tock_web_sse_message_queue_max_count" , 50000 )
57
+ private val messageQueueMaxSize = longProperty(" tock_web_sse_message_queue_max_size_kb" , 2 * messageQueueMaxCount)
58
+
52
59
init {
53
60
if (! database.collectionExists(COLLECTION_NAME )) {
54
61
try {
@@ -57,8 +64,8 @@ internal object ChannelMongoDAO : ChannelDAO {
57
64
COLLECTION_NAME ,
58
65
CreateCollectionOptions ()
59
66
.capped(true )
60
- .sizeInBytes(100000000 )
61
- .maxDocuments(50000 )
67
+ .sizeInBytes(messageQueueMaxSize * 1000 )
68
+ .maxDocuments(messageQueueMaxCount )
62
69
)
63
70
} catch (e: Exception ) {
64
71
logger.error(e)
@@ -68,7 +75,14 @@ internal object ChannelMongoDAO : ChannelDAO {
68
75
webChannelResponseCol = database.getCollection<ChannelEvent >(COLLECTION_NAME )
69
76
try {
70
77
webChannelResponseCol.ensureIndex(ChannelEvent ::appId, ChannelEvent ::recipientId, ChannelEvent ::status)
71
- // TODO add an index with TTL on ChannelEvent::enqueuedAt once MongoDB supports it (cf. https://jira.mongodb.org/browse/SERVER-77586)
78
+ if (messageQueueTtl > 0 ) {
79
+ webChannelResponseCol.ensureIndex(
80
+ ChannelEvent ::enqueuedAt, indexOptions = IndexOptions ().expireAfter(
81
+ messageQueueTtl,
82
+ TimeUnit .DAYS
83
+ )
84
+ )
85
+ }
72
86
} catch (e: Exception ) {
73
87
logger.error(e)
74
88
}
@@ -101,11 +115,19 @@ internal object ChannelMongoDAO : ChannelDAO {
101
115
102
116
private fun process (event : ChannelEvent , handler : ChannelEvent .Handler ) {
103
117
try {
104
- if (handler(event)) {
105
- webChannelResponseCol.updateOneById(event._id , ChannelEvent ::status setTo ChannelEvent .Status .PROCESSED )
106
- }
118
+ handler(event).onComplete({ processed ->
119
+ if (processed) {
120
+ webChannelResponseCol.updateOneById(event._id , ChannelEvent ::status setTo ChannelEvent .Status .PROCESSED )
121
+ }
122
+ }, { e ->
123
+ logger.error(e) {
124
+ " Failed to send SSE message"
125
+ }
126
+ })
107
127
} catch (e: Exception ) {
108
- logger.error(e)
128
+ logger.error(e) {
129
+ " Failed to send SSE message"
130
+ }
109
131
}
110
132
}
111
133
0 commit comments