비동기 적용
This commit is contained in:
1 parent
95d90dcb50
commit
153b9f0ca4
6 files changed
+534
-161
No files matched your search
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
Lyric Background Tasks
|
||||
|
||||
가사 생성 관련 백그라운드 태스크를 정의합니다.
|
||||
"""
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.database.session import AsyncSessionLocal
|
||||
from app.lyric.models import Lyric
|
||||
from app.utils.chatgpt_prompt import ChatgptService
|
||||
|
||||
|
||||
async def generate_lyric_background(
|
||||
task_id: str,
|
||||
prompt: str,
|
||||
language: str,
|
||||
) -> None:
|
||||
"""백그라운드에서 ChatGPT를 통해 가사를 생성하고 Lyric 테이블을 업데이트합니다.
|
||||
|
||||
Args:
|
||||
task_id: 프로젝트 task_id
|
||||
prompt: ChatGPT에 전달할 프롬프트
|
||||
language: 가사 언어
|
||||
"""
|
||||
print(f"[generate_lyric_background] START - task_id: {task_id}")
|
||||
|
||||
try:
|
||||
# ChatGPT 서비스 초기화 (프롬프트는 이미 생성되어 있음)
|
||||
service = ChatgptService(
|
||||
customer_name="", # 프롬프트가 이미 생성되었으므로 빈 값
|
||||
region="",
|
||||
detail_region_info="",
|
||||
language=language,
|
||||
)
|
||||
|
||||
# ChatGPT를 통해 가사 생성
|
||||
print(f"[generate_lyric_background] ChatGPT generation started - task_id: {task_id}")
|
||||
result = await service.generate(prompt=prompt)
|
||||
print(f"[generate_lyric_background] ChatGPT generation completed - task_id: {task_id}")
|
||||
|
||||
# 실패 응답 검사 (ERROR 또는 ChatGPT 거부 응답)
|
||||
failure_patterns = [
|
||||
"ERROR:",
|
||||
"I'm sorry",
|
||||
"I cannot",
|
||||
"I can't",
|
||||
"I apologize",
|
||||
"I'm unable",
|
||||
"I am unable",
|
||||
"I'm not able",
|
||||
"I am not able",
|
||||
]
|
||||
is_failure = any(
|
||||
pattern.lower() in result.lower() for pattern in failure_patterns
|
||||
)
|
||||
|
||||
# Lyric 테이블 업데이트 (새 세션 사용)
|
||||
async with AsyncSessionLocal() as session:
|
||||
query_result = await session.execute(
|
||||
select(Lyric)
|
||||
.where(Lyric.task_id == task_id)
|
||||
.order_by(Lyric.created_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
lyric = query_result.scalar_one_or_none()
|
||||
|
||||
if lyric:
|
||||
if is_failure:
|
||||
print(f"[generate_lyric_background] FAILED - task_id: {task_id}, error: {result}")
|
||||
lyric.status = "failed"
|
||||
lyric.lyric_result = result
|
||||
else:
|
||||
print(f"[generate_lyric_background] SUCCESS - task_id: {task_id}")
|
||||
lyric.status = "completed"
|
||||
lyric.lyric_result = result
|
||||
|
||||
await session.commit()
|
||||
else:
|
||||
print(f"[generate_lyric_background] Lyric NOT FOUND in DB - task_id: {task_id}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[generate_lyric_background] EXCEPTION - task_id: {task_id}, error: {e}")
|
||||
# 실패 시 Lyric 테이블 업데이트
|
||||
async with AsyncSessionLocal() as session:
|
||||
query_result = await session.execute(
|
||||
select(Lyric)
|
||||
.where(Lyric.task_id == task_id)
|
||||
.order_by(Lyric.created_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
lyric = query_result.scalar_one_or_none()
|
||||
|
||||
if lyric:
|
||||
lyric.status = "failed"
|
||||
lyric.lyric_result = f"Error: {str(e)}"
|
||||
await session.commit()
|
||||
print(f"[generate_lyric_background] FAILED - task_id: {task_id}, status updated to failed")
|
||||
Reference in new issue
Block a user