-
Notifications
You must be signed in to change notification settings - Fork 0
[HSC-288] recommendation-server-v0.0.2 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fadb16f
[HSC-276] feat: ๋ถ์ ์๋ฒ ์๋ต ๋ฐํ ํ๋ฆ ์ถ๊ฐ
tkv00 5e59a65
[HSC-276] feat: MSK IAM ์นดํ์นด ์ธ์ฆ ์ง์
tkv00 f9a611e
[HSC-272] feat: ์นดํ์นด config (๋ฉ์์ง ์ ๋ฌ)
rettooo cfef2b7
[HSC-272] feat: ์ถ์ฒ api
rettooo 3f598a8
[HSC-272] feat: db ์ฒดํฌ
rettooo 57c73e6
[HSC-272] feat: ์ถ์ฒ ์คํค๋ง
rettooo ecffdf9
[HSC-272] feat: ์ถ์ฒ ์๋น์ค ๋ก์ง
rettooo eef8cfb
[HSC-272] feat: DB ์ ๋ณด์์ ๊ฒ์ ์ฟผ๋ฆฌ ํ
์คํธ ์์ฑ
rettooo 87ee8bc
[HSC-272] feat: ์ถ์ฒ ํ๋กฌํํธ
rettooo b936f43
Merge pull request #28 from one-year-gap/refactor/HSC-276
tkv00 93307ad
Merge branch 'dev' into feat/HSC-272
tkv00 2fc260b
Merge pull request #29 from one-year-gap/feat/HSC-272
tkv00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """FastAPI entrypoint for the ephemeral analysis server.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from contextlib import asynccontextmanager | ||
|
|
||
| from fastapi import FastAPI, HTTPException | ||
|
|
||
| from app.core.config import get_settings | ||
| from app.core.logging import configure_logging | ||
| from app.services.kafka_analysis_consumer_service import KafkaAnalysisConsumerService | ||
|
|
||
| settings = get_settings() | ||
| configure_logging(settings.debug) | ||
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def lifespan(application: FastAPI): | ||
| consumer_service = KafkaAnalysisConsumerService(settings) | ||
| await consumer_service.start() | ||
| application.state.analysis_consumer_service = consumer_service | ||
| try: | ||
| yield | ||
| finally: | ||
| await consumer_service.stop() | ||
|
|
||
|
|
||
| def create_app() -> FastAPI: | ||
| application = FastAPI(title=f"{settings.app_name}-analysis-server", lifespan=lifespan) | ||
|
|
||
| @application.get("/") | ||
| async def root() -> dict[str, str]: | ||
| return {"app": settings.app_name, "mode": "analysis-server", "health": "/health", "ready": "/ready"} | ||
|
|
||
| @application.get("/health") | ||
| async def health() -> dict[str, object]: | ||
| return application.state.analysis_consumer_service.health_payload() | ||
|
|
||
| @application.get("/ready") | ||
| async def ready() -> dict[str, object]: | ||
| payload = application.state.analysis_consumer_service.readiness_payload() | ||
| if payload["ready"]: | ||
| return payload | ||
| raise HTTPException(status_code=503, detail=payload) | ||
|
|
||
| return application | ||
|
|
||
|
|
||
| app = create_app() | ||
|
|
||
|
|
||
| def run() -> None: | ||
| import uvicorn | ||
|
|
||
| uvicorn.run( | ||
| "app.analysis_server.main:app", | ||
| host=os.getenv("APP_HOST", "0.0.0.0"), | ||
| port=int(os.getenv("APP_PORT", "8000")), | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import time | ||
| from typing import Any | ||
|
|
||
| from aiokafka.abc import AbstractTokenProvider | ||
| from aiokafka.helpers import create_ssl_context | ||
| from aws_msk_iam_sasl_signer import MSKAuthTokenProvider | ||
|
|
||
| from app.core.config import Settings | ||
|
|
||
|
|
||
| class MskIamTokenProvider(AbstractTokenProvider): | ||
| def __init__(self, region: str) -> None: | ||
| self._region = region | ||
| self._token = "" | ||
| self._expiry_ms = 0 | ||
| self._lock = asyncio.Lock() | ||
|
|
||
| async def token(self) -> str: | ||
| async with self._lock: | ||
| now_ms = int(time.time() * 1000) | ||
| if self._token and now_ms < self._expiry_ms - 60_000: | ||
| return self._token | ||
|
|
||
| token, expiry_ms = await asyncio.get_running_loop().run_in_executor( | ||
| None, | ||
| MSKAuthTokenProvider.generate_auth_token, | ||
| self._region, | ||
| ) | ||
| self._token = token | ||
| self._expiry_ms = int(expiry_ms) | ||
| return token | ||
|
|
||
|
|
||
| def build_kafka_client_options(settings: Settings) -> dict[str, Any]: | ||
| options: dict[str, Any] = { | ||
| "bootstrap_servers": [server.strip() for server in settings.kafka_bootstrap_servers.split(",") if server.strip()], | ||
| } | ||
| security_protocol = settings.kafka_security_protocol.strip().upper() | ||
| if not security_protocol: | ||
| return options | ||
|
|
||
| options["security_protocol"] = security_protocol | ||
| if security_protocol in {"SSL", "SASL_SSL"}: | ||
| options["ssl_context"] = create_ssl_context() | ||
|
|
||
| if security_protocol.startswith("SASL"): | ||
| sasl_mechanism = settings.kafka_sasl_mechanism.strip().upper() | ||
| if not sasl_mechanism: | ||
| raise RuntimeError("KAFKA_SASL_MECHANISM must be set when using a SASL security protocol.") | ||
|
|
||
| options["sasl_mechanism"] = sasl_mechanism | ||
| if sasl_mechanism == "OAUTHBEARER": | ||
| region = settings.kafka_aws_region.strip() | ||
| if not region: | ||
| raise RuntimeError("KAFKA_AWS_REGION or AWS_REGION must be set for SASL/OAUTHBEARER.") | ||
| options["sasl_oauth_token_provider"] = MskIamTokenProvider(region) | ||
|
|
||
| return options |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| from fastapi import APIRouter, Depends | ||
| from fastapi import APIRouter, BackgroundTasks, Depends | ||
| from fastapi.responses import Response | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.core.database import get_db_session | ||
| from app.schemas.recommendation import RecommendationRequest, RecommendationResponse | ||
| from app.services.recommendation_service import get_recommendation | ||
| from app.schemas.recommendation import RecommendationRequest | ||
| from app.services.recommendation_service import run_recommendation_and_publish_to_kafka | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.post("/recommendations", response_model=RecommendationResponse) | ||
| @router.post("/recommendations", status_code=202) | ||
| async def post_recommendations( | ||
| body: RecommendationRequest, | ||
| background_tasks: BackgroundTasks, | ||
| session: AsyncSession = Depends(get_db_session), | ||
| ) -> RecommendationResponse: | ||
| return await get_recommendation( | ||
| session=session, | ||
| member_id=body.member_id, | ||
| profile_text=body.profile_text, | ||
| ) | ||
| ) -> Response: | ||
| """ | ||
| 202 Accepted ์ฆ์ ๋ฐํ. ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ถ์ฒ ์์ฑ ํ Kafka recommendation-topic ๋ฐํ. | ||
| Spring์ด Kafka consume โ persona_recommendation ์ ์ฌ โ CompletableFuture.complete(๊ฒฐ๊ณผ). | ||
| """ | ||
| _ = session | ||
| background_tasks.add_task(run_recommendation_and_publish_to_kafka, body.member_id) | ||
| return Response(status_code=202, content=None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prepare_response_dispatch๋ฉ์๋์WHERE์กฐ๊ฑด์ด ๋ถ์ถฉ๋ถํ์ฌ ๋ฉ์์ง ์ค๋ณต ๋ฐํ์ ์ํ์ด ์์ต๋๋ค. ํ์ฌdispatch_status <> 'ACKED'๋ง ํ์ธํ๊ณ ์์ด,SENT์ํ์ธ ๋ฉ์์ง๋ ๋ค์ ์ฒ๋ฆฌ ๋์์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.kafka_analysis_consumer_service์์ ๋ฐฐ์น ์ฒ๋ฆฌ ์ค ์ผ๋ถ ๋ฉ์์ง ๋ฐํ ์คํจ๋ก ์ฌ์๋๊ฐ ๋ฐ์ํ๋ฉด, ์ด๋ฏธ ์ฑ๊ณต์ ์ผ๋ก ๋ฐํ๋ ๋ฉ์์ง๋ค์ด ๋ค์ ๋ฐํ๋ ์ ์์ต๋๋ค.WHERE์กฐ๊ฑด์SENT์ํ๋ ์ ์ธํ์ฌ ์ค๋ณต ์ฒ๋ฆฌ๋ฅผ ๋ฐฉ์งํด์ผ ํฉ๋๋ค.