Add a banner to indicate updates in progress
This commit is contained in:
@@ -43,3 +43,22 @@ export function broadcastReload(reason: string, data?: Record<string, unknown>):
|
||||
}
|
||||
}
|
||||
|
||||
export type UpdateStatusPhase = 'request_received' | 'update_started' | 'update_done';
|
||||
|
||||
export function broadcastUpdateStatus(phase: UpdateStatusPhase, data?: Record<string, unknown>): void {
|
||||
if (!wss) return;
|
||||
|
||||
const payload = JSON.stringify({
|
||||
type: 'update_status',
|
||||
ts: Date.now(),
|
||||
phase,
|
||||
...data,
|
||||
});
|
||||
|
||||
for (const client of wss.clients) {
|
||||
if (client.readyState === client.OPEN) {
|
||||
client.send(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { EditJobPayload } from '@dynamic-sites/shared';
|
||||
import { logger } from '../logger.js';
|
||||
import { broadcastUpdateStatus } from '../live-reload.js';
|
||||
|
||||
export interface EditQueue {
|
||||
enqueue(payload: EditJobPayload): void;
|
||||
@@ -24,11 +25,19 @@ export function createEditQueue(): EditQueue {
|
||||
while (jobs.length > 0 && !shuttingDown) {
|
||||
const job = jobs.shift()!;
|
||||
logger.info({ event: 'job.started', kind: job.kind, id: job.id }, 'Processing job');
|
||||
broadcastUpdateStatus('update_started', { jobKind: job.kind, jobId: job.id });
|
||||
try {
|
||||
await processor!(job);
|
||||
logger.info({ event: 'job.completed', kind: job.kind, id: job.id }, 'Job completed');
|
||||
broadcastUpdateStatus('update_done', { jobKind: job.kind, jobId: job.id, ok: true });
|
||||
} catch (err) {
|
||||
logger.error({ event: 'job.failed', kind: job.kind, id: job.id, error: (err as Error).message }, 'Job failed');
|
||||
broadcastUpdateStatus('update_done', {
|
||||
jobKind: job.kind,
|
||||
jobId: job.id,
|
||||
ok: false,
|
||||
error: (err as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +60,7 @@ export function createEditQueue(): EditQueue {
|
||||
}
|
||||
jobs.push(payload);
|
||||
logger.info({ event: 'job.enqueued', kind: payload.kind, id: payload.id, depth: jobs.length }, 'Job enqueued');
|
||||
broadcastUpdateStatus('request_received', { jobKind: payload.kind, jobId: payload.id, depth: jobs.length });
|
||||
// Start draining on next tick
|
||||
if (processor) setImmediate(drain);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user