feat: 삭제된 콘텐츠의 대기·예약 업로드 정리와 실행 가드 추가
This commit is contained in:
1 parent
cf6949acf7
commit
3854aef320
3 files changed
+69
-6
No files matched your search
@@ -15,6 +15,8 @@ from app.p2v.services.i2v import MIN_LONG_EDGE_PX
|
||||
from app.p2v.services.motion import plan_motion
|
||||
from app.p2v.models import PosterAliveTask
|
||||
from app.database.session import get_session
|
||||
from app.social.models import SocialUpload
|
||||
from app.social.upload_cleanup import delete_pending_uploads
|
||||
from app.user.dependencies.auth import get_current_user
|
||||
from app.user.models import User
|
||||
|
||||
@@ -171,5 +173,7 @@ async def delete(task_id: int, user: User = Depends(get_current_user),
|
||||
if task.status in (QUEUED, RUNNING):
|
||||
raise HTTPException(409, "대기 중이거나 진행 중인 작업은 삭제할 수 없습니다. 끝난 뒤 지워주세요")
|
||||
task.is_deleted = True
|
||||
# 소프트 삭제라 FK CASCADE 가 안 돈다 — 아직 게시되지 않은(대기·예약) 업로드를 같이 지운다
|
||||
await delete_pending_uploads(session, SocialUpload.p2v_video_id, task_id)
|
||||
await session.commit()
|
||||
return {"id": task_id, "removed": True}
|
||||
@@ -0,0 +1,44 @@
|
||||
"""콘텐츠 삭제 시 아직 게시되지 않은 소셜 업로드 정리.
|
||||
|
||||
콘텐츠는 소프트 삭제(`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
|
||||
@@ -144,26 +144,29 @@ async def _increment_retry_count(upload_id: int) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
async def _resolve_source_url(session, upload: SocialUpload) -> Optional[str]:
|
||||
"""업로드가 가리키는 콘텐츠 종류의 영상 URL. 병합 스키마라 채워진 FK 가 곧 종류다."""
|
||||
async def _resolve_source(session, upload: SocialUpload) -> tuple[Optional[str], bool]:
|
||||
"""업로드가 가리키는 콘텐츠의 (영상 URL, 삭제 여부). 병합 스키마라 채워진 FK 가 곧 종류다.
|
||||
|
||||
콘텐츠 행이 없으면 (None, False) — 삭제가 아니라 "찾을 수 없음"으로 구분한다.
|
||||
"""
|
||||
if upload.p2v_video_id is not None:
|
||||
row = (
|
||||
await session.execute(
|
||||
select(PosterAliveTask).where(PosterAliveTask.id == upload.p2v_video_id)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
return row.video_url if row else None
|
||||
return (row.video_url, bool(row.is_deleted)) if row else (None, False)
|
||||
if upload.content_id is not None:
|
||||
row = (
|
||||
await session.execute(
|
||||
select(SsulContent).where(SsulContent.id == upload.content_id)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
return row.video_url if row else None
|
||||
return (row.video_url, bool(row.is_deleted)) if row else (None, False)
|
||||
row = (
|
||||
await session.execute(select(Video).where(Video.id == upload.video_id))
|
||||
).scalar_one_or_none()
|
||||
return row.result_movie_url if row else None
|
||||
return (row.result_movie_url, bool(row.is_deleted)) if row else (None, False)
|
||||
|
||||
|
||||
async def process_social_upload(upload_id: int) -> None:
|
||||
@@ -192,7 +195,19 @@ async def process_social_upload(upload_id: int) -> None:
|
||||
return
|
||||
|
||||
# 2. 대상 영상 조회. video_id / content_id / p2v_video_id 중 정확히 하나만 채워져 있다(CHECK).
|
||||
source_url = await _resolve_source_url(session, upload)
|
||||
source_url, target_deleted = await _resolve_source(session, upload)
|
||||
|
||||
# 삭제된 콘텐츠는 올리지 않는다 — 소프트 삭제라 예약·재시도 업로드가 남아 있을 수 있다.
|
||||
if target_deleted:
|
||||
logger.warning(
|
||||
f"[SOCIAL_UPLOAD] 삭제된 콘텐츠라 업로드하지 않음 - upload_id: {upload_id}"
|
||||
)
|
||||
await _update_upload_status(
|
||||
upload_id=upload_id,
|
||||
status=UploadStatus.CANCELLED,
|
||||
error_message="삭제된 콘텐츠라 업로드하지 않았습니다.",
|
||||
)
|
||||
return
|
||||
|
||||
if not source_url:
|
||||
logger.error(
|
||||
|
||||
Reference in new issue
Block a user