45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
"""콘텐츠 삭제 시 아직 게시되지 않은 소셜 업로드 정리.
|
|
|
|
콘텐츠는 소프트 삭제(`is_deleted=True`)라 `social_upload` 의 FK `ON DELETE CASCADE` 가
|
|
작동하지 않는다. 지우지 않으면 예약 시각에 삭제된 영상이 게시된다.
|
|
|
|
가벼운 모듈로 둔다: `app.social.services` 패키지를 import 하면 업로드 서비스·워커까지
|
|
딸려 와서 P2V 라우터 등과 순환 import 가 생길 수 있다.
|
|
"""
|
|
import logging
|
|
|
|
from sqlalchemy import delete
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import InstrumentedAttribute
|
|
|
|
from app.social.constants import UploadStatus
|
|
from app.social.models import SocialUpload
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def delete_pending_uploads(
|
|
session: AsyncSession,
|
|
target_column: InstrumentedAttribute,
|
|
target_id: int,
|
|
) -> int:
|
|
"""대상 콘텐츠의 대기 중(PENDING) 업로드 — 즉시 대기와 예약 — 를 삭제하고 삭제한 수를 돌려준다.
|
|
|
|
`target_column` 은 `SocialUpload.video_id` / `content_id` / `p2v_video_id` 중 하나다.
|
|
게시 중(UPLOADING)과 이미 끝난 기록(COMPLETED/FAILED/CANCELLED)은 건드리지 않는다.
|
|
커밋하지 않는다 — 호출부가 소프트 삭제와 같은 트랜잭션으로 커밋한다.
|
|
"""
|
|
result = await session.execute(
|
|
delete(SocialUpload).where(
|
|
target_column == target_id,
|
|
SocialUpload.status == UploadStatus.PENDING.value,
|
|
)
|
|
)
|
|
removed = result.rowcount or 0
|
|
if removed:
|
|
logger.info(
|
|
f"[UPLOAD_CLEANUP] 삭제된 콘텐츠의 대기 업로드 제거 - "
|
|
f"column: {target_column.key}, id: {target_id}, count: {removed}"
|
|
)
|
|
return removed
|