Skip to content

Commit 73e8d9c

Browse files
committed
add celery beat translate_pending_rss_item
1 parent 09e7c38 commit 73e8d9c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pythonkr_backend/curation/tasks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.utils import timezone as django_timezone
88
from django.core.files.base import ContentFile
99
from .models import RSSFeed, RSSItem
10+
from .utils_trans import translate_rssitem
1011

1112
def crawl_all_rss_feeds():
1213
"""모든 활성화된 RSS 피드를 크롤링합니다."""
@@ -206,3 +207,55 @@ def crawl_rss_item_content():
206207
"item_id": pending_item.id,
207208
"error": error_msg
208209
}
210+
211+
212+
@shared_task
213+
def translate_pending_rss_item():
214+
"""크롤링이 완료되고 번역 대기 상태인 RSS 아이템을 번역하는 태스크 (10분마다 실행)"""
215+
logfire.info("Starting RSS item translation")
216+
217+
# 크롤링이 완료되고 번역 대기 상태이며 translated_contents가 없는 아이템 1개 가져오기
218+
pending_item = RSSItem.objects.filter(
219+
crawling_status='completed',
220+
translate_status='pending'
221+
).exclude(
222+
translated_contents__isnull=False
223+
).order_by('-crawled_at', '-created_at').first()
224+
225+
if not pending_item:
226+
logfire.info("No pending RSS items to translate")
227+
return {"status": "no_items", "message": "No pending items to translate"}
228+
229+
logfire.info(f"Translating RSS item: {pending_item.title} ({pending_item.link})")
230+
231+
try:
232+
# 번역 실행
233+
translated_content = translate_rssitem(pending_item.id)
234+
235+
# 번역 상태를 완료로 변경
236+
pending_item.translate_status = 'completed'
237+
pending_item.translate_error_message = '' # 성공 시 에러 메시지 초기화
238+
pending_item.save(update_fields=['translate_status', 'translate_error_message'])
239+
240+
logfire.info(f"Successfully translated RSS item: {pending_item.title}")
241+
242+
return {
243+
"status": "success",
244+
"item_id": pending_item.id,
245+
"item_title": pending_item.title,
246+
"translated_content_id": translated_content.id
247+
}
248+
249+
except Exception as e:
250+
error_msg = f"Error translating RSS item {pending_item.id}: {str(e)}"
251+
pending_item.translate_status = 'failed'
252+
pending_item.translate_error_message = error_msg
253+
pending_item.save(update_fields=['translate_status', 'translate_error_message'])
254+
255+
logfire.error(error_msg)
256+
257+
return {
258+
"status": "failed",
259+
"item_id": pending_item.id,
260+
"error": error_msg
261+
}

pythonkr_backend/pythonkr_backend/settings/prod.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@
101101
'schedule': crontab(minute='*/10'), # Every 10 minutes
102102
'options': {'queue': 'celery'}
103103
},
104+
'translate-pending-rss-item': {
105+
'task': 'curation.tasks.translate_pending_rss_item',
106+
'schedule': crontab(minute='*/10'), # Every 10 minutes
107+
'options': {'queue': 'celery'}
108+
},
104109
}

0 commit comments

Comments
 (0)