import type { QueueHandleResult } from "@/worker/queues/types"; export interface BlobGcMessage { type: "r2_blob_gc"; subject_id: string; storage_id: string; blob_id: string; blob_key: string; not_before_ms: number; } export function blobGcMessage(input: { subjectId: string; storageId: string; blobId: string; blobKey: string; notBeforeMs: number; }): BlobGcMessage { return { type: "r2_blob_gc", subject_id: input.subjectId, storage_id: input.storageId, blob_id: input.blobId, blob_key: input.blobKey, not_before_ms: input.notBeforeMs, }; } export function isBlobGcMessage(value: unknown): value is BlobGcMessage { if (!value || typeof value !== "object") return false; const record = value as Record; return ( record.type === "r2_blob_gc" && typeof record.subject_id === "string" && record.subject_id.length > 0 && typeof record.storage_id === "string" && record.storage_id.length > 0 && typeof record.blob_id === "string" && record.blob_id.length > 0 && typeof record.blob_key === "string" && record.blob_key.length > 0 && typeof record.not_before_ms === "number" && Number.isFinite(record.not_before_ms) ); } export function retryUntilReady(message: BlobGcMessage, nowMs: number): QueueHandleResult | null { if (message.not_before_ms <= nowMs) return null; const delaySeconds = Math.max(1, Math.min(900, Math.ceil((message.not_before_ms - nowMs) / 1000))); return { action: "retry", delaySeconds }; }