From 68aea778b7e62fae161afe1fadab4c4a43a5ec5c Mon Sep 17 00:00:00 2001 From: sujanadh Date: Thu, 8 Feb 2024 14:11:31 +0545 Subject: [PATCH] Fix: error handling in s3 --- src/backend/app/s3.py | 5 ++-- .../app/submissions/submission_crud.py | 23 ++++++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/backend/app/s3.py b/src/backend/app/s3.py index b76297dd5f..592f996f81 100644 --- a/src/backend/app/s3.py +++ b/src/backend/app/s3.py @@ -112,8 +112,9 @@ def get_obj_from_bucket(bucket_name: str, s3_path: str) -> Optional[BytesIO]: try: response = client.get_object(bucket_name, s3_path) return BytesIO(response.read()) - except Exception: - return None + except Exception as e: + log.warning(f"Failed attempted download from S3 path: {s3_path}") + raise ValueError(str(e)) from e finally: if response: response.close() diff --git a/src/backend/app/submissions/submission_crud.py b/src/backend/app/submissions/submission_crud.py index d4c255c1d1..4da26ee78e 100644 --- a/src/backend/app/submissions/submission_crud.py +++ b/src/backend/app/submissions/submission_crud.py @@ -357,22 +357,19 @@ def update_submission_in_s3( metadata_s3_path = f"/{s3_project_path}/submissions.meta.json" try: # Get the last submission date from the metadata - file = get_obj_from_bucket(settings.S3_BUCKET_NAME, metadata_s3_path) - if file: - zip_file_last_submission = (json.loads(file.getvalue()))[ - "last_submission" - ] - if last_submission <= zip_file_last_submission: - # Update background task status to COMPLETED - update_bg_task_sync = async_to_sync( - project_crud.update_background_task_status_in_database - ) - update_bg_task_sync(db, background_task_id, 4) # 4 is COMPLETED - return + data = get_obj_from_bucket(settings.S3_BUCKET_NAME, metadata_s3_path) + + zip_file_last_submission = (json.loads(data.getvalue()))["last_submission"] + if last_submission <= zip_file_last_submission: + # Update background task status to COMPLETED + update_bg_task_sync = async_to_sync( + project_crud.update_background_task_status_in_database + ) + update_bg_task_sync(db, background_task_id, 4) # 4 is COMPLETED + return except Exception as e: log.warning(str(e)) - pass # Zip file is outdated, regenerate metadata = {