feat(naver-fetch): FastAPI lifespan + heartbeat 배선 + /health
This commit is contained in:
53
services/naver-fetch/main.py
Normal file
53
services/naver-fetch/main.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""naver-fetch FastAPI entry — lifespan(fetch_loop + heartbeat_loop) + /health."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
import redis.asyncio as aioredis
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
import fetch_worker
|
||||||
|
from config import load_settings
|
||||||
|
from nas_client import NASClient
|
||||||
|
from naver_client import NaverClient
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO,
|
||||||
|
format="%(asctime)s %(name)s %(levelname)s %(message)s")
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
settings = load_settings()
|
||||||
|
nas = NASClient(settings.nas_base_url, settings.internal_api_key)
|
||||||
|
naver = NaverClient(settings.naver_real_estate_type, settings.naver_token_url,
|
||||||
|
settings.naver_headless)
|
||||||
|
state = fetch_worker.FetchState()
|
||||||
|
redis = aioredis.from_url(settings.redis_url, decode_responses=False)
|
||||||
|
|
||||||
|
ft = asyncio.create_task(fetch_worker.fetch_loop(nas, naver, state, settings))
|
||||||
|
hb = asyncio.create_task(fetch_worker.heartbeat_loop(redis, state))
|
||||||
|
logger.info("naver-fetch lifespan 시작")
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
for t in (ft, hb):
|
||||||
|
t.cancel()
|
||||||
|
try:
|
||||||
|
await t
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
await naver.close()
|
||||||
|
await nas.close()
|
||||||
|
await redis.aclose()
|
||||||
|
logger.info("naver-fetch lifespan 종료")
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
def health():
|
||||||
|
return {"ok": True, "service": "naver-fetch"}
|
||||||
6
services/naver-fetch/tests/test_health.py
Normal file
6
services/naver-fetch/tests/test_health.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
"""/health — 핸들러 직접 검증."""
|
||||||
|
from main import health
|
||||||
|
|
||||||
|
|
||||||
|
def test_health():
|
||||||
|
assert health() == {"ok": True, "service": "naver-fetch"}
|
||||||
Reference in New Issue
Block a user