From 28dc319f59cf30dd48047e91ab8e7f81dbb99241 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Tue, 12 Nov 2024 13:11:21 +0100 Subject: [PATCH] delete object from S3 if database save fails --- aggrec/aggregates.py | 13 ++++++++++--- aggrec/settings.py | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/aggrec/aggregates.py b/aggrec/aggregates.py index c811590..96d2c22 100644 --- a/aggrec/aggregates.py +++ b/aggrec/aggregates.py @@ -9,6 +9,7 @@ import bson import pendulum +import pymongo from bson.objectid import ObjectId from fastapi import APIRouter, Header, HTTPException, Request, Response, status from fastapi.responses import StreamingResponse @@ -299,9 +300,15 @@ async def create_aggregate( ) logger.info("Object created: %s", metadata.s3_object_key) - with tracer.start_as_current_span("mongodb.insert"): - metadata.save() - logger.info("Metadata saved: %s", metadata.id) + with tracer.start_as_current_span("mongodb.insert"): + try: + with pymongo.timeout(2): + metadata.save(request.app.settings.mongodb.timeout) + logger.info("Metadata saved: %s", metadata.id) + except Exception as exc: + logger.error("Failed to save metadata, deleting object %s", metadata.s3_object_key, exc_info=exc) + await s3_client.delete_object(Bucket=s3_bucket, Key=metadata.s3_object_key) + raise HTTPException(status.HTTP_502_BAD_GATEWAY, "Database error") from exc aggregates_counter.add(1, {"aggregate_type": aggregate_type.value}) diff --git a/aggrec/settings.py b/aggrec/settings.py index ee0778a..d23163a 100644 --- a/aggrec/settings.py +++ b/aggrec/settings.py @@ -29,6 +29,7 @@ class MqttSettings(BaseModel): class MongoDB(BaseModel): server: MongodbUrl | None = Field(default="mongodb://localhost/aggregates") + timeout: int = Field(default=5) class S3(BaseModel):