forked from mtusi2026/mtusi2026lab4
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
"""Best-effort cleanup for abandoned presigned uploads."""
|
|
|
|
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.core.config import settings
|
|
from app.db.models import AttachmentUpload
|
|
from app.db.session import SessionLocal
|
|
from app.services.object_storage import client
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def cleanup_expired_uploads() -> int:
|
|
"""Delete old pending objects and mark their upload records expired.
|
|
|
|
A grace period protects clients completing an upload near URL expiry.
|
|
PostgreSQL SKIP LOCKED makes this safe across multiple API replicas.
|
|
"""
|
|
cutoff = datetime.now(timezone.utc) - timedelta(
|
|
seconds=settings.attachment_cleanup_grace_seconds
|
|
)
|
|
deleted_count = 0
|
|
|
|
with SessionLocal() as db:
|
|
uploads = db.scalars(
|
|
select(AttachmentUpload)
|
|
.where(
|
|
AttachmentUpload.status == "pending",
|
|
AttachmentUpload.expires_at <= cutoff,
|
|
)
|
|
.order_by(AttachmentUpload.expires_at.asc())
|
|
.limit(settings.attachment_cleanup_batch_size)
|
|
.with_for_update(skip_locked=True)
|
|
).all()
|
|
|
|
if not uploads:
|
|
return 0
|
|
|
|
s3 = client()
|
|
try:
|
|
for upload in uploads:
|
|
# DeleteObject is idempotent: a missing object is also safe.
|
|
s3.delete_object(
|
|
Bucket=settings.s3_bucket,
|
|
Key=upload.object_key,
|
|
)
|
|
upload.status = "expired"
|
|
deleted_count += 1
|
|
|
|
db.commit()
|
|
except Exception:
|
|
db.rollback()
|
|
logger.exception(
|
|
"Attachment cleanup batch failed; pending records will be retried"
|
|
)
|
|
return 0
|
|
|
|
if deleted_count:
|
|
logger.info(
|
|
"Expired abandoned attachment uploads cleaned: count=%d",
|
|
deleted_count,
|
|
)
|
|
return deleted_count
|