Proper app cleanup

This commit is contained in:
Zoe Roux 2025-05-12 13:06:18 +02:00
parent decb09ecca
commit ffd4fb2fa6
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

View File

@ -25,9 +25,9 @@ async def lifespan(_):
get_db() as db,
KyooClient() as client,
TheMovieDatabase() as tmdb,
RequestProcessor(db, client, CompositeProvider(tmdb)) as processor
):
processor = RequestProcessor(db, client, CompositeProvider(tmdb))
await processor.listen_for_requests()
# creating the processor makes it listen to requests event in pg
async with (
get_db() as db,
KyooClient() as client,

View File

@ -1,6 +1,7 @@
from __future__ import annotations
from logging import getLogger
from types import TracebackType
from typing import Annotated, Literal
from asyncpg import Connection
@ -61,9 +62,18 @@ class RequestProcessor:
self._client = client
self._providers = providers
async def listen_for_requests(self):
async def __aenter__(self):
logger.info("Listening for requestes")
await self._database.add_listener("scanner.requests", self.process_request)
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
):
await self._database.remove_listener("scanner.requests", self.process_request)
async def process_request(self):
cur = await self._database.fetchrow(