feat(ssulbox): 썰박스 생성 파이프라인 통합 및 콘텐츠 목록 UNION

This commit is contained in:
skkim committed 2026-08-11 11:03:03 +09:00
1 parent 8096837b13
commit 666c3bf6d1
77 files changed
+15912 -349

No files matched your search

+47 -24
View File
@@ -12,11 +12,15 @@ from sqlalchemy import delete, insert, tuple_
from sqlalchemy.ext.asyncio import AsyncSession
from app.database.like_cache import (
CT_SSUL,
CT_VIDEO,
commit_dirty_processing,
drain_dirty,
is_user_liked,
)
from app.database.session import get_session
# 썰박스 좋아요도 같은 Redis write-behind 큐 + **같은 테이블**(video_reaction)을
# 쓰므로 여기서 함께 플러시한다. 큐가 하나라 스케줄러를 늘릴 필요가 없다.
from app.video.models import VideoReaction
from config import internal_settings
@@ -48,49 +52,68 @@ async def flush_reactions(
detail="Invalid internal secret",
)
pairs = await drain_dirty()
if not pairs:
entries = await drain_dirty()
if not entries:
logger.info("[REACTION_FLUSH] dirty 항목 없음, 종료")
return {"flushed": 0, "adds": 0, "dels": 0}
logger.info(f"[REACTION_FLUSH] START - dirty 항목 {len(pairs)}건")
logger.info(f"[REACTION_FLUSH] START - dirty 항목 {len(entries)}건")
# 두 종류가 **같은 테이블**(video_reaction)에 들어간다. 대상 컬럼만 다르다.
# ADO2 는 video_id, 썰박스는 content_id 를 채운다(나머지는 NULL, CHECK 로 강제).
id_col_of = {CT_VIDEO: "video_id", CT_SSUL: "content_id"}
adds: list[dict] = []
dels: list[tuple[int, str]] = []
# (컬럼명, 대상 id, user_uuid) — 삭제는 컬럼이 달라 종류별로 묶어야 한다
dels: dict[str, list[tuple[int, str]]] = {"video_id": [], "content_id": []}
# Redis 현재 상태 기준으로 add / delete 분류
for video_id, user_uuid in pairs:
liked = await is_user_liked(video_id, user_uuid)
for ctype, content_id, user_uuid in entries:
id_col = id_col_of.get(ctype)
if id_col is None:
# 알 수 없는 종류 — 건너뛴다(파서가 걸러주지만 방어)
logger.warning(f"[REACTION_FLUSH] 알 수 없는 종류 무시 - ctype: {ctype}")
continue
liked = await is_user_liked(content_id, user_uuid, ctype=ctype)
if liked:
adds.append({"video_id": video_id, "user_uuid": user_uuid})
adds.append({id_col: content_id, "user_uuid": user_uuid})
else:
dels.append((video_id, user_uuid))
dels[id_col].append((content_id, user_uuid))
total_adds = len(adds)
total_dels = sum(len(v) for v in dels.values())
try:
# Bulk INSERT IGNORE — UniqueConstraint 보장으로 멱등 처리
if adds:
await session.execute(
insert(VideoReaction).prefix_with("IGNORE").values(adds)
)
# 쓰기를 **하나의 트랜잭션**으로 묶는다.
# 부분 반영이 나면 Redis processing SET 이 남아 다음 회차에 재시도되는데,
# INSERT IGNORE + UNIQUE 로 멱등이라 중복 반영은 안전하다.
#
# ⚠️ adds 는 종류별로 키가 달라(video_id vs content_id) 한 번의
# `values(adds)` 로 묶으면 안 된다 — SQLAlchEmy 가 첫 dict 의 키로
# 컬럼 목록을 정하므로 다른 종류의 값이 누락된다. 종류별로 나눠 실행한다.
for id_col in ("video_id", "content_id"):
rows = [a for a in adds if id_col in a]
if rows:
await session.execute(
insert(VideoReaction).prefix_with("IGNORE").values(rows)
)
# Bulk DELETE
if dels:
await session.execute(
delete(VideoReaction).where(
tuple_(
VideoReaction.video_id,
VideoReaction.user_uuid,
).in_(dels)
if dels[id_col]:
await session.execute(
delete(VideoReaction).where(
tuple_(
getattr(VideoReaction, id_col),
VideoReaction.user_uuid,
).in_(dels[id_col])
)
)
)
await session.commit()
await commit_dirty_processing()
logger.info(
f"[REACTION_FLUSH] SUCCESS - adds: {len(adds)}, dels: {len(dels)}"
f"[REACTION_FLUSH] SUCCESS - adds: {total_adds}, dels: {total_dels}"
)
return {"flushed": len(pairs), "adds": len(adds), "dels": len(dels)}
return {"flushed": len(entries), "adds": total_adds, "dels": total_dels}
except Exception as e:
await session.rollback()