fix: 오늘 날씨 0~6시가 비면 전날 23시 발표를 다시 받아 채움

This commit is contained in:
minaChoi committed 2026-10-07 20:02:29 +09:00
1 parent 4ebf1cd894
commit 73294622ad
3 files changed
+58 -6

No files matched your search

+19 -5
View File
@@ -170,19 +170,33 @@ class WeatherService:
"""예보가 지났으면 새로 받는다. 이미 있으면 뒤에서 갱신하고, background=False 면 받을 때까지 기다린다."""
latest = latest_vilage_base(now)
err, issued = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.latest_issued(s, region.code))
if err is not ErrorType.SUCCESS or (issued is not None and issued >= latest):
if err is not ErrorType.SUCCESS:
return
from_midnight = issued is None or await self._midnight_missing(region, now)
if issued is not None and issued >= latest and not from_midnight:
return
if issued is None or not background:
await self._refresh(region, now, latest, wait=issued is None)
await self._refresh(region, now, latest, wait=issued is None, from_midnight=from_midnight)
return
task = asyncio.create_task(self._refresh(region, now, latest, wait=False))
task = asyncio.create_task(self._refresh(region, now, latest, wait=False, from_midnight=from_midnight))
_background.add(task)
task.add_done_callback(_background.discard)
async def _refresh(self, region: Region, now: datetime, latest: datetime, wait: bool) -> None:
async def _midnight_missing(self, region: Region, now: datetime) -> bool:
"""오늘 0시 칸이 비었는지. 전날 23시 발표 받기가 실패했을 때 다시 받되, 10분에 한 번만 시도한다."""
err, has = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.has_hourly_at(s, region.code, kst_midnight(now.date())))
if err is not ErrorType.SUCCESS or has:
return False
try:
return bool(await REDIS.conn.set(f"vilage0:{region.code}:{now:%Y%m%d}", "1", ex=600, nx=True))
except RedisError as e:
LOG.error(f"[weather] {region.code} midnight retry: {e}")
return False
async def _refresh(self, region: Region, now: datetime, latest: datetime, wait: bool, from_midnight: bool) -> None:
async with redis_lock(f"lock:vilage:{region.code}") as acquired:
if acquired:
bases = [day_start_base(now.date()), latest] if wait else [latest]
bases = [day_start_base(now.date()), latest] if from_midnight else [latest]
await self._fetch_and_store(region, sorted(set(bases)))
return
if wait: