Skip to content
File

Blob: src/worker/r2/gc.ts

typescript23 lines
1import { retryUntilReady, type BlobGcMessage } from "@/worker/queues/blob-gc";
2import type { QueueHandleResult } from "@/worker/queues/types";
3import { deleteFileBlob } from "@/worker/r2/blobs";
4 
5export 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 
10export 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}