Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | /** * Live deployment progress + log stream via Server-Sent Events. * * The browser's native ``EventSource`` would be the obvious fit but it * cannot attach an ``Authorization`` header — and we authenticate with * Keycloak Bearer tokens, not cookies. So we use ``fetch`` + a * ``ReadableStream`` reader instead, which lets us carry the same * Bearer token the rest of the API uses and gives us explicit control * over reconnect behaviour. * * Returns reactive state: * * * ``progress`` — 0..100, ``null`` while no progress event has * arrived (the snapshot frame seeds it from the DB). * * ``currentPhase`` — name of the active phase (e.g. * ``TERRAFORM_APPLY``), or null pre-snapshot. * * ``totalPhases`` — last seen total (8 or 11 depending on whether * the deployment includes a Packer build). * * ``liveLogs`` — bounded array of the most recent log entries * (default 100). Auto-scroll consumers should render the tail. * * ``connectionState`` — high-level state machine surface for the * detail view to show "connecting" / "live" / "reconnecting" / * "ended" badges. */ import { ref, type Ref } from 'vue' import { useKeycloak } from '@/composables/useKeycloak' import { env } from '@/env' export interface LogEntry { timestamp: string level: string category?: string message: string tool?: string streaming?: boolean phase?: string progress_pct?: number [key: string]: any } export interface ProgressEvent { phase: string phase_index: number total_phases: number progress_pct: number message?: string // Full ordered phase-name sequence for the active task. Multi-image // deploys produce a dynamic sequence (N×3 Packer phases for N // templates) whose template keys cannot be guessed from // observation order — the worker ships the authoritative list with // every event. Single-image / non-deploy tasks omit this and the // UI falls back to the static phase tables it bundles for the // legacy shape. phase_names?: string[] } export interface SnapshotEvent { task_id: string | null status: string | null current_phase: string | null progress_pct: number | null type: string | null } export type ConnectionState = 'idle' | 'connecting' | 'live' | 'reconnecting' | 'ended' | 'error' const MAX_LOG_BUFFER = 100 const RECONNECT_BASE_MS = 1000 const RECONNECT_MAX_MS = 30000 export function useDeploymentStream(deploymentId: Ref<string | null>) { const progress = ref<number | null>(null) const currentPhase = ref<string | null>(null) // 1-based phase index from the worker's progress event. Lets the // detail view highlight the right dot in the stepper without // reconstructing it from ``progress_pct`` (which is rounded and // collides on consecutive phases when total is small). const currentPhaseIndex = ref<number | null>(null) const totalPhases = ref<number>(11) // Authoritative phase-name list from the worker. ``[]`` means no // event with ``phase_names`` has been received yet (or the task // doesn't ship it, like the legacy single-image deploy where the // UI's static table already covers the shape). Consumers should // prefer this list over their own predictions when non-empty. const phaseNames = ref<string[]>([]) const liveLogs = ref<LogEntry[]>([]) // Running count of every log line we've received this stream — // independent of ``liveLogs.length``, which is capped by the ring // buffer (``MAX_LOG_BUFFER``). The detail view shows this in the // tail header so the number keeps growing past 100 as the worker // produces output, instead of plateauing at the buffer cap. const totalLogCount = ref<number>(0) const connectionState = ref<ConnectionState>('idle') const lastError = ref<string | null>(null) let abortController: AbortController | null = null let reconnectAttempt = 0 let reconnectTimer: number | null = null const reset = () => { progress.value = null currentPhase.value = null currentPhaseIndex.value = null // Reset the dot count back to the default so a previous run's // total (e.g. 11 for deploy) doesn't briefly bleed into the next // run's stepper (e.g. 7 for destroy) before its first progress // event arrives. totalPhases.value = 11 phaseNames.value = [] liveLogs.value = [] totalLogCount.value = 0 lastError.value = null } const pushLog = (entry: LogEntry) => { // Bounded ring buffer — older entries fall off the head. Vue's // reactivity needs an array reassignment for the splice case to // be tracked across all consumers reliably. const next = [...liveLogs.value, entry] if (next.length > MAX_LOG_BUFFER) { next.splice(0, next.length - MAX_LOG_BUFFER) } liveLogs.value = next // The visible buffer is capped, but the total counter isn't — // it reflects every line the worker produced this run. totalLogCount.value += 1 } const handleEvent = (eventName: string, data: any) => { if (eventName === 'snapshot') { const snap = data as SnapshotEvent const status = (snap.status || '').toUpperCase() const isActive = status === 'PENDING' || status === 'RUNNING' // Only adopt the snapshot's progress/phase when the latest task // is actually live. After a successful deploy the snapshot still // carries ``progress_pct=100`` and ``current_phase="OUTPUTS_…"`` // for the *finished* deploy task; if a destroy then starts, the // backend's ``latest_task`` query briefly returns the old success // task. Painting those into the UI would flash the stale 11-step // deploy stepper at 100% before the destroy's first progress // event arrives. Skip adoption for terminal-status snapshots. if (isActive) { if (snap.progress_pct !== null) progress.value = snap.progress_pct if (snap.current_phase !== null) currentPhase.value = snap.current_phase } // Snapshot doesn't carry total_phases; the first progress event // will fill it in. We keep the conservative default (11). if (status === 'SUCCESS' || status === 'FAILED' || status === 'CANCELLED') { connectionState.value = 'ended' } return } if (eventName === 'progress') { const ev = data as ProgressEvent progress.value = ev.progress_pct currentPhase.value = ev.phase currentPhaseIndex.value = ev.phase_index totalPhases.value = ev.total_phases // Worker sendet die volle Phase-Sequenz mit jedem Progress- // Event mit, sobald der Task einen ``_PhaseTracker`` hat (alle // realen Deploy-/Destroy-/Pause-/Resume-/Redeploy-Pfade). Das // ist die authoritative Source-of-Truth für den Stepper — // insbesondere für Multi-Image-Deploys, wo Template-Keys nicht // aus Beobachtungsreihenfolge erratbar sind. if (Array.isArray(ev.phase_names) && ev.phase_names.length > 0) { phaseNames.value = ev.phase_names } return } if (eventName === 'log') { // Worker logs ship the human-readable ISO time as ``iso_timestamp`` // (the wire-level ``timestamp`` is reserved for Celery's // Unix-time float). Re-key to ``timestamp`` for downstream // consumers that don't need to know that detail. const raw = data as Record<string, any> const entry: LogEntry = { ...(raw as object), timestamp: raw.iso_timestamp || raw.timestamp || new Date().toISOString(), } as LogEntry pushLog(entry) return } if (eventName === 'overflow') { pushLog({ timestamp: new Date().toISOString(), level: 'WARNING', category: 'system', message: 'Live stream lagged behind — older entries dropped', }) return } if (eventName === 'succeeded' || eventName === 'failed' || eventName === 'revoked') { connectionState.value = 'ended' return } } const connect = async () => { if (!deploymentId.value) return abortController?.abort() abortController = new AbortController() connectionState.value = reconnectAttempt === 0 ? 'connecting' : 'reconnecting' const keycloak = useKeycloak() const token = await keycloak.getAccessToken() let response: Response try { response = await fetch(`${env.API_URL}/deployments/${deploymentId.value}/stream`, { signal: abortController.signal, headers: { Authorization: token ? `Bearer ${token}` : '', Accept: 'text/event-stream', }, }) } catch (err) { // Network error — schedule a retry. Auto-cancel from the user // closing the page also lands here as ``AbortError``; we ignore // those. if ((err as DOMException)?.name !== 'AbortError') { lastError.value = (err as Error).message scheduleReconnect() } return } if (!response.ok || !response.body) { lastError.value = `HTTP ${response.status}` // Don't reconnect on 401/403/404 — those won't fix themselves. if (response.status >= 400 && response.status < 500) { connectionState.value = 'error' return } scheduleReconnect() return } connectionState.value = 'live' reconnectAttempt = 0 const reader = response.body.getReader() const decoder = new TextDecoder() let buffer = '' try { while (true) { const { done, value } = await reader.read() if (done) break buffer += decoder.decode(value, { stream: true }) // SSE frames are delimited by a blank line. Split on \n\n; // keep the trailing partial frame for the next chunk. const frames = buffer.split('\n\n') buffer = frames.pop() || '' for (const frame of frames) { parseFrame(frame, handleEvent) } } } catch (err) { // Body iteration aborted — could be client cancellation or // network drop. Same retry logic as the initial connect. if ((err as DOMException)?.name !== 'AbortError') { scheduleReconnect() } return } // Stream closed cleanly by the server (terminal task state). if ((connectionState.value as ConnectionState) !== 'ended') { connectionState.value = 'ended' } } const scheduleReconnect = () => { if (connectionState.value === 'ended') return reconnectAttempt += 1 const delay = Math.min(RECONNECT_BASE_MS * 2 ** (reconnectAttempt - 1), RECONNECT_MAX_MS) connectionState.value = 'reconnecting' if (reconnectTimer !== null) { window.clearTimeout(reconnectTimer) } reconnectTimer = window.setTimeout(() => { reconnectTimer = null connect() }, delay) } const start = () => { reset() reconnectAttempt = 0 connect() } const stop = () => { abortController?.abort() abortController = null if (reconnectTimer !== null) { window.clearTimeout(reconnectTimer) reconnectTimer = null } connectionState.value = 'ended' } return { progress, currentPhase, currentPhaseIndex, totalPhases, phaseNames, liveLogs, totalLogCount, connectionState, lastError, start, stop, } } /** * Parse one ``event: ...\\ndata: ...`` block. * * SSE allows the client to receive multi-line ``data:`` fields (joined * with ``\\n``) and arbitrary ``event:`` names. Comment lines start * with ``:`` and are silently ignored — used here for keepalives. */ function parseFrame(rawFrame: string, dispatch: (eventName: string, data: any) => void) { if (!rawFrame.trim() || rawFrame.startsWith(':')) return let eventName = 'message' const dataLines: string[] = [] for (const line of rawFrame.split('\n')) { if (line.startsWith('event:')) { eventName = line.slice(6).trim() } else if (line.startsWith('data:')) { dataLines.push(line.slice(5).trimStart()) } else if (line.startsWith(':')) { // comment, ignore } } if (dataLines.length === 0) return const dataStr = dataLines.join('\n') try { const data = JSON.parse(dataStr) dispatch(eventName, data) } catch { // Non-JSON payload — pass the raw string through. Unlikely in our // pipeline (backend always emits JSON) but keeps the parser // robust against future probes. dispatch(eventName, dataStr) } } |