diff --git a/app/posts/service.py b/app/posts/service.py index 77f1357..3539db4 100644 --- a/app/posts/service.py +++ b/app/posts/service.py @@ -1,7 +1,7 @@ import asyncio import uuid -from fastapi import HTTPException +from fastapi import BackgroundTasks, HTTPException from app.category.enums import Category from app.comments.repository import CommentRepository @@ -27,6 +27,7 @@ class PostService: post_like_repository: PostLikeRepository comment_repository: CommentRepository slack_service: SlackService + background_tasks: BackgroundTasks async def get_post_by_id(self, *, post_id: int, current_user: User) -> PostRead: post = await self.post_repository.find_by_id(post_id=post_id) @@ -170,7 +171,9 @@ async def create_post( ) if result.new_univ_major: - self.slack_service.notify_new_univ_major(result.new_univ_major) + self.background_tasks.add_task( + self.slack_service.notify_new_univ_major, result.new_univ_major + ) return PostRead.from_post( result.post, @@ -221,7 +224,9 @@ async def update_post( ) if result.new_univ_major: - self.slack_service.notify_new_univ_major(result.new_univ_major) + self.background_tasks.add_task( + self.slack_service.notify_new_univ_major, result.new_univ_major + ) [like_count, comment_count, is_liked, univ_major] = await asyncio.gather( self.post_like_repository.count_by_id(post_id=post_id), diff --git a/app/slack/service.py b/app/slack/service.py index aa1ff31..fd4da81 100644 --- a/app/slack/service.py +++ b/app/slack/service.py @@ -1,22 +1,15 @@ -from typing import Annotated - import httpx -from fastapi import BackgroundTasks, Depends from app.core.config import Environment, core_settings from app.utils.dependency import dependency from .config import slack_settings -BackgroundTasksDep = Annotated[BackgroundTasks, Depends()] - @dependency class SlackService: - background_tasks: BackgroundTasksDep - async def _send_message(self, text: str) -> None: - """슬랙으로 메시지를 전송합니다. (내부용)""" + """슬랙으로 메시지를 전송합니다.""" webhook_url = slack_settings.SLACK_WEBHOOK_URL if not webhook_url: @@ -37,8 +30,8 @@ async def _send_message(self, text: str) -> None: # 슬랙 알림 실패가 메인 로직에 영향 주지 않도록 무시 pass - def notify_new_univ_major(self, univ_major: str) -> None: - """새로운 학교/전공이 등록되었을 때 슬랙 알림을 백그라운드로 보냅니다.""" + async def notify_new_univ_major(self, univ_major: str) -> None: + """새로운 학교/전공이 등록되었을 때 슬랙 알림을 보냅니다.""" phase = core_settings.ENVIRONMENT.value.upper() message = f"[{phase}] 🎓 새로운 학교/전공 검수 요청: *{univ_major}*" - self.background_tasks.add_task(self._send_message, message) + await self._send_message(message)