-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(worker): delay S3 object deletion instead of removing immediately #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fennifith
merged 8 commits into
playfulprogramming:main
from
bbornino:feature/188-s3-lifecycle-config
Aug 2, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ae92a8
feat(worker): delay S3 object deletion instead of removing immediately
bbornino 8e9913e
fix(worker): guard delete-s3-object against re-referenced content-add…
bbornino 4cb9359
fix(worker): scope delete-s3-object job ids to the object's generation
bbornino b80ce7b
fix(worker): don't schedule delete-s3-object when LastModified is unk…
bbornino 29785d8
refactor(worker): decouple scheduleS3ObjectDeletion from packages/s3
bbornino 2e9b29a
Merge branch 'main' into feature/188-s3-lifecycle-config
fennifith 2d9a611
fix(worker): scope sync-post attachment query by branch, drop dead sc…
bbornino 8dfaecb
revert branch filter & drop assertions in post-sync tests
fennifith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import processor from "./processor.ts"; | ||
| import type { TaskInputs } from "@playfulprogramming/bullmq"; | ||
| import type { Job } from "bullmq"; | ||
| import { s3 } from "@playfulprogramming/s3"; | ||
|
|
||
| test("removes the object when no lastModified was captured at scheduling time", async () => { | ||
| await processor({ | ||
| data: { | ||
| bucket: "example-bucket", | ||
| key: "posts/example-post/attachments/notes.pdf", | ||
| }, | ||
| } as unknown as Job<TaskInputs["delete-s3-object"]>); | ||
|
|
||
| expect(s3.unmodifiedSince).not.toBeCalled(); | ||
| expect(s3.remove).toBeCalledWith( | ||
| "example-bucket", | ||
| "posts/example-post/attachments/notes.pdf", | ||
| ); | ||
| }); | ||
|
|
||
| test("removes the object when it hasn't been modified since scheduling", async () => { | ||
| vi.mocked(s3.unmodifiedSince).mockResolvedValueOnce(true); | ||
|
|
||
| await processor({ | ||
| data: { | ||
| bucket: "example-bucket", | ||
| key: "posts/example-post/attachments/notes.pdf", | ||
| lastModified: "2025-05-05T00:00:00.000Z", | ||
| }, | ||
| } as unknown as Job<TaskInputs["delete-s3-object"]>); | ||
|
|
||
| expect(s3.unmodifiedSince).toBeCalledWith( | ||
| "example-bucket", | ||
| "posts/example-post/attachments/notes.pdf", | ||
| new Date("2025-05-05T00:00:00.000Z"), | ||
| ); | ||
| expect(s3.remove).toBeCalledWith( | ||
| "example-bucket", | ||
| "posts/example-post/attachments/notes.pdf", | ||
| ); | ||
| }); | ||
|
|
||
| test("skips removal when the object was rewritten since scheduling", async () => { | ||
| vi.mocked(s3.unmodifiedSince).mockResolvedValueOnce(false); | ||
|
|
||
| await processor({ | ||
| data: { | ||
| bucket: "example-bucket", | ||
| key: "posts/example-post/attachments/notes.pdf", | ||
| lastModified: "2025-05-05T00:00:00.000Z", | ||
| }, | ||
| } as unknown as Job<TaskInputs["delete-s3-object"]>); | ||
|
|
||
| expect(s3.remove).not.toBeCalled(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { Tasks } from "@playfulprogramming/bullmq"; | ||
| import { s3 } from "@playfulprogramming/s3"; | ||
| import { createProcessor } from "../../createProcessor.ts"; | ||
|
|
||
| export default createProcessor(Tasks.DELETE_S3_OBJECT, async (job) => { | ||
| const { bucket, key, lastModified } = job.data; | ||
|
|
||
| if (lastModified !== undefined) { | ||
| const stillUnmodified = await s3.unmodifiedSince( | ||
| bucket, | ||
| key, | ||
| new Date(lastModified), | ||
| ); | ||
|
|
||
| if (!stillUnmodified) { | ||
| console.log( | ||
| `Skipped removal of ${bucket}/${key} - object was rewritten since deletion was scheduled`, | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| await s3.remove(bucket, key); | ||
| console.log(`Removed ${bucket}/${key} from S3 after grace period`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { scheduleS3ObjectDeletion } from "./scheduleS3ObjectDeletion.ts"; | ||
| import { enqueueS3ObjectDeletion } from "@playfulprogramming/bullmq"; | ||
| import { s3 } from "@playfulprogramming/s3"; | ||
|
|
||
| // This module is mocked wholesale in test-utils/setup.ts for every other | ||
| // test file's benefit (they only care that scheduling happened, not how) - | ||
| // undo that here so this file exercises the real implementation. | ||
| vi.unmock("./scheduleS3ObjectDeletion.ts"); | ||
|
|
||
| test("skips scheduling and warns when lastModified can't be determined", async () => { | ||
| vi.mocked(s3.getLastModified).mockResolvedValueOnce(undefined); | ||
| const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
|
|
||
| await scheduleS3ObjectDeletion("example-bucket", "posts/example/notes.pdf"); | ||
|
|
||
| expect(enqueueS3ObjectDeletion).not.toBeCalled(); | ||
| expect(warnSpy).toBeCalledWith( | ||
| expect.stringContaining("posts/example/notes.pdf"), | ||
| ); | ||
| }); | ||
|
|
||
| test("passes the object's lastModified through to enqueueS3ObjectDeletion", async () => { | ||
| const lastModified = new Date("2026-01-01T00:00:00.000Z"); | ||
| vi.mocked(s3.getLastModified).mockResolvedValueOnce(lastModified); | ||
|
|
||
| await scheduleS3ObjectDeletion("example-bucket", "posts/example/notes.pdf"); | ||
|
|
||
| expect(enqueueS3ObjectDeletion).toBeCalledWith( | ||
| "example-bucket", | ||
| "posts/example/notes.pdf", | ||
| lastModified, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { s3 } from "@playfulprogramming/s3"; | ||
| import { enqueueS3ObjectDeletion } from "@playfulprogramming/bullmq"; | ||
|
|
||
| export async function scheduleS3ObjectDeletion( | ||
| bucket: string, | ||
| key: string, | ||
| ): Promise<void> { | ||
| const lastModified = await s3.getLastModified(bucket, key); | ||
|
|
||
| if (lastModified === undefined) { | ||
| // Without a LastModified to check at execution time, the processor | ||
| // would have no way to detect a rewrite during the grace period and | ||
| // would unconditionally delete whatever's at this key 24h from now - | ||
| // including a legitimate new upload. Bail out instead of scheduling | ||
| // an unsafe deletion, but log it: a genuine transient failure to read | ||
| // the object's metadata here means this object never gets scheduled | ||
| // for cleanup at all, so it'd otherwise leak in S3 with no trace. | ||
| console.warn( | ||
| `Skipped scheduling deletion of ${bucket}/${key} - could not read its LastModified`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| await enqueueS3ObjectDeletion(bucket, key, lastModified); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { enqueueS3ObjectDeletion } from "./delete-s3-object.ts"; | ||
| import { createJob } from "../queues.ts"; | ||
|
|
||
| vi.mock("../queues.ts", () => ({ | ||
| createJob: vi.fn(), | ||
| })); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| function jobIdFromCall(callIndex: number): string { | ||
| return vi.mocked(createJob).mock.calls[callIndex][1] as string; | ||
| } | ||
|
|
||
| test("reuses the same job id when lastModified is unchanged across calls", async () => { | ||
| const lastModified = new Date("2026-01-01T00:00:00.000Z"); | ||
|
|
||
| await enqueueS3ObjectDeletion( | ||
| "example-bucket", | ||
| "posts/example/notes.pdf", | ||
| lastModified, | ||
| ); | ||
| await enqueueS3ObjectDeletion( | ||
| "example-bucket", | ||
| "posts/example/notes.pdf", | ||
| lastModified, | ||
| ); | ||
|
|
||
| expect(jobIdFromCall(0)).toEqual(jobIdFromCall(1)); | ||
| }); | ||
|
|
||
| test("uses a different job id when lastModified changes between calls", async () => { | ||
| await enqueueS3ObjectDeletion( | ||
| "example-bucket", | ||
| "posts/example/notes.pdf", | ||
| new Date("2026-01-01T00:00:00.000Z"), | ||
| ); | ||
| await enqueueS3ObjectDeletion( | ||
| "example-bucket", | ||
| "posts/example/notes.pdf", | ||
| new Date("2026-01-02T00:00:00.000Z"), | ||
| ); | ||
|
|
||
| expect(jobIdFromCall(0)).not.toEqual(jobIdFromCall(1)); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { createJob } from "../queues.ts"; | ||
| import { Tasks } from "./types.ts"; | ||
|
|
||
| export interface DeleteS3ObjectInput { | ||
| bucket: string; | ||
| key: string; | ||
| // ISO timestamp of the object's LastModified at scheduling time. The | ||
| // processor re-checks this before deleting, so a key that gets rewritten | ||
| // in the meantime (even with byte-identical content, which leaves its | ||
| // ETag unchanged) doesn't get deleted out from under its new reference. | ||
| lastModified: string; | ||
| } | ||
|
|
||
| export type DeleteS3ObjectOutput = void; | ||
|
|
||
| // Grace period before a scheduled S3 deletion actually runs, so the frontend | ||
| // or CDN doesn't hit a 404 for a key it just fetched or cached. | ||
| export const DELETE_S3_OBJECT_GRACE_PERIOD_MS = 24 * 60 * 60 * 1000; | ||
|
|
||
| export async function enqueueS3ObjectDeletion( | ||
| bucket: string, | ||
| key: string, | ||
| lastModified: Date, | ||
| ): Promise<void> { | ||
| const lastModifiedIso = lastModified.toISOString(); | ||
|
|
||
| // The job ID includes a generation marker (the object's LastModified) so | ||
| // that scheduling a deletion for a key that's since been rewritten gets | ||
| // its own job instead of silently deduplicating against - and being | ||
| // dropped in favor of - a still-pending job for the previous generation | ||
| // of that key. | ||
| await createJob( | ||
| Tasks.DELETE_S3_OBJECT, | ||
| `delete-s3-object:${bucket}:${key}:${lastModifiedIso}`, | ||
| { bucket, key, lastModified: lastModifiedIso }, | ||
| { delay: DELETE_S3_OBJECT_GRACE_PERIOD_MS }, | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add
IfMatchLastModifiedTimeto the DeleteObjectCommand input instead of checking this in a separate call?