Skip to content

Commit f7b41d4

Browse files
authored
Implement hybrid approach: startup + daily 1 AM cache refresh
1 parent f9a9953 commit f7b41d4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

app.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,31 @@ def schedule_daily_refresh():
9494
# Refresh the cache
9595
asyncio.run(refresh_daily_cache())
9696

97+
def startup_refresh_thread():
98+
"""Background thread to perform initial startup refresh."""
99+
print(f"[{datetime.now()}] Starting initial cache refresh in background thread...")
100+
asyncio.run(refresh_daily_cache())
101+
print(f"[{datetime.now()}] Startup cache refresh complete")
102+
97103
@app.on_event("startup")
98104
async def startup_event():
99-
"""Initialize cache on startup and start background refresh thread."""
105+
"""Initialize cache on startup and start background refresh threads.
106+
107+
Hybrid approach:
108+
1. Immediate refresh on startup (in background thread)
109+
2. Daily scheduled refresh at 1 AM
110+
"""
100111
print(f"[{datetime.now()}] Application starting up...")
101-
# Initial cache load
102-
await refresh_daily_cache()
103-
# Start background thread for daily refresh
112+
113+
# Start background thread for immediate startup refresh
114+
startup_thread = threading.Thread(target=startup_refresh_thread, daemon=True)
115+
startup_thread.start()
116+
117+
# Start background thread for daily refresh at 1 AM
104118
refresh_thread = threading.Thread(target=schedule_daily_refresh, daemon=True)
105119
refresh_thread.start()
106-
print(f"[{datetime.now()}] Daily refresh scheduler started")
120+
121+
print(f"[{datetime.now()}] Startup refresh and daily scheduler started")
107122

108123
@app.get("/", response_class=HTMLResponse)
109124
async def root(request: Request):

0 commit comments

Comments
 (0)