Skip to content

Commit e310762

Browse files
committed
perf: stuck streams resolve perf
1 parent 316646d commit e310762

3 files changed

Lines changed: 5 additions & 3 deletions

File tree

apps/backend-convex/convex/crons.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ crons.daily(
2323

2424
crons.interval(
2525
'resolve stuck stream messages',
26-
{ hours: 1 },
26+
{ minutes: 10 },
2727
internal.messages.resolveStuckStreamMessages,
2828
)
2929

apps/backend-convex/convex/messages.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ export const resolveStuckStreamMessages = internalMutation({
138138
handler: async (ctx) => {
139139
const messages = await ctx.db
140140
.query('messages')
141-
.filter(q => q.eq(q.field('isStreaming'), true))
142141
// Get streams that are older than 15 minutes
143-
.filter(q => q.lt(q.field('timestamp'), Date.now() - 15 * 60 * 1000))
142+
.withIndex('by_timestamp', q =>
143+
q.lte('timestamp', Date.now() - 15 * 60 * 1000))
144+
.filter(q => q.eq(q.field('isStreaming'), true))
144145
.collect()
145146

146147
if (!messages.length)

apps/backend-convex/convex/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const aiChatTables = {
2929
})
3030
.index('by_thread', ['threadId'])
3131
.index('by_thread_and_timestamp', ['threadId', 'timestamp'])
32+
.index('by_timestamp', ['timestamp'])
3233
.index('by_stream_id', ['streamId']),
3334
}
3435

0 commit comments

Comments
 (0)