chore(ST-806): cleanup debug-pages, document realtime, restore strict mode
M8 acceptance heeft de end-to-end pipeline bevestigd: trigger → NOTIFY → SSE → store → View Transition. De cycling-symptomen waren een artefact van testen via terminal (alt-tab triggert visibility-pause-by-design), geen bug. Tijd om de tijdelijke instrumentatie en debug-pagina's weg te halen en de architectuur op te schrijven. - Verwijder /debug-realtime, /(app)/debug-realtime-app, /api/debug/* - Strip debug-logs uit /api/realtime/solo (closed-reden alleen in dev) - reactStrictMode weer aan - CONNECTING_INDICATOR_DELAY_MS 2s → 4s (minder flikker bij micro-disconnects) - Nieuwe sectie "Realtime updates (M8)" in scrum4me-architecture.md: diagram, NOTIFY-bron, server-filter, connection lifecycle inclusief visibility-pause + bekende beperking, animatie, auth - DIRECT_URL env-rij uitgebreid met realtime-doel - GET /api/realtime/solo gedocumenteerd in API.md Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7e4b6a20fa
commit
cefd54e56d
12 changed files with 125 additions and 899 deletions
|
|
@ -1,59 +0,0 @@
|
|||
// TIJDELIJKE debug-endpoint. Stuurt een handmatige pg_notify op
|
||||
// `scrum4me_changes` zonder een echte UPDATE te doen. Bedoeld om de
|
||||
// SSE-pipe te testen los van Prisma/triggers.
|
||||
//
|
||||
// VERWIJDEREN voor M8 out-of-draft.
|
||||
|
||||
import { Client } from 'pg'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
const CHANNEL = 'scrum4me_changes'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const directUrl = process.env.DIRECT_URL ?? process.env.DATABASE_URL
|
||||
if (!directUrl) {
|
||||
return Response.json({ error: 'DIRECT_URL/DATABASE_URL niet gezet' }, { status: 500 })
|
||||
}
|
||||
|
||||
let body: unknown = null
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
// empty body is OK — we vullen defaults in
|
||||
}
|
||||
|
||||
const overrides = (body && typeof body === 'object' ? body : {}) as Record<string, unknown>
|
||||
const payload = {
|
||||
op: 'U',
|
||||
entity: 'task',
|
||||
id: `debug-${Date.now()}`,
|
||||
story_id: 'debug-story',
|
||||
product_id: 'debug-product',
|
||||
sprint_id: null,
|
||||
assignee_id: null,
|
||||
task_status: 'TO_DO',
|
||||
task_sort_order: 1,
|
||||
task_title: 'manual debug emit',
|
||||
changed_fields: ['status'],
|
||||
debug: true,
|
||||
emitted_at: new Date().toISOString(),
|
||||
...overrides,
|
||||
}
|
||||
|
||||
const client = new Client({ connectionString: directUrl })
|
||||
try {
|
||||
await client.connect()
|
||||
// pg_notify met JSON-string als payload — zelfde formaat als de trigger
|
||||
await client.query('SELECT pg_notify($1, $2)', [CHANNEL, JSON.stringify(payload)])
|
||||
return Response.json({ ok: true, payload })
|
||||
} catch (err) {
|
||||
return Response.json(
|
||||
{ ok: false, error: err instanceof Error ? err.message : String(err) },
|
||||
{ status: 500 },
|
||||
)
|
||||
} finally {
|
||||
try { await client.end() } catch {}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
// TIJDELIJKE debug-endpoint voor M8-acceptance.
|
||||
// Geen auth, geen filtering — alle pg_notify-events op `scrum4me_changes`
|
||||
// stromen rauw door naar de browser. Bedoeld om te isoleren of de
|
||||
// SSE + LISTEN-pipe op Vercel werkt, los van iron-session, productfilter
|
||||
// of solo-store.
|
||||
//
|
||||
// VERWIJDEREN VOOR M8 OUT-OF-DRAFT.
|
||||
|
||||
import { NextRequest } from 'next/server'
|
||||
import { Client } from 'pg'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
export const maxDuration = 300
|
||||
|
||||
const CHANNEL = 'scrum4me_changes'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const directUrl = process.env.DIRECT_URL ?? process.env.DATABASE_URL
|
||||
if (!directUrl) {
|
||||
return Response.json({ error: 'DIRECT_URL/DATABASE_URL niet gezet' }, { status: 500 })
|
||||
}
|
||||
|
||||
const isPooled = directUrl.includes('pooler.')
|
||||
const hostHint = directUrl.match(/@([^/]+)/)?.[1] ?? 'unknown-host'
|
||||
console.log(`[debug-realtime] connecting (${isPooled ? 'POOLED' : 'direct'}) host=${hostHint}`)
|
||||
|
||||
const encoder = new TextEncoder()
|
||||
const pgClient = new Client({ connectionString: directUrl })
|
||||
let closed = false
|
||||
let heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const enqueue = (chunk: string) => {
|
||||
if (closed) return
|
||||
try {
|
||||
controller.enqueue(encoder.encode(chunk))
|
||||
} catch {
|
||||
// already closed
|
||||
}
|
||||
}
|
||||
|
||||
const cleanup = async (reason: string) => {
|
||||
if (closed) return
|
||||
closed = true
|
||||
if (heartbeatTimer) clearInterval(heartbeatTimer)
|
||||
try {
|
||||
await pgClient.end()
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
controller.close()
|
||||
} catch {
|
||||
// already closed
|
||||
}
|
||||
console.log(`[debug-realtime] closed: ${reason}`)
|
||||
}
|
||||
|
||||
try {
|
||||
await pgClient.connect()
|
||||
await pgClient.query(`LISTEN ${CHANNEL}`)
|
||||
console.log('[debug-realtime] LISTEN ready')
|
||||
} catch (err) {
|
||||
console.error('[debug-realtime] pg connect/listen failed:', err)
|
||||
enqueue(`event: error\ndata: ${JSON.stringify({ message: String(err) })}\n\n`)
|
||||
await cleanup('pg connect failed')
|
||||
return
|
||||
}
|
||||
|
||||
pgClient.on('notification', (msg) => {
|
||||
console.log(`[debug-realtime] RAW notification length=${msg.payload?.length ?? 0}`)
|
||||
if (!msg.payload) return
|
||||
enqueue(`data: ${msg.payload}\n\n`)
|
||||
})
|
||||
|
||||
pgClient.on('error', async (err) => {
|
||||
console.error('[debug-realtime] pg client error:', err)
|
||||
await cleanup('pg error')
|
||||
})
|
||||
|
||||
pgClient.on('end', () => {
|
||||
console.log('[debug-realtime] pg client end')
|
||||
})
|
||||
|
||||
enqueue(
|
||||
`event: ready\ndata: ${JSON.stringify({
|
||||
host: hostHint,
|
||||
pooled: isPooled,
|
||||
channel: CHANNEL,
|
||||
time: new Date().toISOString(),
|
||||
})}\n\n`,
|
||||
)
|
||||
|
||||
heartbeatTimer = setInterval(() => {
|
||||
enqueue(`: heartbeat ${new Date().toISOString()}\n\n`)
|
||||
}, 25_000)
|
||||
|
||||
request.signal.addEventListener('abort', () => {
|
||||
cleanup('client aborted')
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream; charset=utf-8',
|
||||
'Cache-Control': 'no-cache, no-transform',
|
||||
Connection: 'keep-alive',
|
||||
'X-Accel-Buffering': 'no',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -109,26 +109,18 @@ export async function GET(request: NextRequest) {
|
|||
} catch {
|
||||
// already closed
|
||||
}
|
||||
// Tijdelijk altijd loggen (geen NODE_ENV-guard) zodat we Vercel
|
||||
// function logs kunnen zien tijdens M8 acceptance. Strippen na
|
||||
// ST-806.
|
||||
console.log(`[realtime/solo] closed: ${reason}`)
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.log(`[realtime/solo] closed: ${reason}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve actieve sprint éénmalig per connectie
|
||||
const sprint = await prisma_sprint_findActive(productId)
|
||||
const activeSprintId = sprint?.id ?? null
|
||||
|
||||
const isPooled = directUrl.includes('pooler.')
|
||||
const hostHint = directUrl.match(/@([^/]+)/)?.[1] ?? 'unknown-host'
|
||||
console.log(
|
||||
`[realtime/solo] connecting (${isPooled ? 'POOLED — LISTEN may not work!' : 'direct'}) host=${hostHint} sprint=${activeSprintId}`,
|
||||
)
|
||||
|
||||
try {
|
||||
await pgClient.connect()
|
||||
await pgClient.query(`LISTEN ${CHANNEL}`)
|
||||
console.log(`[realtime/solo] LISTEN ${CHANNEL} ready`)
|
||||
} catch (err) {
|
||||
console.error('[realtime/solo] pg connect/listen failed:', err)
|
||||
enqueue(`event: error\ndata: ${JSON.stringify({ message: 'pg connect failed' })}\n\n`)
|
||||
|
|
@ -137,7 +129,6 @@ export async function GET(request: NextRequest) {
|
|||
}
|
||||
|
||||
pgClient.on('notification', (msg) => {
|
||||
console.log(`[realtime/solo] RAW notification on channel=${msg.channel} length=${msg.payload?.length ?? 0}`)
|
||||
if (!msg.payload) return
|
||||
let payload: NotifyPayload
|
||||
try {
|
||||
|
|
@ -145,11 +136,7 @@ export async function GET(request: NextRequest) {
|
|||
} catch {
|
||||
return
|
||||
}
|
||||
const emit = shouldEmit(payload, productId, activeSprintId, userId)
|
||||
console.log(
|
||||
`[realtime/solo] NOTIFY ${payload.entity}:${payload.id} ${payload.op} → ${emit ? 'EMIT' : 'skip'} (sprint=${payload.sprint_id} assignee=${payload.assignee_id} user=${userId})`,
|
||||
)
|
||||
if (!emit) return
|
||||
if (!shouldEmit(payload, productId, activeSprintId, userId)) return
|
||||
enqueue(`data: ${msg.payload}\n\n`)
|
||||
})
|
||||
|
||||
|
|
@ -158,10 +145,6 @@ export async function GET(request: NextRequest) {
|
|||
await cleanup('pg error')
|
||||
})
|
||||
|
||||
pgClient.on('end', () => {
|
||||
console.log('[realtime/solo] pg client end (connection closed)')
|
||||
})
|
||||
|
||||
// Stuur eerst een "ready"-event zodat de client weet dat de connectie staat
|
||||
enqueue(
|
||||
`event: ready\ndata: ${JSON.stringify({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue