From f1001b715c7e996d14d0ed537dab3a7105c7cc4b Mon Sep 17 00:00:00 2001 From: Jakob van Santen Date: Mon, 12 Feb 2024 16:01:41 +0100 Subject: [PATCH] Delete alert chunk on exception --- ampel/ztf/archive/server/app.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ampel/ztf/archive/server/app.py b/ampel/ztf/archive/server/app.py index 481e503..46cbff8 100644 --- a/ampel/ztf/archive/server/app.py +++ b/ampel/ztf/archive/server/app.py @@ -155,7 +155,7 @@ def post_alert_chunk( deserialize_avro_body ), archive: ArchiveUpdater = Depends(get_archive_updater), - bucket=Depends(get_s3_bucket), + bucket: "Bucket"=Depends(get_s3_bucket), auth: bool = Depends(verify_write_token), ): alerts, schema = content_and_schema @@ -164,16 +164,22 @@ def post_alert_chunk( key = f'{hashlib.sha256(json.dumps(sorted(alert["candid"] for alert in alerts)).encode("utf-8")).hexdigest()}.avro' md5 = base64.b64encode(hashlib.md5(blob).digest()).decode("utf-8") - s3_response = bucket.Object(key).put( + obj = bucket.Object(key) + + s3_response = obj.put( Body=blob, ContentMD5=md5, Metadata={"schema-name": schema["name"], "schema-version": schema["version"]}, ) assert 200 <= s3_response["ResponseMetadata"]["HTTPStatusCode"] < 300 - archive.insert_alert_chunk( - alerts, schema, archive_uri=get_url_for_key(bucket, key), ranges=ranges - ) + try: + archive.insert_alert_chunk( + alerts, schema, archive_uri=get_url_for_key(bucket, key), ranges=ranges + ) + except: + obj.delete() + raise def get_alert_from_s3(