fix(convex): corrigir memory leak com .collect() sem limite e adicionar otimizacoes
Problema: Convex backend consumindo 16GB+ de RAM causando OOM kills Correcoes aplicadas: - Substituido todos os .collect() por .take(LIMIT) em 27+ arquivos - Adicionado indice by_usbPolicyStatus para otimizar query de maquinas - Corrigido N+1 problem em alerts.ts usando Map lookup - Corrigido full table scan em usbPolicy.ts - Corrigido subscription leaks no frontend (tickets-view, use-ticket-categories) - Atualizado versao do Convex backend para precompiled-2025-12-04-cc6af4c Arquivos principais modificados: - convex/*.ts - limites em todas as queries .collect() - convex/schema.ts - novo indice by_usbPolicyStatus - convex/alerts.ts - N+1 fix com Map - convex/usbPolicy.ts - uso do novo indice - src/components/tickets/tickets-view.tsx - skip condicional - src/hooks/use-ticket-categories.ts - skip condicional 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a4b46b08ba
commit
638faeb287
33 changed files with 139 additions and 128 deletions
|
|
@ -196,12 +196,13 @@ export const listUsbPolicyEvents = query({
|
|||
},
|
||||
handler: async (ctx, args) => {
|
||||
const limit = args.limit ?? 10
|
||||
const maxFetch = 1000 // Limite maximo de eventos a buscar
|
||||
|
||||
let events = await ctx.db
|
||||
.query("usbPolicyEvents")
|
||||
.withIndex("by_machine_created", (q) => q.eq("machineId", args.machineId))
|
||||
.order("desc")
|
||||
.collect()
|
||||
.take(maxFetch)
|
||||
|
||||
// Aplica filtro de cursor (paginacao)
|
||||
if (args.cursor !== undefined) {
|
||||
|
|
@ -313,13 +314,11 @@ export const cleanupStalePendingPolicies = mutation({
|
|||
const cutoff = now - thresholdMs
|
||||
|
||||
// Buscar maquinas com status PENDING e appliedAt antigo
|
||||
const allMachines = await ctx.db.query("machines").collect()
|
||||
const staleMachines = allMachines.filter(
|
||||
(m) =>
|
||||
m.usbPolicyStatus === "PENDING" &&
|
||||
m.usbPolicyAppliedAt !== undefined &&
|
||||
m.usbPolicyAppliedAt < cutoff
|
||||
)
|
||||
const staleMachines = await ctx.db
|
||||
.query("machines")
|
||||
.withIndex("by_usbPolicyStatus", (q) => q.eq("usbPolicyStatus", "PENDING"))
|
||||
.filter((q) => q.lt(q.field("usbPolicyAppliedAt"), cutoff))
|
||||
.take(1000)
|
||||
|
||||
let cleaned = 0
|
||||
for (const machine of staleMachines) {
|
||||
|
|
@ -346,6 +345,6 @@ export const cleanupStalePendingPolicies = mutation({
|
|||
cleaned++
|
||||
}
|
||||
|
||||
return { cleaned, checked: allMachines.length }
|
||||
return { cleaned, checked: staleMachines.length }
|
||||
},
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue