Skip to content
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

Optimize image deletion #278

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/api/db/models/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,45 @@ export class ImageModel {
context: Pick<Context, 'user'>,
): Promise<gql.StandardErrorPayload> {
try {
const res = await Promise.allSettled(
input.imageIds!.map((imageId) => {
return this.deleteImage({ imageId }, context);
const s3 = new S3.S3Client({ region: process.env.AWS_DEFAULT_REGION });
console.time('delete-images total');
console.time('delete-images mongo records');
const images = await Image.find({ _id: { $in: input.imageIds! } });

const res = await Promise.allSettled([
await Image.deleteMany({
_id: { $in: input.imageIds! },
projectId: context.user['curr_project'],
}),
await ImageAttempt.deleteMany({
_id: { $in: input.imageIds! },
projectId: context.user['curr_project'],
}),
await ImageError.deleteMany({
image: { $in: input.imageIds! },
}),
]);
console.timeEnd('delete-images mongo records');

const keys: { Key: string }[] = [];
console.time('delete-images s3 records');
images!.forEach((image) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So kinda stuck on this bit. Should we only delete images that we can find mongo records for or should we just delete all images that were inputted by the user?

['medium', 'original', 'small'].forEach((size) => {
keys.push({ Key: `${size}/${image._id}-${size}.${image.fileTypeExtension || 'jpg'}` });
});
});
const s3Res = await s3.send(
new S3.DeleteObjectsCommand({
Bucket: `animl-images-serving-${process.env.STAGE}`,
Delete: { Objects: keys },
}),
);
console.timeEnd('delete-images s3 records');

const errors = res
.filter((r): r is PromiseRejectedResult => r.status === 'rejected')
.map((r) => r.reason); // Will always be a GraphQLError

console.timeEnd('delete-images total');
return {
isOk: !errors.length,
errors,
Expand Down
Loading