Skip to content

Commit

Permalink
Delete alert chunk on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jvansanten committed Feb 12, 2024
1 parent a75a64a commit f1001b7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ampel/ztf/archive/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit f1001b7

Please sign in to comment.