From b834799a2d5a09d737b77fa0cfdc6a6aeab541d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=95=E8=B0=B7=E9=85=B1?= Date: Fri, 19 Sep 2025 17:03:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ip=E5=BA=93=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/geoip_helper.py | 13 +++++++++++++ app/service/init_geoip.py | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/helpers/geoip_helper.py b/app/helpers/geoip_helper.py index 3d22927..9222abd 100644 --- a/app/helpers/geoip_helper.py +++ b/app/helpers/geoip_helper.py @@ -104,16 +104,29 @@ class GeoIPHelper: return max(files, key=os.path.getmtime) if files else None def update(self, force=False): + from app.log import logger + for ed in self.editions: eid = EDITIONS[ed] path = self._latest_file(eid) need = force or not path + if path: age_days = (time.time() - os.path.getmtime(path)) / 86400 if age_days >= self.max_age_days: need = True + logger.info(f"[GeoIP] {eid} database is {age_days:.1f} days old (max: {self.max_age_days}), will download new version") + else: + logger.info(f"[GeoIP] {eid} database is {age_days:.1f} days old, still fresh (max: {self.max_age_days})") + else: + logger.info(f"[GeoIP] {eid} database not found, will download") + if need: + logger.info(f"[GeoIP] Downloading {eid} database...") path = self._download_and_extract(eid) + logger.info(f"[GeoIP] {eid} database downloaded successfully") + else: + logger.info(f"[GeoIP] Using existing {eid} database") old = self._readers.get(ed) if old: diff --git a/app/service/init_geoip.py b/app/service/init_geoip.py index 6ce8322..95a2edf 100644 --- a/app/service/init_geoip.py +++ b/app/service/init_geoip.py @@ -20,8 +20,9 @@ async def init_geoip(): logger.info("[GeoIP] Initializing GeoIP database...") # Run the synchronous update method in a background thread + # force=False means only download if files don't exist or are expired loop = asyncio.get_event_loop() - await loop.run_in_executor(None, geoip.update) + await loop.run_in_executor(None, lambda: geoip.update(force=False)) logger.info("[GeoIP] GeoIP database initialization completed") except Exception as e: