Skip to content

Commit f99d37c

Browse files
committed
Wait for GeoIP databases to be ready before starting monitor
Replaces one-shot os.path.exists() check with a retry loop that attempts to open the geoip2 Readers directly. If the files are missing or still being written (e.g. geoipupdate container still downloading), the monitor waits 5s and retries. A successful Reader open guarantees the file is complete and valid, since geoip2 reads the MMDB header on open.
1 parent 25fa735 commit f99d37c

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

monitor.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,15 @@ def get_geo_maximal(ip, city_reader, asn_reader):
153153
def monitor(save_knocks=False):
154154
init_db()
155155
r = redis.Redis(host=os.environ.get('REDIS_HOST', 'localhost'), port=6379, db=0, decode_responses=True)
156-
c_reader = geoip2.database.Reader(GEOIP_CITY_PATH) if os.path.exists(GEOIP_CITY_PATH) else None
157-
a_reader = geoip2.database.Reader(GEOIP_ASN_PATH) if os.path.exists(GEOIP_ASN_PATH) else None
156+
while True:
157+
try:
158+
c_reader = geoip2.database.Reader(GEOIP_CITY_PATH)
159+
a_reader = geoip2.database.Reader(GEOIP_ASN_PATH)
160+
print("✅ GeoIP databases loaded")
161+
break
162+
except Exception as e:
163+
print(f"⏳ Waiting for GeoIP databases... ({e})")
164+
time.sleep(5)
158165

159166
# Seed Redis totals from SQLite on startup to stay in sync
160167
try:

0 commit comments

Comments
 (0)