优化ip库获取逻辑
This commit is contained in:
@@ -104,16 +104,29 @@ class GeoIPHelper:
|
|||||||
return max(files, key=os.path.getmtime) if files else None
|
return max(files, key=os.path.getmtime) if files else None
|
||||||
|
|
||||||
def update(self, force=False):
|
def update(self, force=False):
|
||||||
|
from app.log import logger
|
||||||
|
|
||||||
for ed in self.editions:
|
for ed in self.editions:
|
||||||
eid = EDITIONS[ed]
|
eid = EDITIONS[ed]
|
||||||
path = self._latest_file(eid)
|
path = self._latest_file(eid)
|
||||||
need = force or not path
|
need = force or not path
|
||||||
|
|
||||||
if path:
|
if path:
|
||||||
age_days = (time.time() - os.path.getmtime(path)) / 86400
|
age_days = (time.time() - os.path.getmtime(path)) / 86400
|
||||||
if age_days >= self.max_age_days:
|
if age_days >= self.max_age_days:
|
||||||
need = True
|
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:
|
if need:
|
||||||
|
logger.info(f"[GeoIP] Downloading {eid} database...")
|
||||||
path = self._download_and_extract(eid)
|
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)
|
old = self._readers.get(ed)
|
||||||
if old:
|
if old:
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ async def init_geoip():
|
|||||||
logger.info("[GeoIP] Initializing GeoIP database...")
|
logger.info("[GeoIP] Initializing GeoIP database...")
|
||||||
|
|
||||||
# Run the synchronous update method in a background thread
|
# 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()
|
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")
|
logger.info("[GeoIP] GeoIP database initialization completed")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user