fix: 비 오는데 바깥 각이 1위 — 초단기예보가 비라고 한 시각은 각 점수 계산에 비로 넣고, 초단기예보가 새로 나올 때마다 다시 계산
This commit is contained in:
1 parent
db233d8ffc
commit
8a677a766d
3 files changed
+80
-17
No files matched your search
@@ -142,17 +142,35 @@ class NowService:
|
||||
)
|
||||
|
||||
async def _ultra(self, region: Region, now: datetime) -> Optional[list[RainHour]]:
|
||||
key = f"ultra:{region.code}"
|
||||
base = latest_ultra_base(now)
|
||||
try:
|
||||
cached = await REDIS.conn.get(key)
|
||||
if cached:
|
||||
data = orjson.loads(cached)
|
||||
if data["base"] == base.isoformat():
|
||||
return [RainHour(at=datetime.fromisoformat(h["at"]), rain_type=RainType(h["rain_type"]), rain_mm=h["rain_mm"]) for h in data["hours"]]
|
||||
return await ultra_hours(region, now)
|
||||
|
||||
|
||||
_ULTRA_WAIT_SEC, _ULTRA_WAIT_TRIES = 0.3, 30 # 다른 요청이 받아 오는 중이면 캐시가 찰 때까지 약 9초 기다린다
|
||||
|
||||
|
||||
async def _cached_ultra(key: str, base: datetime) -> Optional[list[RainHour]]:
|
||||
cached = await REDIS.conn.get(key)
|
||||
if not cached:
|
||||
return None
|
||||
data = orjson.loads(cached)
|
||||
if data["base"] != base.isoformat():
|
||||
return None
|
||||
return [RainHour(at=datetime.fromisoformat(h["at"]), rain_type=RainType(h["rain_type"]), rain_mm=h["rain_mm"]) for h in data["hours"]]
|
||||
|
||||
|
||||
async def ultra_hours(region: Region, now: datetime, wait: bool = False) -> Optional[list[RainHour]]:
|
||||
"""초단기예보(앞으로 6시간 비). 실패하면 None. 다른 요청이 받아 오는 중이면 wait 일 때만 기다리고 아니면 None."""
|
||||
key = f"ultra:{region.code}"
|
||||
base = latest_ultra_base(now)
|
||||
try:
|
||||
for attempt in range(_ULTRA_WAIT_TRIES if wait else 1):
|
||||
if attempt:
|
||||
await asyncio.sleep(_ULTRA_WAIT_SEC)
|
||||
if (hours := await _cached_ultra(key, base)) is not None:
|
||||
return hours
|
||||
async with redis_lock(f"lock:ultra:{region.code}") as acquired:
|
||||
if not acquired:
|
||||
return None
|
||||
continue
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
parsed = parse_ultra(await fetch_ultra(client, region.nx, region.ny, base))
|
||||
hours = [RainHour(at=p["at"], rain_type=p["rain_type"], rain_mm=p["rain_mm"]) for p in parsed]
|
||||
@@ -160,6 +178,7 @@ class NowService:
|
||||
payload = {"base": base.isoformat(), "hours": [{"at": h.at.isoformat(), "rain_type": h.rain_type.value, "rain_mm": h.rain_mm} for h in hours]}
|
||||
await REDIS.conn.set(key, orjson.dumps(payload), ex=max(ttl, 60))
|
||||
return hours
|
||||
except _FETCH_ERRORS as e:
|
||||
LOG.error(f"[now] ultra {region.code}: {e}")
|
||||
return None
|
||||
return None
|
||||
except _FETCH_ERRORS as e:
|
||||
LOG.error(f"[now] ultra {region.code}: {e}")
|
||||
return None
|
||||
Reference in new issue
Block a user