Skip to content

Commit 41d7096

Browse files
committed
Move to lifespan function.
1 parent 28672cb commit 41d7096

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

pyfunceble_webworker/main.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import logging
5656
import os
5757
import secrets
58+
from contextlib import asynccontextmanager
5859

5960
import inflection
6061
import PyFunceble.facility
@@ -125,12 +126,23 @@
125126
pyfunceble_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+
128139
app = 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():
190202
app.openapi = custom_openapi
191203

192204

193-
@app.on_event("shutdown")
194205
def 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

Comments
 (0)