5555import logging
5656import os
5757import secrets
58+ from contextlib import asynccontextmanager
5859
5960import inflection
6061import PyFunceble .facility
125126pyfunceble_config_loader .start ()
126127
127128
129+ @asynccontextmanager
130+ async def lifespan (app : FastAPI ):
131+ await periodic_data_update ()
132+ await periodic_location_update ()
133+
134+ yield
135+
136+ cleanup_data_dir ()
137+
138+
128139app = FastAPI (
129140 title = assets_defaults .PROJECT_NAME ,
130141 description = assets_defaults .PROJECT_DESCRIPTION ,
131142 version = __version__ ,
132143 docs_url = None ,
133144 redoc_url = None ,
145+ lifespan = lifespan ,
134146)
135147
136148
@@ -190,18 +202,18 @@ def custom_openapi():
190202app .openapi = custom_openapi
191203
192204
193- @app .on_event ("shutdown" )
194205def cleanup_data_dir () -> None :
195206 """
196207 Cleanup our data directory on shutdown.
197208 """
198209
210+ logging .info ("Starting to cleanup %s" , PyFunceble .storage .CONFIG_DIRECTORY )
199211 DirectoryHelper (PyFunceble .storage .CONFIG_DIRECTORY ).delete ()
212+ logging .info ("Finished to cleanup %s" , PyFunceble .storage .CONFIG_DIRECTORY )
200213
201214
202- @app .on_event ("startup" )
203215@repeat_every (seconds = 60 * 60 * 24 , wait_first = False )
204- def periodic_data_update () -> None :
216+ async def periodic_data_update () -> None :
205217 """
206218 Process a periodic update of PyFunceble internal files.
207219 """
@@ -226,9 +238,8 @@ def periodic_data_update() -> None:
226238 logging .info ("Finished to update PyFunceble's IANA dataset." )
227239
228240
229- @app .on_event ("startup" )
230241@repeat_every (seconds = 60 * 70 , wait_first = False , raise_exceptions = True )
231- def periodic_location_update ():
242+ async def periodic_location_update ():
232243 """
233244 Process a periodic update of our location.
234245 """
0 commit comments