File
Blob: src/worker/r2/gc.ts
| 1 | import { retryUntilReady, type BlobGcMessage } from "@/worker/queues/blob-gc"; |
| 2 | import type { QueueHandleResult } from "@/worker/queues/types"; |
| 3 | import { deleteFileBlob } from "@/worker/r2/blobs"; |
| 4 | |
| 5 | export async function enqueueBlobGc(env: Env, messages: BlobGcMessage[]): Promise<void> { |
| 6 | if (messages.length === 0) return; |
| 7 | await env.BLOB_GC.sendBatch(messages.map((body) => ({ body }))); |
| 8 | } |
| 9 | |
| 10 | export async function handleBlobGc(env: Env, message: BlobGcMessage, nowMs = Date.now()): Promise<QueueHandleResult> { |
| 11 | const pendingRetry = retryUntilReady(message, nowMs); |
| 12 | if (pendingRetry) return pendingRetry; |
| 13 | const object = env.FILE_DAV.getByName(message.storage_id); |
| 14 | const result = await object.verifyBlobUnreferenced({ |
| 15 | subjectId: message.subject_id, |
| 16 | blobId: message.blob_id, |
| 17 | blobKey: message.blob_key, |
| 18 | nowMs, |
| 19 | }); |
| 20 | if (result.unreferenced) await deleteFileBlob(env.FILE_BLOBS, message.blob_key); |
| 21 | return { action: "ack" }; |
| 22 | } |