@@ -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" )
98104async 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 )
109124async def root (request : Request ):
0 commit comments