Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Send a file that keeps failing to the back of the deletion queue
Claude0747f0ced63f
diff --git a/db/migrations/20261118100000_pending_file_deletions.sql b/db/migrations/20261118100000_pending_file_deletions.sqlindex d39b3b1..aa60957 100644--- a/db/migrations/20261118100000_pending_file_deletions.sql+++ b/db/migrations/20261118100000_pending_file_deletions.sql@@ -7,8 +7,12 @@create table if not exists public.pending_file_deletions (storage_key text primary key,envelope_id text not null,- queued_at bigint not null+ queued_at bigint not null,+ -- When a deletion was last tried and failed. The cleanup tries the least+ -- recently tried first, so files that keep failing do not hold up the+ -- rest.+ last_tried_at bigint);-create index if not exists pending_file_deletions_queued_at_idx- on public.pending_file_deletions (queued_at);+create index if not exists pending_file_deletions_order_idx+ on public.pending_file_deletions (last_tried_at nulls first, queued_at);diff --git a/src/lib/envelope-deletion.dbtest.ts b/src/lib/envelope-deletion.dbtest.tsindex 743e688..58da01b 100644--- a/src/lib/envelope-deletion.dbtest.ts+++ b/src/lib/envelope-deletion.dbtest.ts@@ -11,7 +11,11 @@ import type pg from "pg";import { openTestDatabase } from "./db/test-database.ts";import { deleteFinishedEnvelope } from "./envelope-deletion.ts";import { EnvelopeMutationError } from "./envelopes.ts";-import { runRetentionSweep } from "./retention.ts";+import {+ deletePendingFiles,+ queueFileDeletions,+ runRetentionSweep,+} from "./retention.ts";import { getRecipientCertificateKey } from "./services/storage-keys.ts";import {createStorageService,@@ -465,3 +469,50 @@ test("a deletion running while the administrator's handed-over envelopes move doassert.equal(await envelopeExists(envelope.envelopeId), true);});++test("a file that keeps failing goes to the back of the queue, so the others are still reached", async () => {+ const envelopeId = randomUUID();+ const stuck = `test/${envelopeId}/stuck.pdf`;+ const healthy = `test/${envelopeId}/healthy.pdf`;+ // Older than anything else in the queue, so both come first.+ const longAgo = 1;++ for (const key of [stuck, healthy]) {+ await storageService.putObject({+ body: new Uint8Array([37, 80, 68, 70]),+ contentType: "application/pdf",+ key,+ });+ }+ await queueFileDeletions(envelopeId, [stuck], longAgo, database);+ await queueFileDeletions(envelopeId, [healthy], longAgo + 1, database);++ const stuckStorage = {+ ...storageService,+ deleteObject: async (key: string) => {+ if (key === stuck) {+ throw new Error("Storage refuses this one.");+ }++ return storageService.deleteObject(key);+ },+ } as StorageService;+ const originalConsoleError = console.error;++ console.error = () => {};++ try {+ // One at a time: the stuck file first, then, as it was just tried, the+ // healthy one.+ assert.equal(await deletePendingFiles(database, stuckStorage, 1), 0);+ assert.equal(await deletePendingFiles(database, stuckStorage, 1), 1);+ } finally {+ console.error = originalConsoleError;+ }++ assert.deepEqual(await filesExist([stuck, healthy]), [true, false]);+ assert.deepEqual(await pendingFileDeletions(envelopeId), [stuck]);++ await deletePendingFiles(database, storageService);+ assert.deepEqual(await pendingFileDeletions(envelopeId), []);+});diff --git a/src/lib/retention.ts b/src/lib/retention.tsindex 15767fe..97baffd 100644--- a/src/lib/retention.ts+++ b/src/lib/retention.ts@@ -1002,24 +1002,39 @@ export async function deletePendingFile(`Failed to delete stored file ${key}; it stays queued for the next cleanup:`,error,);+ // To the back of the queue, so that files which keep failing do not+ // stop the cleanup from reaching the others.+ await database+ .query(+ `UPDATE pending_file_deletions SET last_tried_at = $2 WHERE storage_key = $1`,+ [key, getCurrentUnixTimestamp()],+ )+ .catch((updateError: unknown) => {+ console.error(+ `Failed to note the failed deletion of stored file ${key}:`,+ updateError,+ );+ });return false;}}-async function deletePendingFiles(+/** Tries the queued file deletions, least recently tried first. */+export async function deletePendingFiles(database: DbExecutor,storageService: StorageService,+ limit = PENDING_FILE_DELETIONS_PER_SWEEP,): Promise<number> {const pending = await queryMany<{ storageKey: string }>(database,`SELECT storage_key AS "storageKey"FROM pending_file_deletions- ORDER BY queued_at ASC, storage_key ASC+ ORDER BY last_tried_at ASC NULLS FIRST, queued_at ASC, storage_key ASCLIMIT $1`,- [PENDING_FILE_DELETIONS_PER_SWEEP],+ [limit],);let done = 0;