aboutsummaryrefslogtreecommitdiff
path: root/api/main.py
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-12-19 16:35:18 -0600
committerParker <contact@pkrm.dev>2024-12-19 16:35:18 -0600
commit55e7ee145c470e832b56c9540f520aedf8792c15 (patch)
treed334be8a1138e238c6b9a5cc4e01cc740320a34a /api/main.py
parentcfbaf8a0b2afa86cd06ca10932e59cc27071f714 (diff)
Add database cleanup schedulerdev
Diffstat (limited to 'api/main.py')
-rw-r--r--api/main.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/api/main.py b/api/main.py
index 0e45e8a..5e38a1c 100644
--- a/api/main.py
+++ b/api/main.py
@@ -6,10 +6,31 @@ from api.routes.links_routes import router as links_router
from api.routes.user_routes import router as user_router
from api.routes.log_routes import router as log_router
from typing import Annotated
+from apscheduler.schedulers.background import BackgroundScheduler
+from contextlib import asynccontextmanager
from api.util.db_dependency import get_db
+from api.util.clean_db import clean_db
from api.util.log import log
from models import Link
+from config import LOG
+
+scheduler = BackgroundScheduler()
+
+
+# Handle the database cleanup scheduler
+def start_scheduler():
+ scheduler.add_job(clean_db, "interval", days=1)
+ scheduler.start()
+
+
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+ try:
+ start_scheduler()
+ yield
+ finally:
+ scheduler.shutdown()
app = FastAPI(
@@ -21,6 +42,7 @@ app = FastAPI(
"identifier": "Unlicense",
"url": "https://unlicense.org",
},
+ lifespan=lifespan,
)
app.add_middleware(