Skip to content

Commit

Permalink
Retry implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesli12 committed Oct 22, 2023
1 parent 2b0dcf4 commit 81168a5
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions web/netlify/functions/delete-post.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import storageClient from "../../db/storageConnect"
import { consts } from "../../utils/consts"

export default async (req: Request) => {
const { next_run } = await req.json()

const res = await storageClient.listObjectsV2({ Bucket: consts.storageBucket })
const posts = await Post.find({ pending: false })

//find the attachments not found in posts
const attachments = res?.Contents?.filter((x) => !posts.some((y) => y.attachments.includes(x.Key)))

//delete those attachments from bucket
await attachments?.map((x) => storageClient.deleteObject({ Bucket: "development-uploads", Key: x.Key }))

return new Response("OK")
let retryCount = 0;
let success = false;

while (!success && retryCount < 3) {
try {
const res = await storageClient.listObjectsV2({ Bucket: consts.storageBucket });
const posts = await Post.find({ pending: false });

//find the attachments not found in posts
const attachments = res?.Contents?.filter((x) => !posts.some((y) => y.attachments.includes(x.Key)));

//delete those attachments from bucket
await attachments?.map((x) => storageClient.deleteObject({ Bucket: "development-uploads", Key: x.Key }));

success = true;
return "OK";
} catch (err) {
retryCount++;
}
}

throw new Error('Failed to delete post after 3 retries');
}

export const config: Config = {
schedule: "@hourly"
schedule: "@daily"
}

0 comments on commit 81168a5

Please sign in to comment.