fix(realestate): listings-ingest async(BackgroundTask)+매칭/알림 타이밍 로그 + notify 홍수방지(cap+baseline)

This commit is contained in:
2026-07-10 11:11:36 +09:00
parent 28418b9f5d
commit 8e28ce9ae5
5 changed files with 56 additions and 13 deletions

View File

@@ -1,8 +1,9 @@
"""naver-fetch 워커 내부 계약: targets 조회 + listings ingest."""
import os
import time
import logging
from typing import List, Dict, Any
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, BackgroundTasks
from pydantic import BaseModel
from .auth import verify_internal_key
@@ -45,7 +46,7 @@ class ListingsIngest(BaseModel):
@router.post("/api/internal/realestate/listings-ingest",
dependencies=[Depends(verify_internal_key)])
def listings_ingest(body: ListingsIngest):
def listings_ingest(body: ListingsIngest, background_tasks: BackgroundTasks):
received = new = 0
for batch in body.batches:
for raw in batch.articles:
@@ -60,10 +61,18 @@ def listings_ingest(body: ListingsIngest):
new += 1
except Exception as e:
logger.warning("네이버 ingest 파싱 실패: %s", e)
matched = 0
if received:
with match_notify_lock:
run_listing_matching()
noti = notify_new_listings()
matched = int(noti.get("sent", 0)) if isinstance(noti, dict) else 0
return {"received": received, "new": new, "matched": matched}
background_tasks.add_task(_match_and_notify)
return {"received": received, "new": new, "queued": bool(received)}
def _match_and_notify():
"""ingest 후처리(백그라운드): 매칭+알림을 공유 락으로 직렬화. 타이밍 로그로 병목 관측."""
with match_notify_lock:
t0 = time.time()
run_listing_matching()
t1 = time.time()
noti = notify_new_listings()
t2 = time.time()
sent = (noti or {}).get("sent") if isinstance(noti, dict) else noti
logger.info("ingest 후처리: match=%.1fs notify=%.1fs sent=%s", t1 - t0, t2 - t1, sent)