-
-
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
Open
bbornino
wants to merge
4
commits into
playfulprogramming:main
Choose a base branch
from
bbornino:feature/188-s3-lifecycle-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 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 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
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 { s3 } from "@playfulprogramming/s3"; | ||
| 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 scheduleS3ObjectDeletion( | ||
| bucket: string, | ||
| key: string, | ||
| ): Promise<void> { | ||
| const lastModified = await s3.getLastModified(bucket, key); | ||
|
|
||
| await createJob( | ||
| Tasks.DELETE_S3_OBJECT, | ||
| `delete-s3-object:${bucket}:${key}`, | ||
| { bucket, key, lastModified: lastModified?.toISOString() }, | ||
| { 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.
Uh oh!
There was an error while loading. Please reload this page.