feat(J): WebSocket live-events + replace polling + fix ffmpeg turntable timeout

- fix(render): ffmpeg overlay=0:0 -> overlay=0:0:shortest=1 to prevent hang on finite PNG sequences
- feat(ws): add core/websocket.py ConnectionManager + Redis Pub/Sub subscriber loop
- feat(ws): add /api/ws WebSocket endpoint with JWT query-param auth in main.py
- feat(ws): emit render_complete/failed + cad_processing_complete events from step_tasks.py
- feat(ws): emit order_status_change events from orders router
- feat(ws): add beat_tasks.py broadcast_queue_status task (every 10s via Redis __broadcast__)
- feat(frontend): add useWebSocket hook with auto-reconnect (exponential backoff, 25s ping)
- feat(frontend): add WebSocketContext + WebSocketProvider wrapping App
- refactor(frontend): remove polling from WorkerActivity (was 5s/3s) + OrderDetail (was 5s)
- refactor(frontend): reduce polling in Layout (8s->60s) + NotificationCenter (15s->60s)
- docs: add ffmpeg shortest=1 + WebSocket JWT auth learnings to LEARNINGS.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 20:49:34 +01:00
parent ceb0143cb6
commit 7a1329958d
16 changed files with 909 additions and 16 deletions
@@ -0,0 +1,62 @@
/**
* WebSocketContext — global WebSocket provider.
*
* Wraps the app with a single WebSocket connection. On incoming events it
* invalidates the relevant React Query caches so all subscribers refresh.
*/
import { createContext, useContext, useCallback } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import { useWebSocket, type WSEvent } from '../hooks/useWebSocket'
const WebSocketContext = createContext<null>(null)
export function WebSocketProvider({ children }: { children: React.ReactNode }) {
const qc = useQueryClient()
const onEvent = useCallback(
(event: WSEvent) => {
switch (event.type) {
case 'render_complete':
case 'render_failed':
qc.invalidateQueries({ queryKey: ['orders'] })
if (event.order_id) {
qc.invalidateQueries({ queryKey: ['order', event.order_id as string] })
}
qc.invalidateQueries({ queryKey: ['worker-activity'] })
break
case 'cad_processing_complete':
qc.invalidateQueries({ queryKey: ['worker-activity'] })
break
case 'order_status_change':
qc.invalidateQueries({ queryKey: ['orders'] })
if (event.order_id) {
qc.invalidateQueries({ queryKey: ['order', event.order_id as string] })
}
break
case 'queue_update':
qc.setQueryData(['queue-status'], event.depths)
break
default:
break
}
},
[qc],
)
useWebSocket({ onEvent })
return (
<WebSocketContext.Provider value={null}>
{children}
</WebSocketContext.Provider>
)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function useWebSocketContext() {
return useContext(WebSocketContext)
}