refactor: enhance logging and task management in document storage and upload tasks
This commit is contained in:
@@ -6,6 +6,7 @@ import dropbox
|
||||
from app.config import settings
|
||||
from app.tasks.retry_config import BaseTaskWithRetry
|
||||
from app.celery_app import celery
|
||||
from app.utils import task_logger, log_task
|
||||
|
||||
def get_dropbox_access_token():
|
||||
"""Refresh the Dropbox access token using the stored refresh token from ENV."""
|
||||
@@ -25,14 +26,16 @@ def get_dropbox_access_token():
|
||||
return response.json()["access_token"]
|
||||
else:
|
||||
error_msg = f"Failed to refresh Dropbox token: {response.status_code} - {response.text}"
|
||||
print(f"[ERROR] {error_msg}")
|
||||
task_logger(error_msg, level="error", step_name="dropbox_auth")
|
||||
raise Exception(error_msg)
|
||||
|
||||
@celery.task(base=BaseTaskWithRetry)
|
||||
@log_task("upload_to_dropbox")
|
||||
def upload_to_dropbox(file_path: str):
|
||||
"""Uploads a file to Dropbox using the API."""
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
task_logger(f"File not found: {file_path}", level="error", step_name="dropbox_upload")
|
||||
raise FileNotFoundError(f"File not found: {file_path}")
|
||||
|
||||
# Extract filename and set target path
|
||||
@@ -41,16 +44,20 @@ def upload_to_dropbox(file_path: str):
|
||||
|
||||
try:
|
||||
# Get fresh access token
|
||||
task_logger(f"Getting Dropbox access token", step_name="dropbox_auth")
|
||||
access_token = get_dropbox_access_token()
|
||||
dbx = dropbox.Dropbox(access_token)
|
||||
|
||||
file_size = os.path.getsize(file_path)
|
||||
chunk_size = 4 * 1024 * 1024 # 4MB chunk size
|
||||
|
||||
task_logger(f"Starting upload of {filename} ({file_size} bytes) to Dropbox", step_name="dropbox_upload")
|
||||
|
||||
with open(file_path, "rb") as file_data:
|
||||
if file_size <= chunk_size:
|
||||
dbx.files_upload(file_data.read(), dropbox_path)
|
||||
else:
|
||||
task_logger(f"Using chunked upload for {filename}", step_name="dropbox_upload")
|
||||
upload_session_start_result = dbx.files_upload_session_start(file_data.read(chunk_size))
|
||||
cursor = dropbox.files.UploadSessionCursor(
|
||||
session_id=upload_session_start_result.session_id,
|
||||
@@ -65,10 +72,10 @@ def upload_to_dropbox(file_path: str):
|
||||
dbx.files_upload_session_append_v2(file_data.read(chunk_size), cursor)
|
||||
cursor.offset = file_data.tell()
|
||||
|
||||
print(f"[INFO] Successfully uploaded {filename} to Dropbox at {dropbox_path}.")
|
||||
task_logger(f"Successfully uploaded {filename} to Dropbox at {dropbox_path}", step_name="dropbox_upload", status="success")
|
||||
return {"status": "Completed", "file": file_path}
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"[ERROR] Failed to upload {filename} to Dropbox: {str(e)}"
|
||||
print(error_msg)
|
||||
error_msg = f"Failed to upload {filename} to Dropbox: {str(e)}"
|
||||
task_logger(error_msg, level="error", step_name="dropbox_upload", status="failure")
|
||||
raise Exception(error_msg)
|
||||
|
||||
Reference in New Issue
Block a user